Skip to content

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

Closed
@vthemelis

Description

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

Activity

changed the title `Union`s don't work as expected when one of their type parameters is shadowed by the arguments of a function. `Union`s crush when one of their type parameters is shadowed by the arguments of a function. on Sep 2, 2023
changed the title `Union`s crush when one of their type parameters is shadowed by the arguments of a function. `Union`s crash when one of their type parameters is shadowed by the arguments of a function. on Sep 3, 2023
added a commit that references this issue on Sep 21, 2024
4d1768c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      `Union`s crash when one of their type parameters is shadowed by the arguments of a function. · Issue #394 · agronholm/typeguard