typeguard icon indicating copy to clipboard operation
typeguard copied to clipboard

Problem with unpacking tuples

Open jolaf opened this issue 1 year ago • 0 comments

Things to check first

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

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

Typeguard version

4.3.0

Python version

3.12.3

What happened?

Traceback (most recent call last):
  File "/.../test.py", line 3, in <module>
    import A
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "/home/.../.local/lib/python3.12/site-packages/typeguard/_importhook.py", line 98, in exec_module
    super().exec_module(module)
  File "/.../A.py", line 9, in <module>
    A().f()
  File "/.../A.py", line 6, in f
    (x, self.y) = ('OK', '!')
  File "/home/.../.local/lib/python3.12/site-packages/typeguard/_functions.py", line 251, in check_variable_assignment
    check_type_internal(value, annotation, memo)
  File "/home/.../.local/lib/python3.12/site-packages/typeguard/_checkers.py", line 866, in check_type_internal
    raise TypeCheckError(f"is not an instance of {qualified_name(origin_type)}")
typeguard.TypeCheckError: value assigned to x (tuple) is not an instance of str

How can we reproduce the bug?

test.py:

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

A.py:

class A:
    y: str

    def f(self) -> None:
        x: str
        (x, self.y) = ('OK', '!')
        print(x + self.y)

A().f()
$ python3 A.py
OK!

$ python3 test.py
<crash>

$ mypy *.py
Success: no issues found in 2 source files

The problem only occurs when x is a local variable, and y is an instance variable.

jolaf avatar Sep 11 '24 23:09 jolaf