Skip to content

not isinstance(float_arg, float) should narrow to int; not isinstance(complex_arg, complex) should narrow to float #20484

@kmaglione

Description

@kmaglione

The typing specs require that a float typed argument also accept int values, and that a complex type argument accept both int and complex values. That implies that, if we have a val typed complex, and isinstance(val, complex) returns false, it can still be an int or float. So,

def test(x: float):
    if not isinstance(x, float):
        reveal_type(x)  # Should reveal int

and,

def test(x: complex):
    if not isinstance(x, complex):
        reveal_type(x)  # Should reveal int | float (or possibly just float)

But they actually get treated as unreachable code and don't reveal anything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-runtime-semanticsmypy doesn't model runtime semantics correctlytopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions