diff --git a/plugins/typing b/plugins/typing index 884c714f..798b7ed3 160000 --- a/plugins/typing +++ b/plugins/typing @@ -1 +1 @@ -Subproject commit 884c714f1338698cc467f82a7615d9dd4fe4b6b9 +Subproject commit 798b7ed37d9878ef7884ff7de2433afac5032fe5 diff --git a/potc/rules/native/__init__.py b/potc/rules/native/__init__.py index 08cb3bfa..ea3193b7 100644 --- a/potc/rules/native/__init__.py +++ b/potc/rules/native/__init__.py @@ -2,9 +2,7 @@ builtin_slice, builtin_range, builtin_bytes, builtin_float, builtin_module, \ builtin_tuple, builtin_func, builtin_type, builtin_dict, builtin_int, builtin_list, builtin_none, builtin_set, \ builtin_str, builtin_items, builtin_complex, builtin_object -from .typing import typing_wrapper, typing_all, typing_typevar, typing_callable, typing_items native_all = [ - typing_all, builtin_all, ] diff --git a/potc/rules/native/typing.py b/potc/rules/native/typing.py deleted file mode 100644 index 6517d78c..00000000 --- a/potc/rules/native/typing.py +++ /dev/null @@ -1,149 +0,0 @@ -import re -import typing - -try: - from collections.abc import Callable as _CollectionCallable -except ImportError: - _CollectionCallable = None -from collections import OrderedDict -from functools import lru_cache, wraps -from types import FunctionType -from typing import TypeVar, Callable - -from .builtin import builtin_type, builtin_raw_type -from ...fixture import rule, Addons - -try: - from types import GenericAlias -except ImportError: - GenericAlias = None - - -@lru_cache() -def _get_origin_trans_map(): - return { - value.__origin__: value - for key, value in typing.__dict__.items() - if getattr(value, '__origin__', None) and isinstance(value.__origin__, type) - } - - -def _origin_trans(v): - o = v.__origin__ - if GenericAlias is not None and isinstance(v, GenericAlias): - return o - else: - return _get_origin_trans_map().get(o, o) - - -def _is_wrapper(func): - @wraps(func) - def _new_func(v, addon: Addons): - if hasattr(v, '__origin__') and hasattr(v, '__args__'): - return func(v, addon) - else: - addon.unprocessable() - - return _new_func - - -@rule() -@_is_wrapper -def typing_callable(v, addon: Addons): - if (_CollectionCallable is not None and v.__origin__ == _CollectionCallable) or \ - (Callable is not None and v.__origin__ == Callable): - _base = addon.val(_origin_trans(v)) - _args, _ret = v.__args__[:-1], v.__args__[-1] - if _args == (Ellipsis,): - return _base[v.__args__] - else: - return _base[list(_args), _ret] - - else: - addon.unprocessable() - - -@rule() -@_is_wrapper -def typing_wrapper(v, addon: Addons): - _base = addon.val(_origin_trans(v)) - if len(v.__args__) == 0: - return _base - elif len(v.__args__) == 1: - return _base[v.__args__[0]] - else: - return _base[v.__args__] - - -_common_types = (type, FunctionType) -try: - from typing import TypingMeta - - - def _is_item(x): - return isinstance(x, _common_types) or isinstance(type(x), TypingMeta) - -except ImportError: - from typing import _Final - - - def _is_item(x): - return isinstance(x, (*_common_types, _Final)) - -_ALL_NAMES = set(typing.__all__) -_STD_NAME = re.compile('^[A-Z][A-Za-z0-9]*$') - - -def _is_name(k): - return k in _ALL_NAMES or _STD_NAME.fullmatch(k) - - -_TYPING_ITEMS = {value: key for key, value in typing.__dict__.items() if _is_name(key) and _is_item(value)} - - -@rule() -def typing_items(v, addon: Addons): - if _is_item(v) and v in _TYPING_ITEMS.keys(): - _v_name = _TYPING_ITEMS[v] - try: - return addon.obj(v, _v_name) - except (ImportError, TypeError): - addon.from_(typing.__name__).import_(_v_name) - return addon.raw(_v_name) - else: - addon.unprocessable() - - -@rule(type_=TypeVar) -def typing_typevar(v: TypeVar, addon: Addons): - _kwargs = OrderedDict() - if v.__bound__ is not None: - _kwargs['bound'] = v.__bound__ - if v.__covariant__: - _kwargs['covariant'] = v.__covariant__ - if v.__contravariant__: - _kwargs['contravariant'] = v.__contravariant__ - - return addon.obj(TypeVar)(v.__name__, *v.__constraints__, **_kwargs) - - -_typing_self_all = [ - ( - typing_typevar, - typing_items, - typing_callable, - typing_wrapper, - ) -] - -#: Overview: -#: Typing types -#: -#: Items: -#: - :class:`TypeVar` -#: - Items in module ``typing`` -#: - :class:`Callable` -#: - :class:`Wrapper` -typing_all = [ - (builtin_type, _typing_self_all, builtin_raw_type), -] diff --git a/potc/testing/property.py b/potc/testing/property.py index cf6a92ef..0035cfc8 100644 --- a/potc/testing/property.py +++ b/potc/testing/property.py @@ -1,7 +1,7 @@ import math import types from functools import partial -from typing import NamedTuple, NoReturn, List, Set, Dict, Tuple, Callable, Any, TypeVar +from typing import NamedTuple from dill import source @@ -78,7 +78,7 @@ def provement(trans=None): Examples: >>> import pytest - >>> from potc_dict.plugin import __rules__ # load rules from plugin potc_dict + >>> from potc_typing.plugin import __rules__ # load rules from plugin potc_typing >>> >>> from potc.testing import provement >>> from potc.translate import BlankTranslator @@ -332,82 +332,4 @@ def test_mixed_case(self): 'feat': [None, type, int, 233, 'sdfkl', (5, 6, -3 + 1j), {}, set(), []], } - def test_typing(self): - with transobj_assert(NoReturn) as (obj, name): - assert obj is NoReturn - - with transobj_assert(List) as (obj, name): - assert obj == List - with transobj_assert(Dict) as (obj, name): - assert obj == Dict - with transobj_assert(Set) as (obj, name): - assert obj == Set - with transobj_assert(Tuple) as (obj, name): - assert obj == Tuple - with transobj_assert(Callable) as (obj, name): - assert obj == Callable - - with transobj_assert(List[int]) as (obj, name): - assert obj == List[int] - with transobj_assert(Dict[int, List[str]]) as (obj, name): - assert obj == Dict[int, List[str]] - with transobj_assert(Set[str]) as (obj, name): - assert obj == Set[str] - with transobj_assert(Tuple[int, str, List[str]]) as (obj, name): - assert obj == Tuple[int, str, List[str]] - with transobj_assert(Callable[..., int]) as (obj, name): - assert obj == Callable[..., int] - with transobj_assert(Callable[[], int]) as (obj, name): - assert obj == Callable[[], int] - with transobj_assert(Callable[[int, Any], str]) as (obj, name): - assert obj == Callable[[int, Any], str] - with transobj_assert(Callable[[int], Any]) as (obj, name): - assert obj == Callable[[int], Any] - - try: - _ = list[int] - except TypeError: - pass - else: - with transobj_assert(list[int]) as (obj, name): - assert obj == list[int] - with transobj_assert(dict[int, list[int]]) as (obj, name): - assert obj == dict[int, list[int]] - - K = TypeVar('K', bound=int, contravariant=True) - V = TypeVar('V', int, str, covariant=True) - - with transobj_assert(K) as (obj, name): - assert isinstance(obj, TypeVar) - assert obj.__name__ == 'K' - assert obj.__constraints__ == () - assert obj.__bound__ == int - assert not obj.__covariant__ - assert obj.__contravariant__ - - with transobj_assert(V) as (obj, name): - assert isinstance(obj, TypeVar) - assert obj.__name__ == 'V' - assert obj.__constraints__ == (int, str) - assert obj.__bound__ is None - assert obj.__covariant__ - assert not obj.__contravariant__ - - with transobj_assert(Dict[K, V]) as (obj, name): - assert obj.__origin__ in {Dict, dict} - _k, _v = obj.__args__ - assert isinstance(_k, TypeVar) - assert _k.__name__ == 'K' - assert _k.__constraints__ == () - assert _k.__bound__ == int - assert not _k.__covariant__ - assert _k.__contravariant__ - - assert isinstance(_v, TypeVar) - assert _v.__name__ == 'V' - assert _v.__constraints__ == (int, str) - assert _v.__bound__ is None - assert _v.__covariant__ - assert not _v.__contravariant__ - return _Provement diff --git a/test/__init__.py b/test/__init__.py index 4ccdbdab..e69de29b 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -1,5 +0,0 @@ -from .config import * -from .entry import * -from .fixture import * -from .rules import * -from .translate import * diff --git a/test/config/__init__.py b/test/config/__init__.py index 1c6ac2fd..e69de29b 100644 --- a/test/config/__init__.py +++ b/test/config/__init__.py @@ -1 +0,0 @@ -from .test_meta import TestConfigMeta diff --git a/test/entry/__init__.py b/test/entry/__init__.py index a5bd848c..e69de29b 100644 --- a/test/entry/__init__.py +++ b/test/entry/__init__.py @@ -1 +0,0 @@ -from .cli import * diff --git a/test/entry/cli/__init__.py b/test/entry/cli/__init__.py index fd709cd3..e69de29b 100644 --- a/test/entry/cli/__init__.py +++ b/test/entry/cli/__init__.py @@ -1,3 +0,0 @@ -from .test_export import TestEntryCliExport -from .test_trans import TestEntryCliTrans -from .test_version import TestEntryCliVersion diff --git a/test/fixture/__init__.py b/test/fixture/__init__.py index e6a10477..e69de29b 100644 --- a/test/fixture/__init__.py +++ b/test/fixture/__init__.py @@ -1,4 +0,0 @@ -from .test_addons import TestFixtureAddons -from .test_chain import TestFixtureChain -from .test_common import TestFixtureCommon -from .test_imports import TestFixtureImports diff --git a/test/rules/__init__.py b/test/rules/__init__.py index 32cebf49..e69de29b 100644 --- a/test/rules/__init__.py +++ b/test/rules/__init__.py @@ -1,3 +0,0 @@ -from .native import * -from .test_basic import TestRulesProvementBasic, TestRulesProvementBlank, TestRulesProvementCommon -from .test_installed import TestRulesInstalled diff --git a/test/rules/native/__init__.py b/test/rules/native/__init__.py index dd4531eb..e69de29b 100644 --- a/test/rules/native/__init__.py +++ b/test/rules/native/__init__.py @@ -1,2 +0,0 @@ -from .test_builtins import TestRulesNativeBuiltins -from .test_typing import TestRuleNativeTyping diff --git a/test/rules/native/test_typing.py b/test/rules/native/test_typing.py deleted file mode 100644 index d80d9618..00000000 --- a/test/rules/native/test_typing.py +++ /dev/null @@ -1,118 +0,0 @@ -from typing import List, Dict, TypeVar, NoReturn, Callable - -import pytest - -from potc.testing import transobj_assert - -try: - from typing import TypingMeta -except ImportError: - is_3_6 = False -else: - is_3_6 = True - -try: - _ = list[int] -except TypeError: - is_3_9 = False -else: - is_3_9 = True - -only_3_6 = pytest.mark.unittest if is_3_6 else pytest.mark.ignore -only_3_9 = pytest.mark.unittest if is_3_9 else pytest.mark.ignore - - -class TestRuleNativeTyping: - @pytest.mark.unittest - def test_typing_items(self): - with transobj_assert(NoReturn) as (obj, name): - assert obj is NoReturn - assert name == 'typing_items' - - @pytest.mark.unittest - def test_collection_types(self): - with transobj_assert(List) as (obj, name): - assert obj == List - assert name == ('typing_items' if not is_3_6 else 'builtin_type') - - with transobj_assert(Dict) as (obj, name): - assert obj == Dict - assert name == ('typing_items' if not is_3_6 else 'builtin_type') - - @pytest.mark.unittest - def test_simple_collections(self): - with transobj_assert(List[int]) as (obj, name): - assert obj == List[int] - assert name == 'typing_wrapper' - - with transobj_assert(Dict[int, List[int]]) as (obj, name): - assert obj == Dict[int, List[int]] - assert name == 'typing_wrapper' - - @pytest.mark.unittest - def test_callable(self): - with transobj_assert(Callable[[], int]) as (obj, name): - assert obj == Callable[[], int] - assert name == 'typing_callable' - - with transobj_assert(Callable[..., int]) as (obj, name): - assert obj == Callable[..., int] - assert name == 'typing_callable' - - with transobj_assert(Callable[[int, str], List[int]]) as (obj, name): - assert obj == Callable[[int, str], List[int]] - assert name == 'typing_callable' - - @only_3_9 - def test_advanced_general_alias(self): - with transobj_assert(list[int]) as (obj, name): - assert obj == list[int] - assert name == 'typing_wrapper' - - with transobj_assert(dict[int, list[int]]) as (obj, name): - assert obj == dict[int, list[int]] - assert name == 'typing_wrapper' - - @pytest.mark.unittest - def test_typevar(self): - K = TypeVar('K', bound=int, contravariant=True) - V = TypeVar('V', int, str, covariant=True) - - with transobj_assert(K) as (obj, name): - assert isinstance(obj, TypeVar) - assert obj.__name__ == 'K' - assert obj.__constraints__ == () - assert obj.__bound__ == int - assert not obj.__covariant__ - assert obj.__contravariant__ - - assert name == 'typing_typevar' - - with transobj_assert(V) as (obj, name): - assert isinstance(obj, TypeVar) - assert obj.__name__ == 'V' - assert obj.__constraints__ == (int, str) - assert obj.__bound__ is None - assert obj.__covariant__ - assert not obj.__contravariant__ - - assert name == 'typing_typevar' - - with transobj_assert(Dict[K, V]) as (obj, name): - assert obj.__origin__ == (Dict if is_3_6 else dict) - _k, _v = obj.__args__ - assert isinstance(_k, TypeVar) - assert _k.__name__ == 'K' - assert _k.__constraints__ == () - assert _k.__bound__ == int - assert not _k.__covariant__ - assert _k.__contravariant__ - - assert isinstance(_v, TypeVar) - assert _v.__name__ == 'V' - assert _v.__constraints__ == (int, str) - assert _v.__bound__ is None - assert _v.__covariant__ - assert not _v.__contravariant__ - - assert name == 'typing_wrapper' diff --git a/test/translate/__init__.py b/test/translate/__init__.py index 81ea247c..e69de29b 100644 --- a/test/translate/__init__.py +++ b/test/translate/__init__.py @@ -1 +0,0 @@ -from .test_trans import TestTranslateTrans diff --git a/test/translate/test_trans.py b/test/translate/test_trans.py index 6bc27184..255216b3 100644 --- a/test/translate/test_trans.py +++ b/test/translate/test_trans.py @@ -30,11 +30,11 @@ def test_translator(self): assert _trace == 'builtin_list' def test_base_translator(self): - with transobj_assert(1, trans=BaseTranslator) as(obj, name): + with transobj_assert(1, trans=BaseTranslator) as (obj, name): assert obj == 1 assert name == 'default_object' - with transobj_assert(1, trans=BaseTranslator()) as(obj, name): + with transobj_assert(1, trans=BaseTranslator()) as (obj, name): assert obj == 1 assert name == 'default_object'