Skip to content

Problem with shadowed Optional #342

@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/android/app/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/android/app/A.py", line 15, in <module>
    f("", type = bool)
  File "/media/sf_d/WWPass/git/android/app/A.py", line 3, in f
    def f(
  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 759, in check_type_internal
    if not isinstance(value, origin_type):
  File "/usr/lib/python3.10/typing.py", line 396, in __instancecheck__
    raise TypeError(f"{self} cannot be used with isinstance()")
TypeError: typing.Optional cannot be used with isinstance()

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:

from typing import Any, Callable, Optional

def f(
        name: str,
        default: Any = None,
        type: Optional[type] = None,
        help: Optional[str] = None,
        metavar: Optional[str] = None,
        multiple: bool = False,
        group: Optional[str] = None,
        callback: Optional[Callable[[Any], None]] = None,
    ) -> None:
    pass

f("", type = 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