Skip to content

Commit

Permalink
Use defacto @OverRide as the wording instead of @OVERRIDES
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorpela committed Oct 15, 2022
1 parent 9c4eae3 commit 5d67cab
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 96 deletions.
37 changes: 20 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ overrides
.. image:: http://pepy.tech/badge/overrides
:target: http://pepy.tech/project/overrides

A decorator that verifies that a method that should override an inherited method actually does, and
that copies the docstring of the inherited method to the overridden method. Since signature
validation and docstring inheritance are performed on class creation and not on class instantiation,
A decorator ``@override`` that verifies that a method that should override an inherited method actually does, and
that.

Copies the docstring of the inherited method to the overridden method.

Since signature validation and docstring inheritance are performed on class creation and not on class instantiation,
this library significantly improves the safety and experience of creating class hierarchies in
Python without significantly impacting performance. See https://stackoverflow.com/q/1167617 for the
initial inspiration for this library.
Expand Down Expand Up @@ -53,11 +56,11 @@ Compatible with Python 3.6+.
Usage
-----

Use ``@overrides`` to indicate that a subclass method should override a superclass method.
Use ``@override`` to indicate that a subclass method should override a superclass method.

.. code-block:: python
from overrides import overrides
from overrides import override
class SuperClass:
Expand All @@ -70,20 +73,20 @@ Use ``@overrides`` to indicate that a subclass method should override a supercla
class SubClass(SuperClass):
@overrides
@override
def foo(self):
return 2
@overrides
@override
def bar(self, y) -> int: # Raises, because the signature is not compatible.
return y
@overrides
@override
def zoo(self): # Raises, because does not exist in the super class.
return "foobarzoo"
Use ``EnforceOverrides`` to require subclass methods that shadow superclass methods to be decorated
with ``@overrides``.
with ``@override``.

.. code-block:: python
Expand All @@ -96,15 +99,15 @@ with ``@overrides``.
class SubClass(SuperClass):
def foo(self): # Raises, because @overrides is missing.
def foo(self): # Raises, because @override is missing.
return 2
Use ``@final`` to indicate that a superclass method cannot be overriden.
With Python 3.11 and above ``@final`` is directly `typing.final <https://docs.python.org/3.11/library/typing.html#typing.final>`_.

.. code-block:: python
from overrides import EnforceOverrides, final
from overrides import EnforceOverrides, final, override
class SuperClass(EnforceOverrides):
Expand All @@ -114,15 +117,15 @@ With Python 3.11 and above ``@final`` is directly `typing.final <https://docs.py
class SubClass(SuperClass):
@overrides
@override
def foo(self): # Raises, because overriding a final method is forbidden.
return 2
Note that ``@classmethod`` and ``@staticmethod`` must be declared before ``@overrides``.
Note that ``@classmethod`` and ``@staticmethod`` must be declared before ``@override``.

.. code-block:: python
from overrides import overrides
from overrides import override
class SuperClass:
Expand All @@ -133,7 +136,7 @@ Note that ``@classmethod`` and ``@staticmethod`` must be declared before ``@over
class SubClass(SuperClass):
@staticmethod
@overrides
@override
def foo(x):
return 2
Expand All @@ -144,12 +147,12 @@ Flags of control
.. code-block:: python
# To prevent all signature checks do:
@overrides(check_signature=False)
@override(check_signature=False)
def some_method(self, now_this_can_be_funny_and_wrong: str, what_ever: int) -> "Dictirux":
pass
# To do the check only at runtime and solve some forward reference problems
@overrides(check_at_runtime=True)
@override(check_at_runtime=True)
def some_other_method(self, ..) -> "SomethingDefinedLater":
pass
Expand Down
4 changes: 2 additions & 2 deletions mypy_fails/wrong_signature.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from overrides import overrides
from overrides import override


class Parent:
Expand All @@ -7,6 +7,6 @@ def metodi(self, x: int) -> str:


class Child(Parent):
@overrides
@override
def metodi(self, x: str) -> int:
return int(x)
4 changes: 2 additions & 2 deletions mypy_fails/wrong_signature_forward_ref.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from overrides import overrides
from overrides import override


class Parent:
Expand All @@ -7,6 +7,6 @@ def metoda(self) -> None:


class Child(Parent):
@overrides
@override
def metoda(self) -> "Child":
return self
4 changes: 2 additions & 2 deletions mypy_passes/passing_override.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Tuple

from overrides import overrides
from overrides import override


class Parent:
Expand All @@ -9,6 +9,6 @@ def execute(self, x: int, y: str, z: bool, *args, **kwargs) -> Tuple[str, int]:


class Child(Parent):
@overrides
@override
def execute(self, *args, **kwargs) -> Tuple[str, int]:
return "moi", 1
2 changes: 2 additions & 0 deletions overrides/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
else:
from typing import final
from overrides.overrides import __VERSION__, overrides
override = overrides

__all__ = [
"__VERSION__",
"override",
"overrides",
"final",
"EnforceOverrides",
Expand Down
2 changes: 1 addition & 1 deletion overrides/enforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _check_if_overrides_without_overrides_decorator(name, value, bases):
continue
if not is_override:
raise TypeError(
f"Method {name} overrides method from {base} but does not have @overrides decorator"
f"Method {name} overrides method from {base} but does not have @override decorator"
)

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions overrides/final.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def final(method: _WrappedMethod) -> _WrappedMethod:
"""Decorator to indicate that the decorated method is finalized and cannot be overridden.
The decorator code is executed while loading class. Using this method
should have minimal runtime performance implications.
Currently, only methods with @overrides are checked.
Currently, only methods with @override are checked.
How to use:
from overrides import final
Expand All @@ -34,7 +34,7 @@ def method(self):
return 2
class SubClass(SuperClass):
@overrides
@override
def method(self): #causes an error
return 1
Expand Down
2 changes: 1 addition & 1 deletion overrides/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def method(self):
class SubClass(SuperClass):
@overrides
@override
def method(self):
return 1
Expand Down
4 changes: 2 additions & 2 deletions tests/test_decorator_params.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Callable, Type
from overrides import overrides
from overrides import override


class SuperClass:
Expand All @@ -24,6 +24,6 @@ def test_my_func() -> None:

@my_decorator(my_object.my_name)
class SubClass(SuperClass):
@overrides
@override
def method(self) -> int:
return 1
12 changes: 6 additions & 6 deletions tests/test_enforce__py38.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from typing import Optional, TypeVar, Union

from overrides import EnforceOverrides, final, overrides
from overrides import EnforceOverrides, final, override
from overrides.signature import ensure_signature_is_compatible


Expand Down Expand Up @@ -40,7 +40,7 @@ def test_enforcing_when_all_ok(self):
class Subclazz(Enforcing):
classVariableIsOk = "OK!"

@overrides
@override
def nonfinal1(self, param: int) -> str:
return "2"

Expand Down Expand Up @@ -74,7 +74,7 @@ def nonfinal2(self):
def test_enforcing_when_property_overriden(self):
class PropertyOverrider(Enforcing):
@property
@overrides
@override
def nonfinal_property(self):
return "subclass_property"

Expand All @@ -86,14 +86,14 @@ def test_enforcing_when_incompatible(self):
with self.assertRaises(TypeError):

class Incompatible(Enforcing):
@overrides
@override
def nonfinal1(self, param: str):
pass

def test_enforcing_when_staticmethod_overriden(self):
class StaticMethodOverrider(Enforcing):
@staticmethod
@overrides
@override
def nonfinal_staticmethod():
return "subclass_staticmethod"

Expand All @@ -105,7 +105,7 @@ def nonfinal_staticmethod():
def test_enforcing_when_classmethod_overriden(self):
class ClassMethodOverrider(Enforcing):
@classmethod
@overrides
@override
def nonfinal_classmethod(cls):
return "subclass_classmethod"

Expand Down

0 comments on commit 5d67cab

Please sign in to comment.