Add conformance tests to allow soft keywords in as namedtuple fields and disallow hard keywords #2399
All commit authors signed the Contributor License Agreement.
<!-- CLA BOT SIGNING COMMENT DO NOT EDIT -->
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.
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.
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?
Requested changes.
carljm
quag
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