Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
Support narrowing unions that include type[None].#16315
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
05f2c711ea576c32ca5f4fc853e9fe12f7d58f056255b2858File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2022,3 +2022,73 @@ def f(x: Union[int, Sequence[int]]) -> None: | ||
| ): | ||
| reveal_type(x) # N: Revealed type is "Tuple[builtins.int, builtins.int]" | ||
| [builtins fixtures/len.pyi] | ||
| [case testNarrowingIsSubclassNoneType1] | ||
| from typing import Type, Union | ||
| def f(cls: Type[Union[None, int]]) -> None: | ||
| if issubclass(cls, int): | ||
| reveal_type(cls) # N: Revealed type is "Type[builtins.int]" | ||
| else: | ||
| reveal_type(cls) # N: Revealed type is "Type[None]" | ||
| [builtins fixtures/isinstance.pyi] | ||
| [case testNarrowingIsSubclassNoneType2] | ||
| from typing import Type, Union | ||
| def f(cls: Type[Union[None, int]]) -> None: | ||
| if issubclass(cls, type(None)): | ||
| reveal_type(cls) # N: Revealed type is "Type[None]" | ||
| else: | ||
| reveal_type(cls) # N: Revealed type is "Type[builtins.int]" | ||
| [builtins fixtures/isinstance.pyi] | ||
| [case testNarrowingIsSubclassNoneType3] | ||
| from typing import Type, Union | ||
| NoneType_ = type(None) | ||
| def f(cls: Type[Union[None, int]]) -> None: | ||
| if issubclass(cls, NoneType_): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. One case is still broken, I believe: (python3.10+)
CollaboratorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. You are right. The definition in types.pyi is not helpful. I assumed something similar to mentioned test: NoneType=type(None) But it is: @finalclassNoneType:def__bool__(self)->Literal[False]: ... My suggestion is to let | ||
| reveal_type(cls) # N: Revealed type is "Type[None]" | ||
| else: | ||
| reveal_type(cls) # N: Revealed type is "Type[builtins.int]" | ||
| [builtins fixtures/isinstance.pyi] | ||
| [case testNarrowingIsSubclassNoneType4] | ||
| # flags: --python-version 3.10 | ||
| from types import NoneType | ||
| from typing import Type, Union | ||
| def f(cls: Type[Union[None, int]]) -> None: | ||
| if issubclass(cls, NoneType): | ||
| reveal_type(cls) # N: Revealed type is "Type[None]" | ||
| else: | ||
| reveal_type(cls) # N: Revealed type is "Type[builtins.int]" | ||
| [builtins fixtures/isinstance.pyi] | ||
| [case testNarrowingIsInstanceNoIntersectionWithFinalTypeAndNoneType] | ||
| # flags: --warn-unreachable --python-version 3.10 | ||
| from types import NoneType | ||
| from typing import final | ||
| class X: ... | ||
| class Y: ... | ||
| @final | ||
| class Z: ... | ||
| x: X | ||
| if isinstance(x, (Y, Z)): | ||
| reveal_type(x) # N: Revealed type is "__main__.<subclass of "X" and "Y">" | ||
| if isinstance(x, (Y, NoneType)): | ||
| reveal_type(x) # N: Revealed type is "__main__.<subclass of "X" and "Y">1" | ||
| if isinstance(x, (Y, Z, NoneType)): | ||
| reveal_type(x) # N: Revealed type is "__main__.<subclass of "X" and "Y">2" | ||
| if isinstance(x, (Z, NoneType)): # E: Subclass of "X" and "Z" cannot exist: "Z" is final \ | ||
| # E: Subclass of "X" and "NoneType" cannot exist: "NoneType" is final | ||
| reveal_type(x) # E: Statement is unreachable | ||
| [builtins fixtures/isinstance.pyi] | ||