Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Featire request: Allow unpacking of type variable bound to typed dict #5069

Closed
NeilGirdhar opened this issue May 3, 2023 · 3 comments
Closed
Labels
enhancement request New feature or request

Comments

@NeilGirdhar
Copy link

from typing import Generic, Unpack

from typing_extensions import TypedDict, TypeVar


Extra = TypeVar('Extra', bound=TypedDict)


class C(Generic[Extra]):
    def f(self, **kwargs: Unpack[Extra]) -> None:  # error: Expected TypedDict type argument for Unpack (reportGeneralTypeIssues)
        pass
@NeilGirdhar NeilGirdhar added the enhancement request New feature or request label May 3, 2023
@NeilGirdhar
Copy link
Author

NeilGirdhar commented May 3, 2023

Ultimately, I'd like something like this to work if possible?

from typing import Generic, Unpack

from typing_extensions import TypedDict, TypeVar, override


class EmptyDict(TypedDict, total=True):
    pass


Extra = TypeVar('Extra', bound=TypedDict, default=EmptyDict)


class C(Generic[Extra]):
    def f(self, **kwargs: Unpack[Extra]) -> None:  # error: Expected TypedDict type argument for Unpack (reportGeneralTypeIssues)
        pass


class D(C):
    @override
    def f(self) -> None:  # error: Method "f" overrides class "C" in an incompatible manner
        pass


class HasName(TypedDict, total=True):
    name: str


class E(C[HasName]):
    @override
    def f(self, name: str) -> None:
        print(name)



D().f()
E().f("a")

@NeilGirdhar NeilGirdhar changed the title Cannot unpack type variable bound to typed dict Featire request: Allow unpacking of type variable bound to typed dict May 3, 2023
@erictraut
Copy link
Collaborator

erictraut commented May 3, 2023

This would require an extension to the Python type system, so it is best discussed in the python/typing forum. It would probably require a new PEP or an extension to PEP 692.

There are two extensions that would be required:

  1. It would need to be clarified that the TypedDict special form can be used as a type (e.g. as a variable type annotation or a TypeVar bound). Currently, this is not clear. Mypy specifically generates an error when it is used in this manner. Pyright doesn't currently, but it probably should.
  2. The functionality in PEP 692 would need to be extended to accommodate an Unpack[T] where T is specifically bound to a TypedDict (assuming 1 is allowed).

I'm going to close this enhancement request for now. If you want to get feedback from the broader typing community about these changes/extensions, please open an issue in python/typing.

@NeilGirdhar
Copy link
Author

@erictraut Okay, many thanks for your thorough explanation as always!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants