Skip to content

==-based narrowing of Optional #18135

Closed
@tyralla

Description

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

Activity

hauntsaninja

hauntsaninja commented on Nov 14, 2024

@hauntsaninja
Collaborator

I think both 2 and 3 are fine. If we go with 3, we should look at presence of __eq__ (int has __eq__ defined, not sure why, dates back to 2015)

tyralla

tyralla commented on Nov 14, 2024

@tyralla
CollaboratorAuthor

Did you mean we should check for __eq__ when going for solution 2?

If so, this could be an additional safety net for most cases. Then, the modified solution 2 would only result in wrong narrowing for subtypes that override __eq__ strangely:

class A: ...

class B(A):
    def __eq__(self, o: object) -> bool:
        return True

def f(x: A | None) -> None:
    if x == None:
        assert x is None

f(B())  # AssertionError

I think, for complete safety, we would have to combine looking for __eq__ with checking for final:

from typing import final

@final
class A: ...

def f(x: A | None) -> None:
    if x == None:
        reveal_type(x)  # must be None
added a commit that references this issue on Nov 18, 2024
1086dcb
added a commit that references this issue on Nov 19, 2024
e2a47e2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @hauntsaninja@tyralla

      Issue actions

        `==`-based narrowing of `Optional` · Issue #18135 · python/mypy