-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Unable to narrow unions including EllipsisType or NoneType using the identity of their singleton #13117
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
I tested with bool. It almost works correctly, but it's a little over-eager. Checking for either True or False will narrow out both boolean values:
|
The |
Ah, that makes sense. So the relevant parallel case for boolean literals is something like this:
which works like I'd expect it to. |
Summarizing the above discussion, these cases are relevant:
I think that the implementation will have to touch |
After this change, narrowing to `Ellipsis` works similarly with regards to narrowing as `None` in `Optional`s It would be a good followup refactor to delegate some of the logic from `is_singleton_type` to the actual mypy types so they could decide for themselves if they are representing singleton objects Fixes: python#13117 Co-authored-by: Zsolt Cserna <[email protected]>
I think, the |
After this change, narrowing to `Ellipsis` works similarly with regards to narrowing as `None` in `Optional`s It would be a good followup refactor to delegate some of the logic from `is_singleton_type` to the actual mypy types so they could decide for themselves if they are representing singleton objects Fixes: python#13117 Co-authored-by: Zsolt Cserna <[email protected]>
#13157) After this change, narrowing to `Ellipsis` works similarly with regards to narrowing as `None` in `Optional`s It would be a good followup refactor to delegate some of the logic from `is_singleton_type` to the actual mypy types so they could decide for themselves if they are representing singleton objects Fixes: #13117 Co-authored-by: Zsolt Cserna <[email protected]> Co-authored-by: László Vaskó <[email protected]> Co-authored-by: Zsolt Cserna <[email protected]>
This greatly reduces complexity as each type can determine for themselves if they are representing a singleton. `get_enum_values` has to be moved as well, as it is used by `get_singleton_type`. Relates-to: python#13117
This greatly reduces complexity as each type can determine for themselves if they are representing a singleton. `get_enum_values` has to be moved as well, as it is used by `get_singleton_type`. Relates-to: #13117 Co-authored-by: László Vaskó <[email protected]>
Nice! Thanks, @vlaci ! |
Bug Report
Variables whose type is a union that includes types.EllipsisType can't be narrowed using
if foo is Ellipsis
. That's unexpected to me, because Ellipsis is documented as the only instance of EllipsisType.Similar but slightly different issues occur with NoneType and NotImplementedType.
To Reproduce
Minimal reproducing case:
Mypy appears to believe that there might be values whose type is EllipsisType but which don't have the same identity as Ellipsis. I don't think that's possible.
Similar things happen with None and NoneType:
NotImplemented and NotImplementedType pass without error, but appear to do so for the wrong reasons:
Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: