Skip to content

Conversation

@erikkemperman
Copy link
Contributor

@erikkemperman erikkemperman commented Jun 4, 2025

Fixes #16975
Supersedes #16977

Allows TypeVarTuples as type argument for subclasses of generic TypedDicts.

Since (1) TypedDict became generic and (2) TypeVarTuple was introduced, mypy now supports creating generic typed dictionaries with variadic type arguments. However, things went wrong when deriving a subclass from such a generic typeddict.

Full disclosure: I submitted an earlier PR for this issue, but it's been a while. Earlier I wasn't 100% sure about one minor aspect of the changes I made, but in the mean time the context has changed to the point that that aspect is no longer relevant.

For tests, I started with a copy of an existing one from test-data/unit/check-typevar-tuple.test, and added two subclasses: one which just passes a TypeVarTuple generic type parameter up to the base class, and another which passes a TypeVarTuple and adds a regular TypeVar argument.

I did this all of this twice: once using the Unpack[Ts] syntax and once using the *Ts syntax.

@github-actions

This comment has been minimized.

@erikkemperman
Copy link
Contributor Author

erikkemperman commented Jun 4, 2025

Apologies, I removed the *Ts syntax unit test for now, obviously that doesn't work for older Python versions... Should perhaps live in check-python311.test? I'm still not entirely clear on the logic behind which test goes where, to be honest.

@github-actions

This comment has been minimized.

@sterliakov
Copy link
Collaborator

Should perhaps live in check-python311.test

Yes, every test that depends on python version higher than minimal supported version (3.9 now) should live in check-python*.test files (matching the minimal version where such syntax is supported)

@github-actions

This comment has been minimized.

@erikkemperman
Copy link
Contributor Author

@sterliakov Thanks for taking a look, and for the suggestion. I've moved the tests using *Ts syntax into check-python311.test.

@erikkemperman
Copy link
Contributor Author

@sterliakov Sorry to ping you again, but perhaps you could review this PR or maybe suggest a member who could? For what it's worth, the code change is pretty minimal and the test coverage should be sufficient, IMHO. Thanks in advance!

Copy link
Collaborator

@sterliakov sterliakov left a comment

Choose a reason for hiding this comment

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

LG, thank you! The test suite looks solid.

You don't have to deal with that here, so just a quick question if you have time: do equivalent tests pass with NamedTuple? They have a lot in common, and many problems affecting TypedDict also affect NamedTuple of similar structure.

Let's see if @hauntsaninja has time to review and merge this, I don't have write perms here:)

@erikkemperman
Copy link
Contributor Author

Thanks @sterliakov!

About the NamedTuple analogy, there is a similar test for it being Generic with TypeVarTuple argument here but only directly, not propagated via inheritance. Which makes sense, I think, as NamedTuples don't behave the same way as TypedDicts do when subclassed (which isn't actually what happens, but you probably get what I mean):

class MyDict(TypedDict):
    foo: int

class MySubDict(MyDict):
    bar: int


msd = MySubDict(foo=1, bar=2)  # Fine



class MyTuple(NamedTuple):
    foo: int

class MySubTuple(MyTuple):
    bar: int


mst = MySubTuple(foo=1, bar=2)  # Error: unexpected keyword "bar"

I sometimes think it would be nice to be able to extend NamedTuples this way, but think I understand why it doesn't currently work that way.

@erikkemperman erikkemperman force-pushed the typeddict_typevar_tuple_paramspec branch from 2632de8 to 94ed01f Compare October 24, 2025 13:47
@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@erikkemperman
Copy link
Contributor Author

@hauntsaninja
I hope you don't mind my pinging you directly; your name was suggested by @sterliakov above. If you have a little time, I would greatly appreciate if you could take a look at this!

I understand that (apparently) this isn't an issue that many folks run into, but I think you'll agree that it actually is a bug that may as well be fixed. I've rebased the branch to latest master just now.

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.

Allow TypeVarTuple as type argument for subclasses of generic TypedDict

2 participants