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

Unions crash when one of their type parameters is shadowed by the arguments of a function. #394

Closed
2 tasks done
vthemelis opened this issue Sep 2, 2023 · 0 comments
Closed
2 tasks done
Labels

Comments

@vthemelis
Copy link
Contributor

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.1.3

Python version

Python 3.10.10

What happened?

from typeguard import typechecked
from typing import Union

@typechecked
def foo(int: Union[int, None]):
    print(int)

foo(42)
foo("foo")

raises this:

Traceback (most recent call last):
  File "/Users/vas/.pyenv/versions/3.10.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/Users/vas/.pyenv/versions/3.10.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/vas/source/typeguard/example.py", line 11, in <module>
    foo(42)
  File "/Users/vas/source/typeguard/example.py", line 8, in foo
    def foo(int: Union[int, bool]) -> int:
  File "/Users/vas/.pyenv/versions/3.10.10/lib/python3.10/typing.py", line 312, in inner
    return func(*args, **kwds)
  File "/Users/vas/.pyenv/versions/3.10.10/lib/python3.10/typing.py", line 403, in __getitem__
    return self._getitem(self, parameters)
  File "/Users/vas/.pyenv/versions/3.10.10/lib/python3.10/typing.py", line 515, in Union
    parameters = tuple(_type_check(p, msg) for p in parameters)
  File "/Users/vas/.pyenv/versions/3.10.10/lib/python3.10/typing.py", line 515, in <genexpr>
    parameters = tuple(_type_check(p, msg) for p in parameters)
  File "/Users/vas/.pyenv/versions/3.10.10/lib/python3.10/typing.py", line 176, in _type_check
    raise TypeError(f"{msg} Got {arg!r:.100}.")
TypeError: Union[arg, ...]: each arg must be a type. Got 42.

This is related to #337 and #342

How can we reproduce the bug?

As above

@vthemelis vthemelis added the bug label Sep 2, 2023
@vthemelis vthemelis changed the title Unions don't work as expected when one of their type parameters is shadowed by the arguments of a function. Unions crush when one of their type parameters is shadowed by the arguments of a function. Sep 2, 2023
@agronholm agronholm changed the title Unions crush when one of their type parameters is shadowed by the arguments of a function. Unions crash when one of their type parameters is shadowed by the arguments of a function. Sep 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant