-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Type checking stops without error messages after uninhabited type detected #3154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
My bad, the fact that So the only issue is that once mypy detects this uninhabited type, it considers the rest of the current scope unreachable, and stops type checking. Specifically, in the example below, the user gets no errors at all:
|
That sounds like the binder has discovered some dead code for you. :-) Agreed it's disconcerting that everything is silent -- that's a general concern I have about mypy's current approach to dead code (after |
But in this case, I'd argue the code isn't dead, in the sense that it's definitely going to be reached in runtime. |
But if mypy infers that a value of type "uninhabited" is returned somewhere, you can't blame it for thinking that code is unreachable, because (in the type system) there are no such values. I think the (Honestly, I think you still haven't quite got the hang of type variables. There really are only three places where they are currently valid: in generic classes, generic functions, and generic type aliases. Your example is neither, and things like |
That would be the case if the type of In fact, in a similar situation with module-level functions, mypy reveals
I agree (the entire example I presented was a result of an accidental omission of a type variable for an argument my part; I was surprised mypy said nothing about it). I think the situations where some type variable can never be constrained are a sign of a programmer mistake. Since @sixolet is already working on the PR for that, I don't think there's a need to create an issue for it.
As I read PEP 484 it seems to say that this is syntactically a valid generic function:
Of course, semantically, mypy is free to figure out that this annotation is meaningless, and report an error. But I don't think it's easy to disallow this structure just based on the syntax (e.g., we can't say that a generic function must have type parameters in its arguments, since that will change after #3113 is merged). |
Just wanted to note that we already have #2885 for problems with |
If #2885 is resolved, my particular example will no longer cause any problems. I'd still prefer to avoid the combination of these two behaviors:
But I think it's best to close this for now, since it would be hard to justify spending time on this without a specific problem to solve. |
@ddfisher ^^^ is this an intentional use of uninhabited or some kind of bug? |
A bit of both, IMO:
|
OK, then (1) should be fixed. Re: (2), is NoReturn then equivalent to uninhabited? Re: (3) I am not too happy with that but changing it would be a huge hassle for our codebase so I guess that's also a feature... |
Maybe reopen with new title or open a new issue for (1)? |
Ok, I'll open a new issue, let me just try a few things first to make sure I understand the issue well. |
Yes, it is the same. |
Under some conditions (it seems the trigger is that generic method has arguments without type hints), a spurious uninhabited type appears:
The first revealed type is correct; the second I think is wrong; the third is definitely wrong. (Of course, the real problem is that any code after
cls().f()
is not type checked.)The output is unaffected by the presence of
--strict
flag.This problem disappears if method
f
is redefined asThe text was updated successfully, but these errors were encountered: