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

Typeguard for TypedDict keys #11553

Open
tlambert03 opened this issue Nov 14, 2021 · 3 comments
Open

Typeguard for TypedDict keys #11553

tlambert03 opened this issue Nov 14, 2021 · 3 comments
Labels
feature topic-type-narrowing Conditional type narrowing / binder topic-typed-dict

Comments

@tlambert03
Copy link

Feature

I don't know enough to say whether this is a mypy request or a typing module request... but I find myself doing this pattern to avoid the error TypedDict key must be a string literal; expected one of ("a", "b") [misc]

class MyDict(TypedDict, total=False):
    a: int
    b: str
    ...

MyDictKeys = Union[Literal['a'], Literal['b']]

def is_mydict_key(key: str) -> TypeGuard[WidgetOptionKeys]:
    return key in MyDictKeys.__annotations__

Pitch

not sure how it would be implemented (or if it already is!) ... but it would be nice to have a way to type narrow a key without having to duplicate all of the keys in a TypedDict in a separate Union of Literals

@A5rocks
Copy link
Contributor

A5rocks commented Nov 15, 2021

You can use Literal['a', 'b'] to shorten the code a bit, but I see what you mean....

mypy could probably add something where if k in n infers k is a key of n... Probably? I think there's actually already a feature request here for that, but I suspect TypedDict would have to be special-cased in some way lol

@AlexWaygood AlexWaygood added the topic-type-narrowing Conditional type narrowing / binder label May 3, 2022
@AlexWaygood
Copy link
Member

mypy could probably add something where if k in n infers k is a key of n... Probably? I think there's actually already a feature request here for that, but I suspect TypedDict would have to be special-cased in some way lol

This is the relevant feature request:

@hmvp
Copy link

hmvp commented Aug 8, 2022

Typescript has a keyOf operator: https://www.typescriptlang.org/docs/handbook/2/keyof-types.html

But to make that work here it could be something like:

class MyDict(TypedDict, total=False):
    a: int
    b: str

def is_mydict_key(key: KeyOf[MyDict]) -> TypeGuard[WidgetOptionKeys]:
    return key in MyDictKeys.__annotations__

Obviously is_mydict_key is a bit useless in this example since mypy would already complain if its not a key.

KeyOf[MyDict] would 'desugar' to Literal['a', 'b']

jelmer added a commit to jelmer/myhoard that referenced this issue Jan 9, 2023
TypedDict doesn't have a derived type for its keys yet and I didn't want
to add a static Literal for each TypedDict that we have to keep up to
date, so there's some additional "type: ignore"s for now. See also
python/mypy#11553
hessu pushed a commit to hessu/myhoard that referenced this issue Jan 21, 2024
TypedDict doesn't have a derived type for its keys yet and I didn't want
to add a static Literal for each TypedDict that we have to keep up to
date, so there's some additional "type: ignore"s for now. See also
python/mypy#11553
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-type-narrowing Conditional type narrowing / binder topic-typed-dict
Projects
None yet
Development

No branches or pull requests

5 participants