-
-
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
Narrow is
with final types correctly
#15646
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
Problems looks real, checking. |
This comment has been minimized.
This comment has been minimized.
mypy soon will have special narrowing for `@final` singleton types: python/mypy#15646 So, it is a good idea to use `@final` for such types.
): | ||
return False | ||
|
||
if isinstance(item, (CallableType, TypeType)) and item.is_singleton_type(): |
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.
What if the RHS of the is
operator is a specialized generic class like list[int]
? The is
conditional will always evaluate to False then.
|
||
x: Union[Type[Mark], Type[Other], Type[None], int] | ||
|
||
if x is Mark: |
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.
What about M = NewType("M", Mark")
? This acts as a subtype of Mark
(and is allowed even though Mark
is marked @final
), but x is Mark
will evaluate to false if x
is M
. Maybe that's an edge case that can be ignored, but it does represent a small hole.
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.
I guess we can ignore this edge case. I'd be surprised if users are passing the NewType
definition somewhere as a value, and even more in this particular situation combined with a @final
. The benefit from this feature outweighs supporting the edge case.
And even then, isn't the issue only in the opposite case, when we have if x is not Marc:
? A situation where at runtime we do enter the block, but we narrowed incorrectly.
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.
Why can you create a NewType
of a final
type? That seems inconsistent - NewType
creates a subtype, but final
means that the type isn't allowed to have subclasses. I don't see this mentioned anywhere in the docs/PEPs. I guess it could mean that you have a subset of values, but they all will be the same type in reality. It's still a little contradictory though.
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]" |
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.
Why does this not narrow the type to Type[Mark]
in the positive (if) case?
Diff from mypy_primer, showing the effect of this PR on open source code: trio (https://github.com/python-trio/trio)
+ src/trio/_core/_local.py:54: error: Unused "type: ignore" comment [unused-ignore]
+ src/trio/_core/_local.py:57: error: Unused "type: ignore" comment [unused-ignore]
steam.py (https://github.com/Gobot1234/steam.py)
+ steam/ext/commands/converters.py:532: error: Unused "type: ignore" comment [unused-ignore]
|
Now we can narrow
@final
types withis
andis not
.Closes #15553