Repository navigation
Treat LiteralString TypedDict keys like str keys - #11981
Henry Su (hsusul) wants to merge 4 commits into
Conversation
🟢 Pyright CLI QA —
|
|
Thanks! The new non-literal-key path skips the mutation checks that literal keys still get. On this PR, these produce no diagnostics: from typing import LiteralString, TypedDict
from typing_extensions import ReadOnly
class Closed(TypedDict, closed=True):
item: int
class RO(TypedDict):
item: ReadOnly[int]
def f(closed: Closed, ro: RO, key: LiteralString) -> None:
closed[key] = 1 # may add an undeclared key to a closed TypedDict
ro[key] = 1 # may write a ReadOnly item
del ro[key] # may delete a ReadOnly required itemOn main these are reported, and the equivalent literal-key forms are still rejected on this PR. Per the typing spec, read-only items must not be mutated and closed TypedDicts can't gain undeclared keys. Could the Also, the branch now conflicts with 🤖 AI-assisted review posted on behalf of Bill Schnurr (@bschnurr). |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
This may overlap with Bill's mutation-checking point, but allowing LiteralString writes exposes a couple more concrete cases. Both weaknesses already exist for plain str writes on base. Base rejects the LiteralString write in each example below but doesn't flag the later consumer; head (a39d439) accepts both.
A dynamic key can put a str into an int field:
from typing import LiteralString, TypedDict
class Record(TypedDict):
count: int
def set_key(record: Record, key: LiteralString) -> None:
record[key] = 'not an integer'
record: Record = {'count': 1}
set_key(record, 'count')
print(record['count'] + 1)CPython raises TypeError at the addition when key is 'count'.
Even a value compatible with every declared and extra value type can leave field narrowing stale:
from typing import LiteralString, reveal_type
from typing_extensions import TypedDict
class Values(TypedDict, extra_items=int | str):
value: int | str
def f(d: Values, key: LiteralString) -> str:
if isinstance(d['value'], str):
d[key] = 1
reveal_type(d['value']) # head: str; runtime value can be int
return d['value'].upper()
return str(d['value'])
f({'value': 'ok'}, 'value')Head still reveals str, but CPython 3.14.3 raises AttributeError at .upper() because the value is now an int.
Could we reject unsafe dynamic mutations, or, when allowing them, check the value against possible targets and invalidate affected narrowing? Checking the value alone won't fix the second case. I don't have a tested fix.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Classification: B (Pyright limitation). Nonliteral keys can target required, read-only, or narrowed fields, so retain the existing write and delete diagnostics until those mutations can be checked soundly. LiteralString reads and known literal-key precision are unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Follow-up in dac4775: nonliteral The PR description still says nonliteral writes and deletes are accepted; please update it. The benchmark cited there predates this follow-up. Validation: core build, 101 targeted TypedDict tests, ESLint, and Prettier passed. CI is pending. |
Why
Indexing a TypedDict with a
LiteralStringkey reports"LiteralString" is not a string literal, while the same access with a plainstrkey is accepted.LiteralStringis the narrower type, so code that is careful about its key types gets an error that looser code doesn't. Fixes #11150.Root cause
getTypeOfIndexedTypedDictonly enters its string-key branch whenClassType.isBuiltIn(subtype, 'str')holds.LiteralStringis a separate built-in class, so it skipped that branch and fell through to thetypeNotStringLiteraldiagnostic. A plainstrtakes the branch and evaluates toUnknownbecause its value isn't known.Change
The branch now also accepts
LiteralString. ALiteralStringkey has no literal value, so it takes the same path asstr. Get, set and del evaluate toUnknownwith no diagnostic. This applies to every TypedDict, not only ones withextra_items. Literal keys are unaffected.A
LiteralStringkey can name a known item at runtime ("KNOWN".lower()). That rules out resolving it to theextra_itemstype alone, so treating it exactly likestris the sound choice.Testing
typedDictClosed12.py, registered asTypedDictClosed12. It covers get, set and del with aLiteralStringkey on anextra_itemsTypedDict, a plain TypedDict and a closed TypedDict, with astrkey and a literal key as controls. It fails on main (9 errors instead of 1) and passes with the change.npx jest typeEvaluator5.test typeEvaluator7.test typeEvaluator2.test checker.testpasses (548 tests).build/perfCompare.py --metric cpu --num-runs 30over a 579-file corpus shows no measurable change. The paired median delta is −105.8 ms ± 216.3 ms (−1.23%), and the interval spans zero.