Example #1
0
    def lookup_concrete(self, tvar):
        """Find a set representative, which is either a concrete type, or the first element."""
        if not isinstance(tvar, TypeVar):
            return tvar

        i = self._find(tvar)
        the_set = self._sets[i]

        rep = None
        for t in the_set:
            if rep is None:
                rep = t
            elif not _type_vars([t]):
                rep = t
            elif (isinstance(t, CallableMeta) or isinstance(t, TuplePlus)
                  or isinstance(t, GenericMeta)) and isinstance(rep, TypeVar):
                rep = t

        if isinstance(rep, CallableMeta):
            return _gorg(
                rep)[[self.lookup_concrete(t1) for t1 in rep.__args__[:-1]],
                     self.lookup_concrete(rep.__args__[-1])]
        elif isinstance(rep, GenericMeta):
            return _gorg(rep)[tuple(
                self.lookup_concrete(t1) for t1 in rep.__args__)]
        return rep or tvar
Example #2
0
    def _get_bounded_typevars(self, hint):
        if not hasattr(self, 'origin_typevars'):
            self.origin_typevars = _gorg(hint).__parameters__

        for origin_typevar, param in zip(self.origin_typevars, hint.__parameters__):
            origin_typevar.__bound__ = param
        return self.origin_typevars
Example #3
0
def _gorg(x):
    """Make _gorg compatible for Python 3.6.2 and 3.6.3."""
    if sys.version_info >= (3, 7, 0):
        return x.__origin__
    if sys.version_info < (3, 6, 3):
        return typing._gorg(x)
    else:
        return x._gorg
Example #4
0
def lookup_method(name, caller_type, *args):
    """Lookup method with the given name for the given type."""
    if isinstance(caller_type, TupleMeta):
        caller_origin = Tuple
    elif isinstance(caller_type, GenericMeta):
        caller_origin = _gorg(caller_type)
    else:
        caller_origin = caller_type

    return TYPE_SIGNATURES[caller_origin][name]
Example #5
0
 def _type_eval(self, t):
     """Evaluate a type. Used for tuples."""
     if isinstance(t, TuplePlus):
         return t.eval_type(self)
     if isinstance(t, TypeVar):
         return self.lookup_concrete(t)
     if isinstance(t, GenericMeta) and t.__args__ is not None:
         return _gorg(t)[tuple(self._type_eval(argument) for argument in t.__args__)]
     else:
         return t
 def _generic_new(base_cls, cls, *args,
                  **kwds):  # noqa: ANN001,ANN002,ANN003,ANN202
     if cls.__origin__ is None:
         return base_cls.__new__(cls)
     else:
         origin = _gorg(cls)
         obj = base_cls.__new__(origin)
         with suppress_ex(AttributeError):
             obj.__orig_class__ = cls
         obj.__init__(*args, **kwds)
         return obj
Example #7
0
 def __Generic__new__(cls, *args, **kwds):
     # this is based on Generic.__new__ from typing-3.5.2.2
     if cls.__origin__ is None:
         obj = cls.__next_in_mro__.__new__(cls)
         obj.__orig_class__ = cls
     else:
         origin = typing._gorg(cls)
         obj = cls.__next_in_mro__.__new__(origin)
         obj.__orig_class__ = cls
         obj.__init__(*args, **kwds)
     return obj
Example #8
0
def literal_substitute(t, type_map):
    """Make substitutions in t according to type_map, returning resulting type."""
    if isinstance(t, TypeVar) and t.__name__ in type_map:
        return type_map[t.__name__]
    elif isinstance(t, TuplePlus):
        subbed_args = [literal_substitute(t1, type_map) for t1 in t.__constraints__]
        return TuplePlus('tup+', *subbed_args)
    elif isinstance(t, CallableMeta):
        args = list(literal_substitute(t1, type_map) for t1 in t.__args__[:-1])
        res = literal_substitute(t.__args__[-1], type_map)
        new_t = Callable[args, res]
        if hasattr(t, 'polymorphic_tvars'):
            new_t.polymorphic_tvars = t.polymorphic_tvars
        return new_t
    elif isinstance(t, GenericMeta) and t.__args__ is not None:
        return _gorg(t)[tuple(literal_substitute(t1, type_map) for t1 in t.__args__)]
    else:
        return t
Example #9
0
def get_origin(tp):
    """Get the unsubscripted version of a type. Supports generic types, Union,
    Callable, and Tuple. Returns None for unsupported types. Examples::

        get_origin(int) == None
        get_origin(ClassVar[int]) == None
        get_origin(Generic) == Generic
        get_origin(Generic[T]) == Generic
        get_origin(Union[T, int]) == Union
        get_origin(List[Tuple[T, T]][int]) == List
    """

    if isinstance(tp, GenericMeta):
        return _gorg(tp)
    if is_union_type(tp):
        return Union

    return None
Example #10
0
    def _is_unannotated(self, hint):
        if not hasattr(self, 'origin_typevars'):
            self.origin_typevars = _gorg(hint).__parameters__

        return hint.__parameters__ == self.origin_typevars
Example #11
0
def _gorg(x):
    if sys.version_info < (3, 6, 3):
        return typing._gorg(x)
    else:
        return x._gorg
Example #12
0
File: base.py Project: nigef/pyta
def _gorg(x):
    if sys.version_info < (3, 6, 3):
        return typing._gorg(x)
    else:
        return x._gorg