Skip to content

Treat LiteralString TypedDict keys like str keys - #11981

Open
Henry Su (hsusul) wants to merge 4 commits into
microsoft:mainfrom
hsusul:fix/typeddict-literalstring-extra-items
Open

Henry Su (hsusul) wants to merge 4 commits into
microsoft:mainfrom
hsusul:fix/typeddict-literalstring-extra-items

Conversation

@hsusul

Copy link
Copy Markdown
Contributor

Why

Indexing a TypedDict with a LiteralString key reports "LiteralString" is not a string literal, while the same access with a plain str key is accepted. LiteralString is the narrower type, so code that is careful about its key types gets an error that looser code doesn't. Fixes #11150.

from typing import TypedDict, LiteralString

class TD(TypedDict, extra_items=str): ...

def test(x: TD, k1: LiteralString, k2: str):
    x[k1]  # error before this change
    x[k2]  # no error

Root cause

getTypeOfIndexedTypedDict only enters its string-key branch when ClassType.isBuiltIn(subtype, 'str') holds. LiteralString is a separate built-in class, so it skipped that branch and fell through to the typeNotStringLiteral diagnostic. A plain str takes the branch and evaluates to Unknown because its value isn't known.

Change

The branch now also accepts LiteralString. A LiteralString key has no literal value, so it takes the same path as str. Get, set and del evaluate to Unknown with no diagnostic. This applies to every TypedDict, not only ones with extra_items. Literal keys are unaffected.

A LiteralString key can name a known item at runtime ("KNOWN".lower()). That rules out resolving it to the extra_items type alone, so treating it exactly like str is the sound choice.

Testing

  • New sample typedDictClosed12.py, registered as TypedDictClosed12. It covers get, set and del with a LiteralString key on an extra_items TypedDict, a plain TypedDict and a closed TypedDict, with a str key 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.test passes (548 tests).
  • build/perfCompare.py --metric cpu --num-runs 30 over 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.

@heejaechang

Copy link
Copy Markdown
Collaborator

🟢 Pyright CLI QA — a39d4391 — verdict: green

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

No new behavior difference was detected.

@bschnurr

Copy link
Copy Markdown
Member

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 item

On 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 LiteralString path keep these assignment/deletion checks (treating LiteralString like str for writes), and add these as error cases in the sample?

Also, the branch now conflicts with main on typedDictClosed12.py: main added a sample with the same name. Renaming this PR's sample (e.g. typedDictLiteralString1.py) resolves it.

🤖 AI-assisted review posted on behalf of Bill Schnurr (@bschnurr).

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

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>
@bschnurr

Copy link
Copy Markdown
Member

Follow-up in dac4775: nonliteral LiteralString keys are accepted for reads, but writes and deletes now receive the existing nonliteral-key diagnostic. Known literal keys remain precise. This prevents mutations of required or read-only fields and stale narrowing.

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.

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.

TypedDict w/ extra items accepts str key but not LiteralString

4 participants