-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add python < 37 compatible type checks
- Loading branch information
1 parent
662d05a
commit f478771
Showing
7 changed files
with
72 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |