Skip to content

Commit

Permalink
Some preparation for Python 3.7 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stewori committed Apr 5, 2018
1 parent 774e4be commit 928fd62
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 59 deletions.
3 changes: 2 additions & 1 deletion pytypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ def __Generic__new__(cls, *args, **kwds):
get_generator_yield_type, is_Union, get_Union_params, get_Tuple_params, is_Tuple_ellipsis, \
get_Callable_args_res, get_Generic_itemtype, get_Mapping_key_value, get_Generic_parameters,\
get_arg_for_TypeVar, _issubclass as is_subtype, _isinstance as is_of_type, annotations, \
get_member_types, Empty, _catch_up_global_annotations_decorator, TypeAgent, restore_profiler
get_member_types, Empty, _catch_up_global_annotations_decorator, TypeAgent, restore_profiler, \
is_Tuple, is_Generic, is_Callable, _extra_dict as abc2typing_dict
from .util import getargspecs, get_staticmethod_qualname, get_class_qualname, mro, \
get_class_that_defined_method, is_method, is_classmethod, _pytypes_excepthook, \
_install_excepthook
Expand Down
20 changes: 12 additions & 8 deletions pytypes/stubfile_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import warnings
from inspect import isclass, ismodule, ismethod
try:
from backports.typing import Union, Tuple, TupleMeta, GenericMeta, CallableMeta
from backports.typing import Union, Tuple, Callable
except ImportError:
from typing import Union, Tuple, TupleMeta, GenericMeta, CallableMeta
from typing import Union, Tuple, Callable

import pytypes
from pytypes import util
Expand Down Expand Up @@ -247,7 +247,7 @@ def _match_stub_type(stub_type):
return stub_type
# Todo: Only apply if stub-module is involved
# Todo: Somehow cache results
if isinstance(stub_type, TupleMeta):
if pytypes.is_Tuple(stub_type):
prms = pytypes.get_Tuple_params(stub_type)
res = Tuple[(tuple(_match_stub_type(t) for t in prms))]
elif pytypes.is_Union(stub_type):
Expand All @@ -256,19 +256,23 @@ def _match_stub_type(stub_type):
res = Union[tuple(_match_stub_type(t) for t in stub_type.__args__)]
except AttributeError:
res = Union[tuple(_match_stub_type(t) for t in stub_type.__union_params__)]
elif isinstance(stub_type, GenericMeta):
elif pytypes.is_Generic(stub_type):
if stub_type.__args__ is None:
res = stub_type
elif isinstance(stub_type, CallableMeta):
elif pytypes.is_Callable(stub_type):
if hasattr(stub_type, '__result__'):
res = stub_type.__origin__[tuple(_match_stub_type(t) for t in stub_type.__args__)]
res = Callable[tuple(_match_stub_type(t) for t in stub_type.__args__)]
res.__result__ = _match_stub_type(stub_type.__result__)
else:
res = stub_type.__origin__[tuple([
res = Callable[tuple([
[_match_stub_type(t) for t in stub_type.__args__[:-1]],
_match_stub_type(stub_type.__args__[-1]) ]) ]
else:
res = stub_type.__origin__[tuple(_match_stub_type(t) for t in stub_type.__args__)]
tpl = tuple(_match_stub_type(t) for t in stub_type.__args__)
try:
res = stub_type.__origin__[tpl]
except TypeError:
res = pytypes.abc2typing_dict[stub_type.__origin__][tpl]
elif isclass(stub_type):
res = stub_type._match_type if hasattr(stub_type, '_match_type') else stub_type
else:
Expand Down
Loading

0 comments on commit 928fd62

Please sign in to comment.