← Back to Pull requests

Add conformance tests to allow soft keywords in as namedtuple fields and disallow hard keywords #2399

Open
quag opened this pull request with 316 lines diff across 15 files
View on GitHub
quag quag commented on Dec 04, 2026 06:08 UTC
Author

The zuban v0.11.1 type checker had a bug where it disallowed soft keywords ("match", "case", and "type") to be field names of namedtuples. namedtuple fields must be valid python identifiers that do not start with a "_". Soft keywords are valid python identifiers. This bug will be fixed in the next version of Zuban. This commit adds conformance tests for both allowing soft keywords as namedtuple fields and disallowing hard keywords.

The other type checkers all correctly allowed soft keywords and disallowed hard keywords.

https://docs.python.org/3/reference/lexical_analysis.html#keywords
https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords
https://docs.python.org/3/library/keyword.html

Diff: 316 lines (+304 / -12) across 15 files Reviewer: carljm Review duration: 1130m
All Vouch review confidence checks passed
Residual risk 0.05 = Change Risk (0.58) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.58 · 316 lines diff across 15 files
0.58
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.93 · 4 substantive comment(s)
0.93
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.345
Real PR Conversation Timeline
6 items
python-cla-bot[bot] python-cla-bot[bot] commented on Dec 04, 2026 06:08 UTC
Bot

All commit authors signed the Contributor License Agreement.

![CLA signed](https://cla.python.org/)<!-- CLA BOT SIGNING COMMENT DO NOT EDIT -->

carljm carljm commented on code on Dec 04, 2026 19:22 UTC
Code Review
conformance/tests/namedtuples_keywords_hard.py:1
@@ -0,0 +1,7 @@
+"""
+Test namedtuple cannot have hard keywords as field names as they are not valid python identifiers.
+"""
+
+from collections import namedtuple
+
+HardKeywordsType = namedtuple("HardKeywordsType", ["False", "None", "True", "and", "as", "assert", "async", "await", "break", "class", "continue", "def", "del", "elif", "else", "except", "finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass", "raise", "return", "try", "while", "with", "yield"])  # E

This assertion will pass if a type checker forbids any single one of these keywords, even if it allows all the rest.

There's actually an example of this in the results in this PR. Pyright passes, but it misses and (column 77).

If we want to be exhaustive, I think we'd need a separate statement for each keyword.

But the spec explicitly says that type checkers may replicate the runtime validation of field names, so I think these would all need to be # E? anyway. And # E? assertions in the conformance suite are basically useless, other than as documentation that "yes, we considered this, and the spec says we shouldn't care."

Given that we already test def as a representative hard keyword, both with and without rename, I think we should just drop this file entirely and focus on the soft keywords for this PR.

carljm carljm commented on code on Dec 04, 2026 19:36 UTC
Code Review
conformance/results/zuban/namedtuples_keywords_soft.toml:1
@@ -0,0 +1,10 @@
+conformant = "Fail"

This is what's failing CI. "Fail" is valid for conformance_automated, but it's not a valid value for conformant.

Since Zuban accepts the class syntax but rejects match, case, and type in collections.namedtuple, I think we should use conformant = "Partial" with a notes entry explaining the specific gap.

carljm carljm commented on code on Dec 04, 2026 19:36 UTC
Code Review
conformance/tests/namedtuples_keywords_soft.py:1
@@ -0,0 +1,17 @@
+"""
+Test NamedTuple and namedtuple can have soft keywords as field names as they are valid Python identifiers.
+"""
+
+from collections import namedtuple
+from typing import NamedTuple
+
+SoftKeywordsType = namedtuple(
+    "SoftKeywordsType", ["match", "case", "type", "lazy"]
+)  # OK

Could we also check that we can actually use these field names? A checker that silently renames a field would pass this test. The original Zuban issue shows errors both when passing type= to the constructor and when accessing .type.

row = SoftKeywordsType(match=1, case=2, type=3, lazy=4)
value = row.type

I think it'd also be worth covering the functional form of typing.NamedTuple, since that's the other form where the checker has to interpret strings as field names.

T = NamedTuple("T", [("type", int)])
assert_type(T(type=1).type, int)

Maybe these cases could just go in namedtuples_define_functional.py alongside the existing field name tests?

carljm carljm requested changes on Dec 04, 2026 19:43 UTC
Changes Requested Reviewer

Requested changes.

Reviewers
Assignees
quag quag
Labels
risk: low confidence: 90.8% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 58.0% (MEDIUM) · Higher = larger blast radius
58.0%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 90.8% · Lower = rushed or shallow review
90.8%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 5.3% · Safe to merge
5.3%
Repository