File: README.rst

package info (click to toggle)
python-overrides 7.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 244 kB
  • sloc: python: 1,661; sh: 5; makefile: 2
file content (167 lines) | stat: -rw-r--r-- 5,105 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
overrides
=========

.. image:: https://img.shields.io/pypi/v/overrides.svg
  :target: https://pypi.python.org/pypi/overrides

.. image:: http://pepy.tech/badge/overrides
  :target: http://pepy.tech/project/overrides

A decorator ``@override`` that verifies that a method that should override an inherited method actually does it.

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.

Motivation
----------

Python has no standard mechanism by which to guarantee that (1) a method that previously overrode an inherited method
continues to do so, and (2) a method that previously did not override an inherited will not override now.
This opens the door for subtle problems as class hierarchies evolve over time. For example,

1. A method that is added to a superclass is shadowed by an existing method with the same name in a 
   subclass.

2. A method of a superclass that is overridden by a subclass is renamed in the superclass but not in 
   the subclass.

3. A method of a superclass that is overridden by a subclass is removed in the superclass but not in
   the subclass.

4. A method of a superclass that is overridden by a subclass but the signature of the overridden
   method is incompatible with that of the inherited one.

These can be only checked by explicitly marking method override in the code.

Python also has no standard mechanism by which to inherit docstrings in overridden methods. Because 
most standard linters (e.g., flake8) have rules that require all public methods to have a docstring, 
this inevitably leads to a proliferation of ``See parent class for usage`` docstrings on overridden
methods, or, worse, to a disabling of these rules altogether. In addition, mediocre or missing
docstrings degrade the quality of tooltips and completions that can be provided by an editor.

Installation
------------

Compatible with Python 3.6+.

.. code-block:: bash

    $ pip install overrides

Usage
-----

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

.. code-block:: python

    from overrides import override

    class SuperClass:

        def foo(self):
            """This docstring will be inherited by any method that overrides this!"""
            return 1

        def bar(self, x) -> str:
            return x

    class SubClass(SuperClass):

        @override
        def foo(self):
            return 2

        @override
        def bar(self, y) -> int: # Raises, because the signature is not compatible.
            return y
            
        @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 ``@override``.

.. code-block:: python
 
    from overrides import EnforceOverrides

    class SuperClass(EnforceOverrides):

        def foo(self):
            return 1

    class SubClass(SuperClass):

        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, override

    class SuperClass(EnforceOverrides):

        @final
        def foo(self):
            return 1

    class SubClass(SuperClass):

        @override
        def foo(self): # Raises, because overriding a final method is forbidden.
            return 2

Note that ``@classmethod`` and ``@staticmethod`` must be declared before ``@override``.

.. code-block:: python

    from overrides import override

    class SuperClass:

        @staticmethod
        def foo(x):
            return 1

    class SubClass(SuperClass):

        @staticmethod
        @override
        def foo(x):
            return 2


Flags of control
----------------

.. code-block:: python

    # To prevent all signature checks do:
    @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
    @override(check_at_runtime=True)
    def some_other_method(self, ..) -> "SomethingDefinedLater":
        pass

    a.some_other_method() # Kaboom if not SomethingDefinedLater


Contributors
------------

This project exists only through the work of all the people who contribute.

mkorpela, drorasaf, ngoodman90, TylerYep, leeopop, donpatrice, jayvdb, joelgrus, lisyarus, 
soulmerge, rkr-at-dbx, ashwin153, brentyi,  jobh, tjsmart, bersbersbers, LysanderGG, mgorny.