Repository navigation
Detect the keyword and in namedtuple field names - #11930
crypt0lith wants to merge 2 commits into
Conversation
Tokenizer.isPythonKeyword checks the keyword lookup with '!keyword', and KeywordType.And is 0, so a namedtuple field named 'and' isn't reported as a keyword. Replace the falsy check with a strict equality check.
🟢 Pyright CLI QA —
|
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
Local review: The updated fixture reports |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
A known-true rename flag still triggers the new keyword-field error:
from collections import namedtuple
from typing import Final
enabled: Final = True
Result = namedtuple('Result', ['and'], rename=enabled)
print(Result._fields) # ('_0',)Base reports no error here; this head reports Field names cannot be a keyword on 'and'. Both reveal enabled as Literal[True], and CPython successfully renames the field to _0. Base had a separate field-synthesis problem, so this is specifically about the new false diagnostic.
Could we recognize a statically known true flag and test it alongside a known-false control?
Separately, and not a blocker for this regression: head diagnoses namedtuple(typename='Ordered', field_names=['and']) but not namedtuple(field_names=['and'], typename='Reordered'). CPython raises ValueError for both. That's a pre-existing, broader argument-binding gap that seems worth tracking separately.
This isn't a regression caused by #11930; it already applied to all keywords, base just didn't treat from collections import namedtuple
from typing import Final
enabled: Final = True
Result = namedtuple('Result', ['class'], rename=enabled)pyright 1.1.414: The reason is because pyright/packages/pyright-internal/src/analyzer/namedTuples.ts Lines 73 to 82 in 7cef37b The parsed node is the name |
|
Rich Chiodo (@rchiodo) The |
Summary
Since e63f229, a namedtuple field named
andisn't reported as a keyword.Root cause
Tokenizer.isPythonKeywordchecks the keyword lookup with!keywordinstead ofkeyword === undefined.KeywordType.Andis 0, so it's falsy.Fix
Replace the falsy check with a strict equality check.
Test
Add a field named
andalongside the existing keyword field-name cases innamedTuple9.py.