Skip to content

support conditional fields on TypedDict and NamedTuple#2263

Open
carljm wants to merge 2 commits intopython:mainfrom
carljm:condfield
Open

support conditional fields on TypedDict and NamedTuple#2263
carljm wants to merge 2 commits intopython:mainfrom
carljm:condfield

Conversation

@carljm
Copy link
Copy Markdown
Member

@carljm carljm commented Apr 17, 2026

We reached consensus on this a while back in this Discuss thread, but I never got around to actually making the PR.

Seems like in the meantime we accidentally implemented the strict version of the check for TypedDict in ty, but astral-sh/ruff#24702 fixes that. Happy to wait a bit to land this if other type checkers want to have conforming implementations ready, too.

Comment thread docs/spec/typeddict.rst Outdated
@carljm carljm requested a review from davidhalter April 18, 2026 00:51
Copy link
Copy Markdown
Collaborator

@davidhalter davidhalter left a comment

Choose a reason for hiding this comment

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

Is it intentional that this is not specified for Protocol/Enums/Dataclasses? I would probably prefer this as a consistent change for all of these.

However I'm also good with this PR as a first step.

@davidhalter
Copy link
Copy Markdown
Collaborator

Or maybe the question should be: Why is this not defined in a more general way in the spec? Shouldn't this be what we want in all class-like scopes? Conformance tests should probably test this for different scenarios but I personally do not see a reason to add this specifically only to TypedDicts and NamedTuples.

This PR is IMO still better then the status quo.

@JelleZijlstra
Copy link
Copy Markdown
Member

What's special about TypedDict and NamedTuple is that they're very restricted (they don't support class variables, methods, etc.). So it makes sense to enumerate only those things that are explicitly allowed.

@AlexWaygood
Copy link
Copy Markdown
Member

they don't support class variables, methods, etc.

(NamedTuples do support methods, unlike TypedDicts)

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

Labels

topic: conformance tests Issues with the conformance test suite topic: typing spec For improving the typing spec

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants