-
-
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
Typeguard for TypedDict keys #11553
Comments
You can use mypy could probably add something where |
This is the relevant feature request: |
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
|
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
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
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]
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 separateUnion
ofLiterals
The text was updated successfully, but these errors were encountered: