Skip to content

Commit

Permalink
Add python 3.10 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex0Blackwell committed Oct 2, 2023
1 parent 01e96c6 commit 4384c74
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
12 changes: 8 additions & 4 deletions python_typing/strict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import typing
from copy import deepcopy
from inspect import _empty, getfullargspec, signature
from inspect import _empty, getfullargspec, isclass, signature


class Typing:
Expand Down Expand Up @@ -53,6 +53,10 @@ def check_return_type(self, func, return_value):
# impact users of this library
pass

def _format_type(self, type):
# Get rid of <class ...> repr
return type.__name__ if isclass(type) else type

def _assert_type_helper(self, value, expected_types):
multiple_valid_types = False
is_an_expected_type = False
Expand All @@ -75,10 +79,10 @@ def _assert_type_helper(self, value, expected_types):
msg = ""
if self._return_type is not None:
msg += "return "
return_type = getattr(self._return_type, "__name__", self._return_type)
return_type = self._format_type(self._return_type)
func_sig = f'"{self._func_name}() -> {return_type}" '
else:
arg_type = getattr(self._arg_type, "__name__", self._arg_type)
arg_type = self._format_type(self._arg_type)
func_sig = f'"{self._func_name}({self._arg_name}={arg_type})" '

shortened_value = str(value)[:10] + (str(value)[10:] and "..")
Expand All @@ -101,7 +105,7 @@ def _assert_structure(self, structure, expected_structure):
expected_structure, "__origin__", expected_structure
)
if structure is not expected_structure_type:
arg_type = getattr(self._arg_type, "__name__", self._arg_type)
arg_type = self._format_type(self._arg_type)
msg = (
f"Expected type {arg_type} in "
f'"{self._func_name}({self._arg_name}={arg_type})" '
Expand Down
3 changes: 2 additions & 1 deletion python_typing_tests/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def _func(_: Optional[str]):

with pytest.raises(TypeError) as err:
_func()
assert str(err.value) == "_func() missing 1 required positional argument: '_'"
# Assert "in" for python 3.10+ compatibility
assert "_func() missing 1 required positional argument: '_'" in str(err.value)


def test_arg_optional_give_none():
Expand Down
11 changes: 8 additions & 3 deletions python_typing_tests/test_strict.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def _func():

with pytest.raises(TypeError) as err:
_func("arg")
assert str(err.value) == "_func() takes 0 positional arguments but 1 was given"
# Assert "in" for python 3.10+ compatibility
assert "_func() takes 0 positional arguments but 1 was given" in str(err.value)


def test_arg_pass_nothing():
Expand All @@ -30,7 +31,8 @@ def _func(_):

with pytest.raises(TypeError) as err:
_func()
assert str(err.value) == "_func() missing 1 required positional argument: '_'"
# Assert "in" for python 3.10+ compatibility
assert "_func() missing 1 required positional argument: '_'" in str(err.value)


def test_arg_no_type():
Expand Down Expand Up @@ -184,4 +186,7 @@ def _func(_: Callable):

with pytest.raises(TypeError) as err:
_func(1)
assert str(err.value) == 'Value (1) in "_func(_=typing.Callable)" is not of type Callable'
assert (
str(err.value)
== 'Value (1) in "_func(_=typing.Callable)" is not of type Callable'
)

0 comments on commit 4384c74

Please sign in to comment.