Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Narrow is with final types correctly #15646

wants to merge 5 commits into from

Conversation

sobolevn
Copy link
Member

Now we can narrow @final types with is and is not.

Closes #15553

@github-actions

This comment has been minimized.

@sobolevn
Copy link
Member Author

Problems looks real, checking.

@github-actions

This comment has been minimized.

@sobolevn sobolevn requested a review from JukkaL July 13, 2023 13:53
sobolevn added a commit to sobolevn/litestar that referenced this pull request Jul 15, 2023
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():

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:

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.

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.

Copy link
Contributor

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]"

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?

Copy link
Contributor

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Narrow x is Sentinel where x is Union[type[Sentinel], ...] and Sentinel is @final
5 participants