Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError at ast.parse #333

Closed
2 tasks done
jolaf opened this issue Apr 9, 2023 · 11 comments
Closed
2 tasks done

SyntaxError at ast.parse #333

jolaf opened this issue Apr 9, 2023 · 11 comments
Labels

Comments

@jolaf
Copy link
Contributor

jolaf commented Apr 9, 2023

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

Python version

3.10.6

What happened?

Traceback (most recent call last):
  File "/tmp/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/.local/lib/python3.10/site-packages/typeguard/_importhook.py", line 106, 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 83, in source_to_code
    tree = TypeguardTransformer().visit(tree)
  File "/usr/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 514, in visit_Module
    self.generic_visit(node)
  File "/usr/lib/python3.10/ast.py", line 486, in generic_visit
    value = self.visit(value)
  File "/usr/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 559, in visit_ClassDef
    self.generic_visit(node)
  File "/usr/lib/python3.10/ast.py", line 486, in generic_visit
    value = self.visit(value)
  File "/usr/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 610, in visit_FunctionDef
    annotation = self._convert_annotation(copy(arg.annotation))
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 499, in _convert_annotation
    new_annotation = cast(expr, AnnotationTransformer(self).visit(annotation))
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 336, in visit
    new_node = super().visit(node)
  File "/usr/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 370, in visit_Subscript
    self.generic_visit(node)
  File "/usr/lib/python3.10/ast.py", line 495, in generic_visit
    new_node = self.visit(old_value)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 336, in visit
    new_node = super().visit(node)
  File "/usr/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/usr/lib/python3.10/ast.py", line 486, in generic_visit
    value = self.visit(value)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 336, in visit
    new_node = super().visit(node)
  File "/usr/lib/python3.10/ast.py", line 410, in visit
    return visitor(node)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_transformer.py", line 395, 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:

def f(a: Literal['%']):
    pass

Run:

$ python3 test.py
@jolaf jolaf added the bug label Apr 9, 2023
@agronholm
Copy link
Owner

Ok, do you have any suggestions as to how I might reproduce the bug? The stack trace gives me no clue as to what is going wrong. If you could reduce the problematic module to a minimum viable case, that would help.

@agronholm
Copy link
Owner

The problem occurs during code compilation so just remove classes and functions until you locate the piece that causes the problem. The code in the module doesn't even have to be workable for this, just syntactically correct.

@jolaf
Copy link
Contributor Author

jolaf commented Apr 9, 2023

Oh, I've finally nailed it! Updated the description.

@agronholm
Copy link
Owner

Oof, indeed typing.Literal would trigger this since typeguard tries to aggressively resolve any forward references, and now it treats the subscript of Literal as such.

@agronholm
Copy link
Owner

I now have a test that reproduces this.

@jolaf
Copy link
Contributor Author

jolaf commented Apr 9, 2023

Thanks! Hopefully, the fix would make it to 4.0.0 release.

@agronholm
Copy link
Owner

Of course.

@agronholm
Copy link
Owner

Could you test against latest master and report whether typeguard works with your code base now?

@jolaf
Copy link
Contributor Author

jolaf commented Apr 9, 2023

Yep, it works now. Thanks!

@agronholm
Copy link
Owner

If nothing else comes up, I'll release v4.0.0 final this week.

@jolaf
Copy link
Contributor Author

jolaf commented Apr 10, 2023

Amazing, thanks!

@jolaf jolaf mentioned this issue Sep 10, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants