Skip to content

Conversation

@hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Oct 2, 2024

# https://github.com/python/mypy/issues/17864
def f(x: str | int) -> None:
if x == "x":
reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]"
Copy link
Member

Choose a reason for hiding this comment

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

Is this desired behavior? I feel like we should narrow to Literal["x"]. Yes, it might be a subclass of int that compares equal with str, but don't do that.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, this case seems fine. But classes that compare equal to int are not unusual (e.g. Decimal), so it's not clear where to draw the line. We might need some ad-hoc rules to support common use cases.

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Oct 5, 2024

Choose a reason for hiding this comment

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

The last time I tried that change IIRC it looked pretty good on primer, I think the main obstacle was it exposed some shortcomings in the binder

@hauntsaninja hauntsaninja merged commit 4e4826f into python:master Oct 5, 2024
@hauntsaninja hauntsaninja deleted the regr-test branch October 5, 2024 01:33
hauntsaninja added a commit that referenced this pull request Jan 22, 2026
We've wanted to do this for a long time, but previous attempt was
reverted due to issues like #17841
, #17864 ,
#17869

Following #20492 we should now be in
a position to do this narrowing

Fixes #3229
Fixes #20234
Fixes #18208
Fixes #16774
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants