Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade typeguard #126

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions deal/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ def _check_result(self, result: Any) -> None:
import typeguard
except ImportError:
return
memo = typeguard._CallMemo(
func=self.func,
args=self.args,
kwargs=self.kwargs,
memo = typeguard.CallMemo(
func=self.func, # type: ignore[arg-type]
frame_locals=self.kwargs,
)
typeguard.check_argument_types(memo=memo)
typeguard.check_return_type(result, memo=memo)
from typeguard._functions import check_argument_types, check_return_type

if not self.args and self.kwargs:
check_argument_types(memo=memo)
check_return_type(result, memo=memo)


class cases: # noqa: N
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ all = [
"deal-solver",
"hypothesis",
"pygments",
"typeguard",
"typeguard>=3.0.0",
"vaa>=0.2.1",
]
integration = [ # integration tests
"astroid>=2.11.0",
"deal-solver",
"hypothesis",
"pygments",
"typeguard<=2.13.3",
"typeguard",
"vaa>=0.2.1",
"sphinx>=4.5.0",
"flake8",
Expand Down
38 changes: 31 additions & 7 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from typing import NoReturn, TypeVar

import pytest
Expand All @@ -13,6 +14,11 @@
import hypothesis.errors
import hypothesis.strategies

try:
import typeguard
except ImportError:
typeguard = None


@deal.raises(ZeroDivisionError)
def div1(a: int, b: int) -> float:
Expand All @@ -37,11 +43,11 @@ def div2(a: int, b: int) -> float:


@pytest.mark.skipif(hypothesis is None, reason='hypothesis is not installed')
def test_short_version_is_discoverable():
def test_short_version_is_discoverable(monkeypatch):
from _pytest.python import PyCollector

collector = PyCollector.__new__(PyCollector)
collector._matches_prefix_or_glob_option = lambda *args: True
monkeypatch.setattr(collector, '_matches_prefix_or_glob_option', lambda *args: True)
test = deal.cases(div1)
assert collector.istestfunction(test, 'test_div') is True

Expand Down Expand Up @@ -104,7 +110,8 @@ def div(a: int, b: int) -> int:
def div(a: int, b: int) -> str: # type: ignore[no-redef]
return 1 # type: ignore[return-value]

with pytest.raises(TypeError):
msg = re.escape('the return value (int) is not an instance of str')
with pytest.raises(typeguard.TypeCheckError, match=msg):
case = next(iter(deal.cases(div, count=20)))
case()

Expand Down Expand Up @@ -140,8 +147,8 @@ def bad(a: int) -> str:
# type is wrong and checked
cases = deal.cases(bad, count=1)
case = next(iter(cases))
msg = 'type of the return value must be str; got int instead'
with pytest.raises(TypeError, match=msg):
msg = re.escape('the return value (int) is not an instance of str')
with pytest.raises(typeguard.TypeCheckError, match=msg):
case()

# type is wrong and ignored
Expand All @@ -159,6 +166,7 @@ def good(a: int) -> int:


@pytest.mark.skipif(hypothesis is None, reason='hypothesis is not installed')
@pytest.mark.skipif(typeguard is None, reason='typeguard is not installed')
def test_return_type():
def identity(a) -> int:
return a
Expand All @@ -168,8 +176,24 @@ def identity(a) -> int:
case()

case = deal.TestCase(args=('hi', ), **kwargs)
msg = 'type of the return value must be int; got str instead'
with pytest.raises(TypeError, match=msg):
msg = re.escape('the return value (str) is not an instance of int')
with pytest.raises(typeguard.TypeCheckError, match=msg):
case()


@pytest.mark.skipif(hypothesis is None, reason='hypothesis is not installed')
@pytest.mark.skipif(typeguard is None, reason='typeguard is not installed')
def test_typecheck_explicit_kwargs():
def identity(a: int) -> str:
return 'ok'

kwargs: dict = dict(args=(), func=identity, exceptions=(), check_types=True)
case = deal.TestCase(kwargs={'a': 13}, **kwargs)
case()

case = deal.TestCase(kwargs={'a': 'hi'}, **kwargs)
msg = re.escape('argument "a" (str) is not an instance of int')
with pytest.raises(typeguard.TypeCheckError, match=msg):
case()


Expand Down