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

mypyc: KeyError 'expr' with NamedTuple #16845

Open
asottile-sentry opened this issue Feb 1, 2024 · 1 comment
Open

mypyc: KeyError 'expr' with NamedTuple #16845

asottile-sentry opened this issue Feb 1, 2024 · 1 comment
Labels
bug mypy got something wrong topic-mypyc mypyc bugs topic-named-tuple

Comments

@asottile-sentry
Copy link

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

import ast
from typing import NamedTuple

class C(NamedTuple):
    value: ast.expr

Expected Behavior

not crash

Actual Behavior

$ mypyc t.py
running build_ext
building 't' extension
clang -fno-strict-overflow -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk -I/private/tmp/z/venv/lib/python3.12/site-packages/mypyc/lib-rt -I/private/tmp/z/venv/include -I/opt/homebrew/opt/python@3.12/Frameworks/Python.framework/Versions/3.12/include/python3.12 -c build/__native.c -o build/temp.macosx-13-arm64-cpython-312/build/__native.o -O3 -g1 -Werror -Wno-unused-function -Wno-unused-label -Wno-unreachable-code -Wno-unused-variable -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-but-set-variable -Wno-ignored-optimization-argument -Wno-cpp
clang -bundle -undefined dynamic_lookup -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk build/temp.macosx-13-arm64-cpython-312/build/__native.o -o build/lib.macosx-13-arm64-cpython-312/t.cpython-312-darwin.so
copying build/lib.macosx-13-arm64-cpython-312/t.cpython-312-darwin.so -> 
$ python3 -c 'import t'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "t.py", line 5, in <module>
    value: ast.expr
KeyError: 'expr'

Your Environment

  • Mypy version used: 1.8.0
  • Mypy command-line flags: n/a
  • Mypy configuration options from mypy.ini (and other config files): n/a
  • Python version used: 3.12.1
@asottile-sentry asottile-sentry added the bug mypy got something wrong label Feb 1, 2024
@bzoracler
Copy link
Contributor

One workaround is to use a from import:

from ast import expr
from typing import NamedTuple

class C(NamedTuple):
    value: expr

It appears you don't need this workaround if you define a plain class and not inherit from a typing.NamedTuple.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-mypyc mypyc bugs topic-named-tuple
Projects
None yet
Development

No branches or pull requests

3 participants