GithubHelp home page GithubHelp logo

vinibiavatti1 / overrides Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mkorpela/overrides

0.0 2.0 0.0 232 KB

A decorator to automatically detect mismatch when overriding a method

License: Apache License 2.0

Shell 0.34% Python 99.66%

overrides's Introduction

overrides

http://pepy.tech/badge/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, 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+.

$ pip install overrides

Usage

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

from overrides import overrides

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):

    @overrides
    def foo(self):
        return 2

    @overrides
    def bar(self, y) -> int: # Raises, because the signature is not compatible.
        return y

Use EnforceOverrides to require subclass methods that shadow superclass methods to be decorated with @overrides.

from overrides import EnforceOverrides

class SuperClass(EnforceOverrides):

    def foo(self):
        return 1

class SubClass(SuperClass):

    def foo(self): # Raises, because @overrides is missing.
        return 2

Use @final to indicate that a superclass method cannot be overriden.

from overrides import EnforceOverrides, final

class SuperClass(EnforceOveriddes):

    @final
    def foo(self):
        return 1

class SubClass(SuperClass):

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

Note that @classmethod and @staticmethod must be declared before @overrides.

from overrides import overrides

class SuperClass:

    @staticmethod
    def foo(x):
        return 1

class SubClass(SuperClass):

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

Flags of control

# To prevent all signature checks do:
@overrides(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 forward references
@overrides(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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.