Skip to content

Commit

Permalink
Pass signature check if inspect.signature throws a ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
tjsmart authored and mkorpela committed Oct 4, 2022
1 parent c99bbfb commit 6116edb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
7 changes: 6 additions & 1 deletion overrides/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ def ensure_signature_is_compatible(
"""
super_callable = _unbound_func(super_callable)
sub_callable = _unbound_func(sub_callable)
super_sig = inspect.signature(super_callable)

try:
super_sig = inspect.signature(super_callable)
except ValueError:
return

super_type_hints = _get_type_hints(super_callable)
sub_sig = inspect.signature(sub_callable)
sub_type_hints = _get_type_hints(sub_callable)
Expand Down
34 changes: 30 additions & 4 deletions tests/test_overrides.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import sys
import unittest
from contextlib import contextmanager
from typing import Generic, TypeVar

import test_somepackage
Expand All @@ -7,6 +9,11 @@
TObject = TypeVar("TObject", bound="Foo")


@contextmanager
def no_error():
yield


class SubClassOfGeneric(Generic[TObject]):
def some_method(self):
"""Generic sub class."""
Expand Down Expand Up @@ -61,10 +68,6 @@ class SubclassOfInt(int):
def __str__(self):
return "subclass of int"

@overrides
def bit_length(self):
pass


class CheckAtRuntime(SuperClass):
@overrides(check_at_runtime=True)
Expand Down Expand Up @@ -146,6 +149,29 @@ def test_overrides_check_at_runtime(self):
with self.assertRaises(TypeError):
CheckAtRuntime().some_method(1)

def test_overrides_builtin_method_correct_signature(self):
class SubclassOfInt(int):
@overrides
def bit_length(self):
return super().bit_length()

x = SubclassOfInt(1)
self.assertEqual(x.bit_length(), 1)

def test_overrides_builtin_method_incorrect_signature(self):
if sys.version_info >= (3, 7):
expected_error = self.assertRaises(TypeError)
else:
# In 3.6 inspecting signature's isn't possible for builtin
# methods so this this passes the signature check.
expected_error = no_error()

with expected_error:
class SubclassOfInt(int):
@overrides
def bit_length(self, _):
"This will fail, bit_length takes in no arguments"


if __name__ == "__main__":
unittest.main()

0 comments on commit 6116edb

Please sign in to comment.