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: from __future__ imports must occur at the beginning of the file" after comments #385

Closed
2 tasks done
jolaf opened this issue Aug 25, 2023 · 1 comment · Fixed by lovasoa/marshmallow_dataclass#273
Labels

Comments

@jolaf
Copy link
Contributor

jolaf commented Aug 25, 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.1.2

Python version

3.11.2

What happened?

Traceback (most recent call last):
  File "C:\Temp\test.py", line 4, in <module>
    import_module('A')
  File "C:\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "C:\Python311\Lib\site-packages\typeguard\_importhook.py", line 98, in exec_module
    super().exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 936, in exec_module
  File "<frozen importlib._bootstrap_external>", line 1074, in get_code
  File "C:\Python311\Lib\site-packages\typeguard\_importhook.py", line 87, in source_to_code
    return _call_with_frames_removed(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\typeguard\_importhook.py", line 47, in _call_with_frames_removed
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "C:\Temp\A.py", line 4
    from __future__ import annotations
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: from __future__ imports must occur at the beginning of the file

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:

#
# Comment
#
from __future__ import annotations

def f() -> int:
    return 5

print("OK")

Run:

$ python3 A.py
OK

$ python3 test.py
<crash>
@jolaf
Copy link
Contributor Author

jolaf commented Aug 29, 2023

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant