Skip to content

Commit

Permalink
don't require all types of the super
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Nov 28, 2023
1 parent d6c44a4 commit a323dff
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions spinn_utilities/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#: :meta private:
Method = TypeVar("Method", bound=Callable[..., Any])

# This near constant is changed by a few unittests to check our code
MATCH_ALL = False


class overrides(object):
"""
Expand Down Expand Up @@ -128,41 +131,47 @@ def __verify_method_types(self, method: Method):
super_types = get_type_hints(self._superclass_method)
except NameError:
return self.__verify_method_types_by_annotation(method)

if self._adds_typing:
for arg in super_types:
if method_types[arg] != super_types[arg]:
raise AttributeError(
f"Method {self._override_name} has {arg} "
f"typed as {method_types[arg]} "
f"while super has {super_types[arg]}")
else:
all_types = [
arg for arg in method_types
if arg not in self._additional_arguments]
if len(all_types) != len(super_types):
raise AttributeError(
f"Method has {len(method_types)} typed arguments but "
f"{self._override_name} has {len(super_types)}")
for arg in all_types:
if method_types[arg] != super_types[arg]:
if arg != "return" or not issubclass(
method_types[arg], super_types[arg]):
raise AttributeError(
f"Method {self._override_name} has {arg} typed as "
f"{method_types[arg]} "
f"while super has {super_types[arg]}")
return

all_types = [
arg for arg in method_types
if arg not in self._additional_arguments]

if MATCH_ALL and len(all_types) != len(super_types):
raise AttributeError(
f"Method has {len(method_types)} typed arguments but "
f"{self._override_name} has {len(super_types)}")
for arg in all_types:
if method_types[arg] != super_types[arg]:
if arg != "return" or not issubclass(
method_types[arg], super_types[arg]):
raise AttributeError(
f"Method {self._override_name} has {arg} typed as "
f"{method_types[arg]} "
f"while super has {super_types[arg]}")

def __verify_method_types_by_annotation(self, method: Method):
method_types = inspect.getfullargspec(method).annotations
super_types = inspect.getfullargspec(
self._superclass_method).annotations

all_types = [
arg for arg in method_types
if arg not in self._additional_arguments]
if len(all_types) != len(super_types):

if MATCH_ALL and len(all_types) != len(super_types):
raise AttributeError(
f"Method has {len(method_types)} typed arguments but "
f"{self._override_name} has {len(super_types)}")

for arg in all_types:
if method_types[arg] != super_types[arg]:
if isinstance(method_types[arg], type):
Expand Down

0 comments on commit a323dff

Please sign in to comment.