Skip to content

Commit

Permalink
Pull superficial changes out of #352 (#358)
Browse files Browse the repository at this point in the history
Summary:
Pull superficial changes out of #352

Wanting to try out "upgrade typeguard to 3.X" (#352) and "upgrade typeguard to 4.X" in parallel, pulling some superficial parts out so that I can minimise distractions

Pull Request resolved: #358

Differential Revision: D55365453

Pulled By: shish

fbshipit-source-id: 7f09e9586cef2d810d069e7b723358f7f2f4a40f
  • Loading branch information
shish authored and facebook-github-bot committed Mar 26, 2024
1 parent f8bffd3 commit 8dfa5b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
21 changes: 7 additions & 14 deletions tests/lib_testslide.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def assert_passes(self, *args, **kwargs):
def assert_fails(self, *args, **kwargs):
with self.assertRaisesRegex(
testslide.lib.TypeCheckError,
"Call to "
+ self.callable_template.__name__
+ " has incompatible argument types",
f"Call to {self.callable_template.__name__} has incompatible argument types",
):
testslide.lib._validate_callable_arg_types(
self.skip_first_arg, self.callable_template, args, kwargs
Expand Down Expand Up @@ -208,15 +206,15 @@ def callable_template(self):
return sample_module.test_union

@context.example
def passes_with_StritMock_without_template(self):
def passes_with_StrictMock_without_template(self):
self.assert_passes({"StrictMock": StrictMock()})

@context.example("it works with unittest.mock.Mock without spec")
def passes_with_unittest_mock_Mock_without_spec(self):
self.assert_passes({"Mock": unittest.mock.Mock()})

@context.example
def passes_with_StritMock_with_valid_template(self):
def passes_with_StrictMock_with_valid_template(self):
self.assert_passes(
{"StrictMock(template=str)": StrictMock(template=str)}
)
Expand All @@ -226,7 +224,7 @@ def passes_with_unittest_mock_Mock_with_valid_spec(self):
self.assert_passes({"Mock(spec=str)": unittest.mock.Mock(spec=str)})

@context.example
def fails_with_StritMock_with_invalid_template(self):
def fails_with_StrictMock_with_invalid_template(self):
self.assert_fails(
{"StrictMock(template=dict)": StrictMock(template=dict)}
)
Expand All @@ -242,7 +240,7 @@ def callable_template(self):
return sample_module.test_tuple

@context.example
def passes_with_StritMock_without_template(self):
def passes_with_StrictMock_without_template(self):
self.assert_passes(
{
"StrictMock": (
Expand All @@ -264,7 +262,7 @@ def passes_with_unittest_mock_Mock_without_spec(self):
)

@context.example
def passes_with_StritMock_with_valid_template(self):
def passes_with_StrictMock_with_valid_template(self):
self.assert_passes(
{
"StrictMock(template=int)": (
Expand All @@ -286,7 +284,7 @@ def passes_with_unittest_mock_Mock_with_valid_spec(self):
)

@context.example
def fails_with_StritMock_with_invalid_template(self):
def fails_with_StrictMock_with_invalid_template(self):
self.assert_fails(
{
"StrictMock(template=dict)": (
Expand Down Expand Up @@ -366,11 +364,6 @@ def with_typevar_return() -> Type[TypeVar("T")]: # type: ignore[empty-body]
@context.example
def fails_for_wrong_type(self):
self.assert_fails(42)
# assert_regex = r"(?s)type of return must be .+; got .+ instead: .+Defined"
# with self.assertRaisesRegex(TypeError, assert_regex):
# testslide.lib._validate_return_type(
# self.callable_template, 42, self.caller_frame_info
# )

@context.example
def fails_for_mock_with_wrong_template(self):
Expand Down
13 changes: 7 additions & 6 deletions testslide/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ def _validate_callable_signature(
kwargs: Dict[str, Any],
) -> bool:
# python stdlib tests have to exempt some builtins for signature validation tests
# they use a giant alloy/deny list, which is impractical here so just ignore
# they use a giant allow/deny list, which is impractical here so just ignore
# all builtins.

# Ugly hack to make mock objects not be subclass of Mock
if _is_a_builtin(callable_template):
return False
if skip_first_arg and not inspect.ismethod(callable_template):
Expand Down Expand Up @@ -257,7 +259,6 @@ def _validate_callable_arg_types(
expected_type = argspec.annotations.get(argname)
if not expected_type:
continue

_validate_argument_type(expected_type, argname, args[idx])
except TypeCheckError as type_error:
type_errors.append(f"{repr(argname)}: {type_error}")
Expand All @@ -273,11 +274,11 @@ def _validate_callable_arg_types(
type_errors.append(f"{repr(argname)}: {type_error}")

if type_errors:
separator = "\n "
raise TypeCheckError(
"Call to "
+ callable_template.__name__
+ " has incompatible argument types:\n "
+ "\n ".join(type_errors)
f"Call to {callable_template.__name__} "
"has incompatible argument types:\n "
f"{separator.join(type_errors)}"
)


Expand Down

0 comments on commit 8dfa5b6

Please sign in to comment.