Skip to content

Implement KeyOf[T] type utility #1

@AliiiBenn

Description

@AliiiBenn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions