Package: overrides
A decorator@overridethat 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. Seehttps://stackoverflow.com/q/1167617for the
initial inspiration for this library.MotivationPython 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,A method that is added to a superclass is shadowed by an existing method with the same name in a
subclass.A method of a superclass that is overridden by a subclass is renamed in the superclass but not in
the subclass.A method of a superclass that is overridden by a subclass is removed in the superclass but not in
the subclass.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 ofSee parent class for usagedocstrings 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.InstallationCompatible with Python 3.6+.$pipinstalloverridesUsageUse@overrideto indicate that a subclass method should override a superclass method.fromoverridesimportoverrideclassSuperClass:deffoo(self):"""This docstring will be inherited by any method that overrides this!"""return1defbar(self,x)->str:returnxclassSubClass(SuperClass):@overridedeffoo(self):return2@overridedefbar(self,y)->int:# Raises, because the signature is not compatible.returny@overridedefzoo(self):# Raises, because does not exist in the super class.return"foobarzoo"UseEnforceOverridesto require subclass methods that shadow superclass methods to be decorated
with@override.fromoverridesimportEnforceOverridesclassSuperClass(EnforceOverrides):deffoo(self):return1classSubClass(SuperClass):deffoo(self):# Raises, because @override is missing.return2Use@finalto indicate that a superclass method cannot be overriden.
With Python 3.11 and above@finalis directlytyping.final.fromoverridesimportEnforceOverrides,final,overrideclassSuperClass(EnforceOverrides):@finaldeffoo(self):return1classSubClass(SuperClass):@overridedeffoo(self):# Raises, because overriding a final method is forbidden.return2Note that@classmethodand@staticmethodmust be declared before@override.fromoverridesimportoverrideclassSuperClass:@staticmethoddeffoo(x):return1classSubClass(SuperClass):@staticmethod@overridedeffoo(x):return2Flags of control# To prevent all signature checks do:@override(check_signature=False)defsome_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)defsome_other_method(self,..)->"SomethingDefinedLater":passa.some_other_method()# Kaboom if not SomethingDefinedLaterContributorsThis 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.