Skip to content

Commit

Permalink
black
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorpela committed Oct 9, 2022
1 parent 89ca2d2 commit 2473634
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
16 changes: 9 additions & 7 deletions overrides/enforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,25 @@ def _check_if_overrides_without_overrides_decorator(name, value, bases):
for base in bases:
base_class_method = getattr(base, name, False)
if (
not base_class_method
or not callable(base_class_method)
or getattr(base_class_method, "__ignored__", False)
not base_class_method
or not callable(base_class_method)
or getattr(base_class_method, "__ignored__", False)
):
continue
if not is_override:
raise TypeError(f"Method {name} overrides but does not have @overrides decorator")
raise TypeError(
f"Method {name} overrides but does not have @overrides decorator"
)

@staticmethod
def _check_if_overrides_final_method(name, bases):
for base in bases:
base_class_method = getattr(base, name, False)
# `__finalized__` is added by `@final` decorator
if getattr(base_class_method, "__finalized__", False):
raise TypeError(
f"Method {base_class_method} is finalized in {base}, it cannot be overridden"
)
f"Method {name} is finalized in {base}, it cannot be overridden"
)

@staticmethod
def _handle_special_value(value):
Expand All @@ -50,7 +53,6 @@ def _handle_special_value(value):
return value



class EnforceOverrides(metaclass=EnforceOverridesMeta):
"Use this as the parent class for your custom classes"
pass
4 changes: 3 additions & 1 deletion overrides/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ def method(self):


def _overrides(
method: _WrappedMethod, check_signature: bool, check_at_runtime: bool,
method: _WrappedMethod,
check_signature: bool,
check_at_runtime: bool,
) -> _WrappedMethod:
setattr(method, "__override__", True)
global_vars = getattr(method, "__globals__", None)
Expand Down
8 changes: 6 additions & 2 deletions overrides/typing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ def _is_origin_subtype(left: OriginType, right: OriginType) -> bool:


NormalizedTypeArgs = typing.Union[
typing.Tuple[typing.Any, ...], typing.FrozenSet[NormalizedType], NormalizedType,
typing.Tuple[typing.Any, ...],
typing.FrozenSet[NormalizedType],
NormalizedType,
]


Expand Down Expand Up @@ -416,7 +418,9 @@ def _is_normal_subtype(


def issubtype(
left: Type, right: Type, forward_refs: typing.Optional[dict] = None,
left: Type,
right: Type,
forward_refs: typing.Optional[dict] = None,
) -> typing.Optional[bool]:
"""Check that the left argument is a subtype of the right.
For unions, check if the type arguments of the left is a subset of the right.
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
author_email=address,
url="https://github.com/mkorpela/overrides",
packages=find_packages(),
package_data={"overrides": ["*.pyi", "py.typed"],},
package_data={
"overrides": ["*.pyi", "py.typed"],
},
include_package_data=True,
install_requires=['typing;python_version<"3.5"'],
python_requires=">=3.6",
Expand Down
5 changes: 3 additions & 2 deletions tests/test_enforce__py38.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Enforcing(EnforceOverrides):
def finality(self):
return "final"


@final
def __and__(self, other):
return True
Expand Down Expand Up @@ -53,23 +52,25 @@ def nonfinal1(self, param: int) -> str:

def test_enforcing_when_finality_broken(self):
with self.assertRaises(TypeError):

class BrokesFinality(Enforcing):
def finality(self):
return "NEVER HERE"

def test_trying_to_override_final_magic_method(self):
with self.assertRaises(TypeError):

class FinalMagicOverrides(Enforcing):
def __and__(self, other):
return False

def test_enforcing_when_none_explicit_override(self):
with self.assertRaises(TypeError):

class Overrider(Enforcing):
def nonfinal2(self):
return "NEVER HERE EITHER"


def test_enforcing_when_property_overriden(self):
class PropertyOverrider(Enforcing):
@property
Expand Down

0 comments on commit 2473634

Please sign in to comment.