def _annotations_corrector(function, bases, parameters): """ """ annotations = typing.get_type_hints(function) accumulator = dict() for key, value in annotations.items(): # If it is an unfilled typevar if isinstance(value, typing.TypeVar): # Find parent with generic version of this parameter position = None for parent in bases: if value in getattr(parent, '__parameters__', []): position = parent.__parameters__.index(value) if position is None: raise ValueError("Could not find position in __parameters__ of parent classes") # If this is a structured reference, resolve it if _is_structured_forward_ref(value): base = parameters[position] tinyns = {value._structured_forward_ref_name: parameters[position]} # my_lambda = "lambda {0}: {1}".format(value._structured_forward_ref_name, value._structured_forward_ref_code) concrete = eval(value._structured_forward_ref_code, tinyns) else: concrete = parameters[position] accumulator[key] = concrete else: accumulator[key] = value return accumulator
def _update_annotations(value, bases, parameters): if hasattr(value, '__annotations__'): annotations = typing.get_type_hints(value) corrected = _annotations_corrector(value, bases, parameters) value.__annotations__ = corrected return value # wrapped functions, such as classmethods # NOTE: this may have to handle recursive descent elif hasattr(value, '__func__'): return _update_annotations(value.__func__, bases, parameters)
# def f_map(cls, function: 'Codomain') -> 'Domain': # def f_map(cls, function: Domain) -> Domain: @classmethod def f_apply(cls, element: Codomain, function: Domain) -> Codomain: #def f_apply(cls, element: CF('Codomain.Element'), function: CF('Domain.Morphism')) -> CF('Codomain.Element'): return NotImplemented class ListFunctor(Functor[AnyCategory, ListCategory]): """ Waht I really want is for the forward refs on f_map to resolve themselves in the context of ListFunctor (which has provided values for Codomain and Domain) """ pass anno_map = typing.get_type_hints(ListFunctor.f_map) anno_apply = typing.get_type_hints(ListFunctor.f_apply) print() print("anno_map:", type(anno_map), anno_map) print("anno_apply", type(anno_apply), anno_apply) print() import ipdb ipdb.set_trace() print()