-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Support for generic TypedDict #3863
Comments
Generic TypedDicts are not supported yet. (Though if there were, this would be a reasonable notations -- after all this is how you declare generic aliases.) |
Well then, consider this a feature request.
…On Tue, Aug 22, 2017, 5:21 PM Guido van Rossum ***@***.***> wrote:
Generic TypedDicts are not supported yet. (Though if there were, this
would be a reasonable notations -- after all this is how you declare
generic aliases.)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#3863 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABBndywOD43OsdDFu1fawtNkYW1ZpFDAks5sa3BpgaJpZM4O_UuC>
.
|
To add a justification note, this feature would be useful for handling polymorphic JSON responses, such as a shared pager: class Item(TypedDict):
type: str
class ValueItem(Item):
value: int
T = TypeVar('T', bound=Item)
class ItemPager(TypedDict, Generic[T]):
next_page: Optional[str]
items: List[T]
def get_value_items() -> List[ValueItem]:
value_items: List[ValueItem] = []
url = "https://example.com/value_items"
while url:
response = requests.get(url)
pager: ItemPager[ValueItem] = response.json()
url = pager['next_page']
value_items += pager['items']
return value_items |
Hi! |
I suggest filing a bpo issue for that.
|
@pawelrubin was a bpo issue created for this? |
There's now a BPO issue for this here |
See also python/typing_extensions#7 |
To expand on this: generic |
We likely won't add this support to mypy-extensions. Retitling this to reflect the request that mypy adds support for generic TypedDicts in general. |
@JelleZijlstra, could you add a link to this issue in #11753? |
Fixes #3863 This builds on top of some infra I added for recursive types (Ref #13297). Implementation is quite straightforward. The only non-trivial thing is that when extending/merging TypedDicts, the item types need to me mapped to supertype during semantic analysis. This means we can't call `is_subtype()` etc., and can in theory get types like `Union[int, int]`. But OTOH this equally applies to type aliases, and doesn't seem to cause problems.
mypy complains about
R
in theTypedDict
declaration. I would expect that I could declare a generic TypedDict like this.The text was updated successfully, but these errors were encountered: