Skip to content

Detect the keyword and in namedtuple field names - #11930

Open
crypt0lith wants to merge 2 commits into
microsoft:mainfrom
crypt0lith:fix/namedtuple-field-is-keyword-falsy-check
Open

crypt0lith wants to merge 2 commits into
microsoft:mainfrom
crypt0lith:fix/namedtuple-field-is-keyword-falsy-check

Conversation

@crypt0lith

Copy link
Copy Markdown
Contributor

Summary

Since e63f229, a namedtuple field named and isn't reported as a keyword.

Root cause

Tokenizer.isPythonKeyword checks the keyword lookup with !keyword instead of keyword === undefined. KeywordType.And is 0, so it's falsy.

Fix

Replace the falsy check with a strict equality check.

Test

Add a field named and alongside the existing keyword field-name cases in namedTuple9.py.

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.
@heejaechang

Copy link
Copy Markdown
Collaborator

🟢 Pyright CLI QA — 9fc00248 — verdict: green

Automated CLI checks compared this change with its base revision.
5 focused check(s) completed.

No new behavior difference was detected.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@bschnurr

Copy link
Copy Markdown
Member

Local review: 7cef37b8 integrated with ff00b031.

The updated fixture reports Field names cannot be a keyword for and on this head but not on main. The other three keyword diagnostics are unchanged. All 17 focused NamedTuple tests passed.

@rchiodo Rich Chiodo (rchiodo) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@crypt0lith

Copy link
Copy Markdown
Contributor Author

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.

This isn't a regression caused by #11930; it already applied to all keywords, base just didn't treat and as a keyword:

from collections import namedtuple
from typing import Final

enabled: Final = True
Result = namedtuple('Result', ['class'], rename=enabled)

pyright 1.1.414:

:5:32 - error: Field names cannot be a keyword (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations

The reason is because createNamedTupleType calls evaluateStaticBoolExpression directly on the expression:

if (renameArg?.valueExpression) {
const renameValue = evaluateStaticBoolExpression(
renameArg.valueExpression,
fileInfo.executionEnvironment,
fileInfo.definedConstants
);
if (renameValue === true) {
allowRename = true;
}
}

The parsed node is the name enabled not the literal constant True, so evaluateStaticBoolExpression returns undefined.

@crypt0lith

Copy link
Copy Markdown
Contributor Author

Rich Chiodo (@rchiodo) The enabled: Final = True finding is addressed in #12057

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants