-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Do not intersect types in isinstance checks if at least one is final. #16330
Do not intersect types in isinstance checks if at least one is final. #16330
Conversation
Diff from mypy_primer, showing the effect of this PR on open source code: CPython (Argument Clinic) (https://github.com/python/cpython)
+ Tools/clinic/clinic.py:5830: error: Subclass of "Sentinels" and "bool" cannot exist: "bool" is final [unreachable]
+ Tools/clinic/clinic.py:5830: error: Subclass of "Sentinels" and "NoneType" cannot exist: "NoneType" is final [unreachable]
+ Tools/clinic/clinic.py:5830: error: Subclass of "Null" and "bool" cannot exist: "bool" is final [unreachable]
+ Tools/clinic/clinic.py:5830: error: Subclass of "Null" and "NoneType" cannot exist: "NoneType" is final [unreachable]
+ Tools/clinic/clinic.py:5831: error: Statement is unreachable [unreachable]
sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/util/typing.py:205: error: Unused "type: ignore" comment [unused-ignore]
|
Regarding the Mypy primer: The (correct) new error reports for For from typing import Any, Type, _SpecialForm
cls: Type[Any]
if isinstance(cls, _SpecialForm):
cls.asdf # error: Statement is unreachable |
The sphinx issue caused me some headaches, but I think I've collected some hints. The argument Interestingly, Mypy 1.6 agrees with my reasoning when annotating with from typing import Type
t1: type
t2: Type
if isinstance(t1, str):
reveal_type(t1) # note: Revealed type is "temp.<subclass of "type" and "str">"
if isinstance(t2, str):
reveal_type(t2) # error: Statement is unreachable If I change the annotation of So, everything seems clear. However, when applying Mypy 1.6 on the following example code involving from typing import Type, _SpecialForm
t1: type
t2: Type
if isinstance(t1, _SpecialForm): # error: Subclass of "type" and "<typing special form>" cannot exist: would have incompatible method signatures
reveal_type(t1) # error: Statement is unreachable
if isinstance(t2, _SpecialForm):
reveal_type(t2) # error: Statement is unreachable To conclude, strange things might be going on, but they are not related to this pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@KotlinIsland: Thanks for looking over it! |
@tyralla All good, I had started implementing this in basedmypy, but now I don't have to, thanks! |
Nice feature, thank you! |
Fixes #15148
I think it also fixes the initial bug reported in #12163 (this is why I added a TypeVar test case) but not this bug reported later in the same issue.