8000 Narrow `is` with final types correctly by arvi18 · Pull Request #6 · coderabbit-test/mypy · GitHub
[go: up one dir, main page]

Skip to content

Narrow is with final types correctly #6

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Narrow is with final types correctly
  • Loading branch information
sobolevn committed Jul 12, 2023
commit 99d4465bbb8026525ba54d82b47b02fb0fff4158
4 changes: 4 additions & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,10 @@ def covers_at_runtime(item: Type, supertype: Type) -> bool:
item = get_proper_type(item)
supertype = get_proper_type(supertype)

if isinstance(item, (CallableType, TypeType)) and item.is_singleton_type():
if is_proper_subtype(item, supertype):
return True
Comment on lines +1968 to +1970

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This condition checks if the item is a singleton type and a proper subtype of the supertype. Can you add a comment explaining what a singleton type is in this context, and why this check is needed?

Suggested change
if isinstance(item, (CallableType, TypeType)) and item.is_singleton_type():
if is_proper_subtype(item, supertype):
return True
# Check if the item is a singleton type (e.g., `None`, final classes)
# and a proper subtype of the supertype.
if isinstance(item, (CallableType, TypeType)) and item.is_singleton_type():
if is_proper_subtype(item, supertype):


# Since runtime type checks will ignore type arguments, erase the types.
supertype = erase_type(supertype)
if is_proper_subtype(
Expand Down
9 changes: 9 additions & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,9 @@ def with_unpacked_kwargs(self) -> NormalizedCallableType:
)
)

def is_singleton_type(self) -> bool:
return self.is_type_obj() and self.type_object().is_final

def __hash__(self) -> int:
# self.is_type_obj() will fail if self.fallback.type is a FakeInfo
if isinstance(self.fallback.type, FakeInfo):
Expand Down Expand Up @@ -2856,6 +2859,12 @@ def __eq__(self, other: object) -> bool:
return NotImplemented
return self.item == other.item

def is_singleton_type(self) -> bool:
return (
(isinstance(self.item, Instance) and self.item.type.is_final)
or isinstance(self.item, NoneType)
)

def serialize(self) -> JsonDict:
return {".class": "TypeType", "item": self.item.serialize()}

Expand Down
76 changes: 76 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -1269,3 +1269,79 @@ def g() -> None:
def foo(): ...
foo()
[builtins fixtures/dict.pyi]


[case testNarrowingIsFinalType]
from typing import Type, Union
from typing_extensions import final

@final
class Mark: ...

@final
class Other: ...

x: Union[Type[Mark], Type[Other], Type[None], int]

if x is Mark:
reveal_type(x) # N: Revealed type is "Type[__main__.Mark]"
else:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Other], Type[None], builtins.int]"

if x is not Mark:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Other], Type[None], builtins.int]"
else:
reveal_type(x) # N: Revealed type is "Type[__main__.Mark]"

y: Type[Mark] = Mark
if x is y:
reveal_type(x) # N: Revealed type is "Type[__main__.Mark]"
else:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Other], Type[None], builtins.int]"

if x is not y:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Other], Type[None], builtins.int]"
else:
reveal_type(x) # N: Revealed type is "Type[__main__.Mark]"

if x is type(None):
reveal_type(x) # N: Revealed type is "Type[None]"
else:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[__main__.Other], builtins.int]"

if x is not type(None):
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[__main__.Other], builtins.int]"
else:
reveal_type(x) # N: Revealed type is "Type[None]"

z: Type[None]
if x is z:
reveal_type(x) # N: Revealed type is "Type[None]"
else:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[__main__.Other], builtins.int]"

if x is not z:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[__main__.Other], builtins.int]"
else:
reveal_type(x) # N: Revealed type is "Type[None]"
[builtins fixtures/isinstancelist.pyi]


[case testNarrowingIsRegualType]
from typing import Type, Union

class Mark: ...

x: Union[Type[Mark], Type[None], int]

if x is Mark:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[None], builtins.int]"
else:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[None], builtins.int]"

y: Type[Mark] = Mark
if x is y:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[None], builtins.int]"
else:
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[None], builtins.int]"
[builtins fixtures/isinstancelist.pyi]
0