metadsl.typing_tools
Functions
|
Allows classmethods to get generic types by checking if we are getting a descriptor type and if we are, we pass in the generic type as the class instead of the origin type. |
|
Modified from https://github.com/python/cpython/blob/aa73841a8fdded4a462d045d1eb03899cbeecd65/Lib/typing.py#L707-L717 |
|
|
|
|
|
Returns a typing.Callable type that corresponds to the type of the bound infer. |
|
|
|
|
|
Gets the type of a function: |
|
Returns the inner types for a type. |
|
|
Takes in a type, and if it is a generic type, returns the original type hint. |
|
|
Returns the type of the value with generic arguments preserved. |
|
|
|
Wraps a function to return the args, kwargs, and the inferred return type based on the arguments. |
|
|
|
|
|
|
|
|
|
Matches a type hint with a type, return a mapping of any type vars to their values. |
|
|
|
Merges typevar mappings. |
|
|
|
|
|
Replaces type vars in a type hint with other types. |
|
|
|
Classes
|
|
|
ExpandedType should be thought of as being expanded when passed into a function, so that fn(ExpandedType[int]()) will be thought of as fn(*xs) where xs is an iterable of int. |
|
|
Subclass this to support isinstance and issubclass checks with generic classes. |
|
|
|
|
|
|
|
|
|
|
OfType[T] should be considered a subclass of T even though it is not. |
|
Type of type that can be used in isubclass to determine whether it wil be understood as a callable from the typing_tools perspective. |
|
- class metadsl.typing_tools.BoundInfer(fn, wrapper, owner, is_classmethod)[source]
- fn: Callable[[...], metadsl.typing_tools.T]
- owner: Type
- class metadsl.typing_tools.ExpandedType(*args, **kwds)[source]
ExpandedType should be thought of as being expanded when passed into a function, so that fn(ExpandedType[int]()) will be thought of as fn(*xs) where xs is an iterable of int.
- class metadsl.typing_tools.GenericCheck[source]
Subclass this to support isinstance and issubclass checks with generic classes.
- class metadsl.typing_tools.OfType(*args, **kwds)[source]
OfType[T] should be considered a subclass of T even though it is not.
- metadsl.typing_tools.get_origin_type(t)[source]
Takes in a type, and if it is a generic type, returns the original type hint.
typing.List[int] -> typing.List[T]
- metadsl.typing_tools.get_type(v)[source]
Returns the type of the value with generic arguments preserved.
- Return type
type
[TypeVar
(T
)]
- metadsl.typing_tools.infer(fn, wrapper)[source]
Wraps a function to return the args, kwargs, and the inferred return type based on the arguments.
This raise a TypeError when called with types that are invalid.
It refers on the explicit generic types of the arguments, it does not traverse them to check their types. That means if you pass in [1, 2, 3] it won’t know this is a typing.List[int]. Instead it, will only know if you create a generic instance manually from a custom generic class like, MyList[int](1, 2, 3).
- metadsl.typing_tools.infer_return_type(fn, owner, is_classmethod, args, kwargs)[source]
- Return type
Tuple
[Tuple
[object
,...
],Mapping
[str
,object
],type
[TypeVar
(T
)],Mapping
[TypeVar
(TypeVar
, bound= <member ‘__bound__’ of ‘TypeVar’ objects>, covariant=<member ‘__covariant__’ of ‘TypeVar’ objects>, contravariant=<member ‘__contravariant__’ of ‘TypeVar’ objects>),type
[TypeVar
(CT_co
, bound=type
, covariant=True)]]]
- metadsl.typing_tools.match_types(hint, t)[source]
Matches a type hint with a type, return a mapping of any type vars to their values.
- metadsl.typing_tools.merge_typevars(*typevars)[source]
Merges typevar mappings. If there is a duplicate key, either the values should be the same or one should have typing.Any type, or one should be a typevar itself. If it is a typevar, that typevar is also set to the other’s value