agronholm / typeguard

Run-time type checker for Python
Other
1.52k stars 114 forks source link

Unexpected PEP 604 union behaviour when one of the types is shadowed #395

Closed vthemelis closed 1 year ago

vthemelis commented 1 year ago

Things to check first

Typeguard version

master - 041b0e3fc1650ece19d9319167922e769649984c

Python version

Python 3.10.12

What happened?

When one of the arguments of the types in the union is shadowed, it should effectively be handled as Any but is currently effectively handled as an uninhabited type.

How can we reproduce the bug?

def test_function_with_union_where_one_of_the_types_is_shadowed():
    @typechecked
    def foo(int: int | None) -> int | None:
        return int

    # All the following should pass because the typechecker cannot 'see'
    # int-type as it's shadowed from the int-argument.
    # However, the only test that passes at the moment is `foo(None)`.
    assert foo(1) == 1
    assert foo(None) is None
    assert foo("foo") == "foo"