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

[WIP] Adding support for generic TypedDicts #1390

Closed
wants to merge 1 commit into from
Closed

[WIP] Adding support for generic TypedDicts #1390

wants to merge 1 commit into from

Conversation

sransara
Copy link
Contributor

@sransara sransara commented Jan 19, 2021

Hi, what do you think about supporting generic TypedDicts?

In this pull request I tried to add this functionality hopefully without mangling too many other things.
The basis of the implementation is by enhancing the getTypedDictMembersForClass and getTypedDictMembersForClassRecursive.

There is also a mypy issue on adding support for generic TypedDicts.

In the following code you can see this PR in action, but there are still a few actions TODO items. I just wanted to get some feedback before moving forward.

from typing import TypedDict, TypeVar, Generic, List
T = TypeVar("T")

class TD(Generic[T], TypedDict):
    f1: List[T]
    
def test_td(aa: TD[T], bb: TD[T]):
    return aa["f1"], bb["f1"]

td1 = test_td({"f1": ["foo"]}, {"f1": ["bar"]})
reveal_type(td1) # info: Type of "td1" is "tuple[List[str], List[str]]"

test_td({"f1": ["foo"]}, {"f1": [1]}) # error

class TD2(TD[T]):
    f2: T

td21: TD2[str] = {
    'f1':["far"],
    'f2':"boo"
}
reveal_type(td21["f1"]) # info: Type of "td21["f1"]" is "List[str]"

td22 = TD2(
    f1=["far"],
    f2="boo"
)
reveal_type(td22) # info: Type of "td22" is "TD2[str]"

td23: TD2[str] = {
    'f1':[0],
    'f2':"boo"
} # error

td24: TD2[str] = TD2(
    f1=[0],
    f2="boo"
) # error

td25 = TD2(
    f1=[0],
    f2="boo"
) 
reveal_type(td25) #  info: Type of "td25" is "TD2[int | Literal['boo']]

TODO

  • Support for declaring with TypedDict constructor notation
  • Adding proper test cases to the automated test suite
  • And never enough testing to make sure no corner cases were missed

@erictraut
Copy link
Collaborator

Generic TypedDict is not supported by PEP 589, nor is it supported at runtime by Python.

If you want this to be supported, you'd need to go through the typing-sig or PEP drafting process.

We are not interested in adding support to Pyright unless/until it becomes part of the Python type standard.

@erictraut
Copy link
Collaborator

That said, I'm impressed that you were able to add this functionality!

@sransara
Copy link
Contributor Author

Generic TypedDict is not supported by PEP 589, nor is it supported at runtime by Python.

If you want this to be supported, you'd need to go through the typing-sig or PEP drafting process.

We are not interested in adding support to Pyright unless/until it becomes part of the Python type standard.

Oh that is sad. I can take a look at how to suggest it on typing-sig mailing list. Should I close this PR until something is certain?

That said, I'm impressed that you were able to add this functionality!

It was a fun exercise :)

@erictraut
Copy link
Collaborator

Yeah, please close this PR for now. If it is added to the Python 3.10 runtime, we can revive this PR.

@sransara
Copy link
Contributor Author

Sounds good. Thanks for the quick response.

@sransara sransara closed this Jan 19, 2021
@jakebailey
Copy link
Member

RE: typing-sig, if you haven't done it before, easiest is to just sign up on mail.python.org and send emails there to the typing-sig asking about the feature; I'd expect that either the PEP can be revised to be less lax, or that it will end up needing a new PEP thanks to the aforementioned runtime type changes (thanks to needing the square brackets to work at runtime, etc).

@sransara
Copy link
Contributor Author

Got it. Thank you for the information.

@sransara
Copy link
Contributor Author

I sent out the email to typing-sig mailing list (awaiting moderator approval at the moment). Fingers crossed hoping there's a way to move forward with the runtime support.

heejaechang pushed a commit to heejaechang/pyright that referenced this pull request Nov 3, 2021
@sransara sransara deleted the generic-typed-dict branch October 11, 2023 22:02
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.

3 participants