Skip to content

Commit

Permalink
feat: add python < 37 compatible type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jnoortheen committed Apr 13, 2020
1 parent 662d05a commit f478771
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 20 deletions.
3 changes: 2 additions & 1 deletion arger/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from typing import Any, List, Set, Tuple

from arger.parser.docstring import parse_docstring
from arger.utils import match_types, portable_argspec
from arger.typing_utils import match_types
from arger.utils import portable_argspec


def generate_options():
Expand Down
22 changes: 22 additions & 0 deletions arger/typing_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys


NEW_TYPING = sys.version_info[:3] >= (3, 7, 0) # PEP 560


def _origin(tp):
"""Return x.__origin__ or type(x) based on the Python version."""
if hasattr(tp, '_gorg'):
return tp._gorg
if getattr(tp, "__origin__", None) is not None:
return tp.__origin__
return tp


def match_types(tp, *matches) -> bool:
"""Match the given type to list of other types.
:param tp:
:param matches:
"""
return any([_origin(m) is _origin(tp) for m in matches])
17 changes: 0 additions & 17 deletions arger/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import inspect
from typing import Any, List, Tuple


def portable_argspec(func):
Expand Down Expand Up @@ -32,19 +31,3 @@ def portable_argspec(func):
kw_params,
annotations,
)


def match_types(type, *matches):
"""Match the given type to list of other types.
:param type:
:param matches:
>>> match_types(List, List[str], List, Tuple, Tuple[str], Tuple[Any])
True
>>> match_types(List[str], List[Any])
True
>>> match_types(List[str], List)
True
"""
return any([str(m).startswith(str(type)) for m in matches])
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ tox = "^3.14.5"
pyinotify = "^0.9.6"
beautifulsoup4 = "^4.9.0"
mistune = "2.0.0a2"
pytest-lazy-fixture = "^0.6.3"

[tool.black]

Expand Down
1 change: 0 additions & 1 deletion tests/test_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pathlib import Path
from typing import Any, Callable, List

import pytest

Expand Down
31 changes: 31 additions & 0 deletions tests/test_typing_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Any, List, Tuple

from pytest import fixture, mark
from pytest_lazyfixture import lazy_fixture as lz

from arger.typing_utils import match_types


@fixture(params=[list, List, List[str], List[int], List[Any]])
def list_type(request):
return request.param


@fixture(
params=[tuple, Tuple, Tuple[str], Tuple[str, str], Tuple[int], Tuple[int, ...]]
)
def tuple_type(request):
return request.param


@mark.parametrize(
'tp, tps, expected',
[
(lz('list_type'), lz('list_type'), True),
(lz('list_type'), lz('tuple_type'), False),
(lz('tuple_type'), lz('tuple_type'), True),
(lz('tuple_type'), lz('list_type'), False),
],
)
def test_match_types(tp, tps, expected):
assert match_types(tp, tps) == expected

0 comments on commit f478771

Please sign in to comment.