forked from vercel/python-typemap
-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
Description
Add a KeyOf[T] type that extracts the literal keys of a type T, similar to TypeScript's keyof.
Specification
KeyOf[dict[str, int]] = Literal['keys', 'values', 'items', ...]
KeyOf[MyClass] = Literal['field1', 'field2', ...]
KeyOf[MyTypedDict] = Literal['key1', 'key2', ...]Implementation notes
- Should work with classes, TypedDict, and regular dict types
- Needs runtime evaluator in
typemap/type_eval/_eval_operators.py - Should use existing introspection capabilities (
Attrs,Members) - Returns a union of Literal types
References
- PEP 827 draft mentions KeyOf as a requested feature (line 1666)
- TypeScript equivalent:
keyof T
Acceptance criteria
- KeyOf works with regular classes
- KeyOf works with TypedDict
- KeyOf works with dict types
- Runtime evaluator passes all tests
- Unit tests added
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels