mypy icon indicating copy to clipboard operation
mypy copied to clipboard

mypyc: `KeyError 'expr'` with NamedTuple

Open asottile-sentry opened this issue 1 year ago • 1 comments

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/[email protected]/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 avatar Feb 01 '24 10:02 asottile-sentry

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.

bzoracler avatar Feb 02 '24 17:02 bzoracler