agronholm / typeguard

Run-time type checker for Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SyntaxError on Literal

jolaf opened this issue · comments

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.4

Python version

3.10.12

What happened?

Traceback (most recent call last):
  File "test.py", line 7, 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/.local/lib/python3.10/site-packages/typeguard/_importhook.py", line 98, in exec_module
    super().exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 879, in exec_module
  File "<frozen importlib._bootstrap_external>", line 1017, in get_code
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_importhook.py", line 75, in source_to_code
    tree = TypeguardTransformer().visit(tree)
  File "/usr/lib/python3.10/ast.py", line 418, in visit
    return visitor(node)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 623, in visit_Module
    self.generic_visit(node)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 513, in generic_visit
    node = super().generic_visit(node)
  File "/usr/lib/python3.10/ast.py", line 494, in generic_visit
    value = self.visit(value)
  File "/usr/lib/python3.10/ast.py", line 418, in visit
    return visitor(node)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 746, in visit_FunctionDef
    annotation = self._convert_annotation(deepcopy(arg.annotation))
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 607, in _convert_annotation
    new_annotation = cast(expr, AnnotationTransformer(self).visit(annotation))
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 352, in visit
    new_node = super().visit(node)
  File "/usr/lib/python3.10/ast.py", line 418, in visit
    return visitor(node)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 432, in visit_Subscript
    [self.visit(item) for item in slice_value.elts],
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 432, in <listcomp>
    [self.visit(item) for item in slice_value.elts],
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 352, in visit
    new_node = super().visit(node)
  File "/usr/lib/python3.10/ast.py", line 418, in visit
    return visitor(node)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 489, in visit_Constant
    expression = ast.parse(node.value, mode="eval")
  File "/usr/lib/python3.10/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    %
    ^
SyntaxError: invalid syntax

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 Literal
def f(a: Literal['%', '{', '$'] = '%') -> None:
    print("OK")
f()
$ python3 A.py
OK

$ python3 test.py
<crash>

The problem didn't appear on v4.1.3, so probably some recent fix introduced it, or it may be a regression.

sigh I just can't catch a break.

Sorry. :(

This looks very similar to #333, may be a regression.

Looks like 4c04b12 caused this.