-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
MyPy does not understand types exclusion using operator "is" #15788
Comments
Not really, no. All you checked was that it isn't the https://mypy.readthedocs.io/en/stable/type_narrowing.html suggests that for type narrowing you can use first_item = next(items, stopper)
reveal_type(first_item) suggests that first_item is just object and that's it, it appears to have forgotten all about T. Perhaps For test purposes using dict() as a sentinel reveals that |
What I said, return type of Is it possible in Python type system ? In MyPy? So check |
Or for this code in particular, you could use a |
I think mypy is working correctly (as designed) in this case. Is there anything actionable from this issue, or should it be closed? |
Real example:
or, reduced:
check here: https://mypy-play.net/?mypy=latest&python=3.11
gives:
Which is obviously incorrect, since we checked that
first_item
is not an object(). I can not check forisinstance(fist_item, object)
because every object is object :). Seems, MyPy understandsisinstance()
assertions, but does not understandis
.Seems, type of
next(xxx, yyy)
should bexxx[0] | Literal[yyy]
instead ofxxx[0] | yyy
(pseudocode, I don't know how to express correctly)The text was updated successfully, but these errors were encountered: