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

Assigning to *args causes a compile error #330

Closed
2 tasks done
johnw-bluemark opened this issue Apr 7, 2023 · 11 comments
Closed
2 tasks done

Assigning to *args causes a compile error #330

johnw-bluemark opened this issue Apr 7, 2023 · 11 comments
Labels

Comments

@johnw-bluemark
Copy link

johnw-bluemark commented Apr 7, 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

3.0.2

Python version

3.8.10, 3.10.11

What happened?

It errors during compile. It should not error during compile

How can we reproduce the bug?

test.py

def test(*args):
    args = []

typecheck.py

from typeguard import install_import_hook
install_import_hook('test')
import test

run typecheck.py

Python 3.8:

Traceback (most recent call last):
  File "typecheck.py", line 3, in <module>
    import test
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "C:\Python38\lib\site-packages\typeguard\_importhook.py", line 96, in exec_module
    super().exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 844, in exec_module
  File "<frozen importlib._bootstrap_external>", line 981, in get_code
  File "C:\Python38\lib\site-packages\typeguard\_importhook.py", line 85, in source_to_code
    return _call_with_frames_removed(
  File "C:\Python38\lib\site-packages\typeguard\_importhook.py", line 42, in _call_with_frames_removed
    return f(*args, **kwargs)
TypeError: expected some sort of slice, but got <_ast.Tuple object at 0x000001FC89BD5370>

Python 3.10:

Traceback (most recent call last):
  File "C:\Users\Johnw\Desktop\temp\typecheck.py", line 3, in <module>
    import test
  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 "C:\Python310\lib\site-packages\typeguard\_importhook.py", line 96, 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 "C:\Python310\lib\site-packages\typeguard\_importhook.py", line 85, in source_to_code
    return _call_with_frames_removed(
  File "C:\Python310\lib\site-packages\typeguard\_importhook.py", line 42, in _call_with_frames_removed
    return f(*args, **kwargs)
TypeError: required field "ctx" missing from Subscript
@agronholm
Copy link
Owner

This works with 4.0.0rc1. I plan to release the final version soon, as long as there are no showstopper issues.

@johnw-bluemark
Copy link
Author

So it does. I'm now testing 4.0.0.rc1, and I've encountered a different error in a large project. I'll make a new bug report when I can narrow down the cause.

@agronholm
Copy link
Owner

Make sure you delete any __pycache__ directories so that the compilation is done again and not loaded from cache.

@johnw-bluemark
Copy link
Author

The 4.0.0.rc1 issue was indeed related to __pycache__.

@agronholm
Copy link
Owner

agronholm commented Apr 7, 2023

Yeah. I'm adding a mechanism to typeguard that varies the file name pattern of the generated typeguard-modified .pyc files based on the version information obtained from importlib.metadata (if available).

EDIT: Done in 1715bd7.

@johnw-bluemark
Copy link
Author

I've found a very similar issue in 4.0.0.rc1

test.py

def test(*args: str):
    pass

Giving any type annotation to *args causes the same error I reported above, but only for Python 3.8. Python 3.10 runs it just fine.

@agronholm
Copy link
Owner

I've added a new test for both positional and keyword variable arguments, and indeed it fails on Python 3.7 and 3.8. Good thing this was caught before the final release.

@basnijholt
Copy link

I also keep running into that issue in https://github.com/python-adaptive/adaptive/.

This works with 4.0.0rc1. I plan to release the final version soon, as long as there are no showstopper issues.

What is your idea of "soon"? 😄

@agronholm
Copy link
Owner

My plan was to release it next week, provided that no other showstopper issues crop up.

@agronholm
Copy link
Owner

So, turns out that on Python 3.8 and earlier, you can't assign ast.Tuple to the slice field of ast.Subscript, you have to wrap it in ast.Index.

@agronholm
Copy link
Owner

The fix is available in v4.0.0rc2.

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

3 participants