Skip to content

Cannot check types that are shadowed by argument names #337

@jolaf

Description

@jolaf

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.0.0rc2 master

Python version

3.10.6

What happened?

Traceback (most recent call last):
  File "/media/sf_d/WWPass/git/python-test/test.py", line 4, in <module>
    import_module('A')
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "/home/jolaf/git/typeguard/src/typeguard/_importhook.py", line 122, in exec_module
    super().exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/media/sf_d/WWPass/git/python-test/A.py", line 6, in <module>
    f(bool)
  File "/media/sf_d/WWPass/git/python-test/A.py", line 1, in f
    def f(type: type | None = None) -> None:
  File "/home/jolaf/git/typeguard/src/typeguard/_functions.py", line 135, in check_argument_types
    check_type_internal(value, annotation, memo)
  File "/home/jolaf/git/typeguard/src/typeguard/_checkers.py", line 756, in check_type_internal
    checker(value, origin_type, args, memo)
  File "/home/jolaf/git/typeguard/src/typeguard/_checkers.py", line 432, in check_uniontype
    raise TypeCheckError(f"did not match any element in the union:\n{formatted_errors}")
typeguard.TypeCheckError: argument "type" (class bool) did not match any element in the union:
  bool: is not an instance of bool
  NoneType: is not an instance of NoneType

How can we reproduce the bug?

test.py:

from importlib import import_module
from typeguard import install_import_hook
with install_import_hook(('A',)):
    import_module('A')

A.py:

def f(type: type | None = None) -> None:
    pass

f(bool)

Run:

$ python3 test.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions