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

==-based narrowing of Optional #18135

Open
tyralla opened this issue Nov 9, 2024 · 0 comments
Open

==-based narrowing of Optional #18135

tyralla opened this issue Nov 9, 2024 · 0 comments
Labels
feature topic-type-narrowing Conditional type narrowing / binder

Comments

@tyralla
Copy link
Collaborator

tyralla commented Nov 9, 2024

  1. Mypy's current behaviour:
x: int | None
if x == None:
    x  # "Union[builtins.int, None]"
else:
    x  # "Union[builtins.int, None]"
  1. Pyright's current behaviour:
x: int | None
if x == None:
    x  # x is None
else:
    x  # x is int
  1. I think the ideal solution would be:
x: int | None
if x == None:
    x  # "Union[builtins.int, None]"
else:
    x  # "builtins.int"

This Mypy code comment seems to favour solution 2.

I am asking because if Mypy would follow solution 2 or 3, in-based narrowing of optional types could be implemented more consistently.

Related pull requests: #15760 #17154 #17044.

@tyralla tyralla added feature topic-type-narrowing Conditional type narrowing / binder labels Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

1 participant