metadsl.typing_tools

Functions

generic_getattr(self, attr)

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.

generic_subclasscheck(self, cls)

Modified from https://github.com/python/cpython/blob/aa73841a8fdded4a462d045d1eb03899cbeecd65/Lib/typing.py#L707-L717

get_all_typevars(t)

rtype

Iterable[TypeVar(TypeVar, bound= <member '__bound__' of 'TypeVar' objects>, covariant=<member '__covariant__' of 'TypeVar' objects>, contravariant=<member '__contravariant__' of 'TypeVar' objects>)]

get_arg_hints(fn)

rtype

List[type[TypeVar(CT_co, bound= type, covariant=True)]]

get_bound_infer_type(b)

Returns a typing.Callable type that corresponds to the type of the bound infer.

get_fn_typevars(fn)

rtype

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)]]

get_function_replace_type(f)

rtype

type[Callable]

get_function_type(fn)

Gets the type of a function:

get_inner_types(t)

Returns the inner types for a type.

get_origin(t)

rtype

type[TypeVar(CT_co, bound= type, covariant=True)]

get_origin_type(t)

Takes in a type, and if it is a generic type, returns the original type hint.

get_type(v)

Returns the type of the value with generic arguments preserved.

get_typevars_in_scope()

rtype

Set[TypeVar(TypeVar, bound= <member '__bound__' of 'TypeVar' objects>, covariant=<member '__covariant__' of 'TypeVar' objects>, contravariant=<member '__contravariant__' of 'TypeVar' objects>)]

infer(fn, wrapper)

Wraps a function to return the args, kwargs, and the inferred return type based on the arguments.

infer_return_type(fn, owner, is_classmethod, ...)

rtype

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)]]]

inspect_signature(fn)

rtype

Signature

match_functions(fn_with_typevars, fn)

rtype

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)]]

match_type(hint, value)

rtype

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)]]

match_types(hint, t)

Matches a type hint with a type, return a mapping of any type vars to their values.

match_values(hint_value, value)

rtype

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)]]

merge_typevars(*typevars)

Merges typevar mappings.

record_scoped_typevars(f, *additional_typevars)

rtype

None

replace_fn_typevars(fn, typevars[, ...])

rtype

TypeVar(T)

replace_typevars(typevars, hint)

Replaces type vars in a type hint with other types.

special_form_subclasscheck(self, cls)

typing_get_type_hints(fn)

rtype

Dict[str, type[TypeVar(CT_co, bound= type, covariant=True)]]

Classes

BoundInfer(fn, wrapper, owner, is_classmethod)

ExpandedType(*args, **kwds)

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.

FunctionReplaceTyping(fn, typevars, ...)

GenericCheck()

Subclass this to support isinstance and issubclass checks with generic classes.

GenericCheckType

Identity()

Infer(fn, wrapper)

NewTypeVarScope([previous_typvars_in_scope])

OfType(*args, **kwds)

OfType[T] should be considered a subclass of T even though it is not.

ToCallable()

ToCallableMeta

Type of type that can be used in isubclass to determine whether it wil be understood as a callable from the typing_tools perspective.

TypeVarScope(*tvs)

class metadsl.typing_tools.BoundInfer(fn, wrapper, owner, is_classmethod)[source]
fn: Callable[[...], metadsl.typing_tools.T]
is_classmethod: bool
owner: Type
wrapper: Callable[[Callable[[...], metadsl.typing_tools.T], Tuple[object, ...], Mapping[str, object], Type[metadsl.typing_tools.T]], metadsl.typing_tools.U]
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.

class metadsl.typing_tools.ToCallable[source]
class metadsl.typing_tools.TypeVarScope(*tvs)[source]
typevars_in_scope: Tuple[TypeVar, ...]
metadsl.typing_tools.get_arg_hints(fn)[source]
Return type

List[type[TypeVar(CT_co, bound= type, covariant=True)]]

metadsl.typing_tools.get_fn_typevars(fn)[source]
Return type

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.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]

Return type

type[TypeVar(CT_co, bound= type, covariant=True)]

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).

Return type

Callable[..., TypeVar(U)]

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_functions(fn_with_typevars, fn)[source]
Return type

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.

Return type

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_values(hint_value, value)[source]
Return type

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.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

Return type

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.replace_fn_typevars(fn, typevars, inner_mapping=Identity())[source]
Return type

TypeVar(T)

metadsl.typing_tools.replace_typevars(typevars, hint)[source]

Replaces type vars in a type hint with other types.

Return type

TypeVar(T_type, bound= type[TypeVar(CT_co, bound= type, covariant=True)])