patma
patma copied to clipboard
Should it be an error if there are cases past a "catch-all" case?
If a case is case _:
or case x:
, nothing can go past that. Should we make it an error if one of these is followed by another case?
I don't know what is the convention for Python, but in the environments I am used to this kind of thing is a lint error - the same as any 'unreachable code' warning:
if (something) {
return 1;
} else {
return 2;
}
print('Hello, world!'); // Warning - unreachable code
Hm, maybe.
My initial reaction is that this feels like the job of static analysis. For something to be elevated to the level of "error", I think we need to be exhaustive (i.e. not just catching trivial cases). Since that's far beyond the capabilities of Python's compiler, I'd say we just leave it for other tools to do better. I feel like Python's job is just to quietly optimize away obviously unreachable code (just like if False: ...
).
Maybe a warning, if we find it's a common enough problem. But those are typically reserved for relying on implementation details, or behavior that will change in the future. This is code that, left unchanged, will never suddenly start doing the "wrong" thing unexpectedly.
You've convinced me.
The one syntax warning that doesn't all in those categories is for assert(x, y)
-- but that's a particularly subtle trap. I don't think putting something after case _:
is subtle at all. Let's leave it to the linters.
I would also agree that it should not be an error in the compiler itself. Although I am not adding anything new here: you can already have unreachable code in Python otherwise. For instance, the compiler does not reject something like if False:
, either, nor code that follows a break
.