Skip to content

==-based narrowing of Optional #18135

Closed
@tyralla

Description

@tyralla
  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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions