Skip to main content

No project description provided

Project description

typeapi

Python

Compatibility: Python 3.6.3+

The typeapi package provides an object-oriented interface for introspecting type hints from the typing and typing_extensions module at runtime. Currently, only a subset of the different kinds of type hints are supported, namely through the following representations:

Concrete type Description Added in
ClassTypeHint For any normal or generic type as well as typing.Any. Provides access to the underlying type, the type arguments and parameters, if any. 1.0.0
UnionTypeHint Represents Union type hint and gives access to the union members. 1.0.0
LiteralTypeHint Represents a Literal type hint and gives access to the literal values. 1.0.0
AnnotatedTypeHint Represents an Annotated type hint and gives access to the annotated type as well as the metadata. 1.0.0
TypeVarTypeHint Represents a TypeVar type hint and gives an interface to access the variable's metadata (such as constarints, variance, ...). 1.0.0
ForwardRefTypeHint Represents a forward reference. 1.0.0
TupleTypeHint Reperesents a Tuple type hint, allowing you to differentiate between repeated and explicitly sized tuples. 1.2.0

The main entry point to wrapping a low-level type hint is the TypeHint() constructor.

Examples

Inspect a List[int] type hint:

# cat <<EOF | python -
from typeapi import ClassTypeHint, TypeHint
from typing import List

hint = TypeHint(List[int])
assert isinstance(hint, ClassTypeHint)
assert hint.type is list

item_hint = hint[0]
assert isinstance(item_hint, ClassTypeHint)
assert item_hint.type is int

Retrieve the metadata from an Annotated[...] type hint:

# cat <<EOF | python -
from typeapi import AnnotatedTypeHint, ClassTypeHint, TypeHint
from typing_extensions import Annotated

hint = TypeHint(Annotated[int, 42])
assert isinstance(hint, AnnotatedTypeHint)
assert hint.type is int
assert hint.metadata == (42,)

sub_hint = hint[0]
assert isinstance(sub_hint, ClassTypeHint)
assert sub_hint.type is int

Parameterize one type hint with the parameterization of a generic alias:

# cat <<EOF | python -
from dataclasses import dataclass
from typeapi import ClassTypeHint, TypeHint
from typing import Generic, TypeVar
from typing_extensions import Annotated

T = TypeVar("T")

@dataclass
class MyGeneric(Generic[T]):
  value: T

hint = TypeHint(MyGeneric[int])
assert isinstance(hint, ClassTypeHint)
assert hint.get_parameter_map() == {T: int}

member_hint = TypeHint(T).parameterize(hint.get_parameter_map())
assert isinstance(member_hint, ClassTypeHint)
assert member_hint.type is int

Evaluate forward references:

# cat <<EOF | python -
from typeapi import ClassTypeHint, ForwardRefTypeHint, TypeHint
from typing import List

MyVector = List["MyType"]

class MyType:
  pass

hint = TypeHint(MyVector)
assert isinstance(hint, ClassTypeHint)
assert hint.type is list

item_hint = hint[0]
assert isinstance(item_hint, ForwardRefTypeHint)
assert item_hint.expr == "MyType"

hint = hint.evaluate(globals())
item_hint = hint[1]
assert isinstance(item_hint, ClassTypeHint)
assert item_hint.type is MyType

Planned work

  • Support more features of the typing system (e.g. ClassVar, ParamSpec, ...)
  • Support evaluating forward references that utilize newer Python language features (such as built-in type subscripts and type union syntax).
    • Subscript support could be achieved by mocking the built-in types during the evaluation of the expression.
    • Type unions could be achieved by rewriting the expression AST before evaluating and mocking every value in the expression.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typeapi-1.2.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typeapi-1.2.0-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file typeapi-1.2.0.tar.gz.

File metadata

  • Download URL: typeapi-1.2.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.2 requests/2.28.1 requests-toolbelt/0.10.0 urllib3/1.26.12 tqdm/4.64.1 importlib-metadata/5.0.0 keyring/23.9.3 rfc3986/2.0.0 colorama/0.4.5 CPython/3.10.4

File hashes

Hashes for typeapi-1.2.0.tar.gz
Algorithm Hash digest
SHA256 a215015b5f79e990ef86f19573ce5ad544800233f9ce6ce22ed83b81c3a7f408
MD5 9ca5a3fe27e0072144d9a99afda2c530
BLAKE2b-256 6a592979791890bce78df943996bc12d02c179af01364bca90df0e39345fdcb9

See more details on using hashes here.

File details

Details for the file typeapi-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: typeapi-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.2 requests/2.28.1 requests-toolbelt/0.10.0 urllib3/1.26.12 tqdm/4.64.1 importlib-metadata/5.0.0 keyring/23.9.3 rfc3986/2.0.0 colorama/0.4.5 CPython/3.10.4

File hashes

Hashes for typeapi-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a3a72857029e76ad445a926a20fbf20d80a57fb433359e9e030289247fd626f
MD5 5a991c4ac424df46024995df7fe846a9
BLAKE2b-256 adf33c3f46d38089b194a14f30099066e8f968cdb95fa14341e28e465381040a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page