GithubHelp home page GithubHelp logo

amulet-team / runtime-final Goto Github PK

View Code? Open in Web Editor NEW

This project forked from izxxr/runtime-final

0.0 1.0 0.0 36 KB

Declare final Python classes and methods at runtime.

License: MIT License

Python 100.00%

runtime-final's Introduction

amulet-runtime-final

Declare final Python classes and methods at runtime.

This module provides a decorator based interface to declare final classes and methods that are enforced at runtime. This module is inspired by and is compatible with typing.final. See PEP-591 for more details on this topic.

Installation

Python 3.6 or higher is required.

You can simply install this module from pip.

python -m pip install amulet-runtime-final

Usage

The main component of this module is the final decorator that can be used to decorate classes and methods inside a class. As such:

  • Classes decorated with @final cannot be subclassed.
  • Methods decorated with @final cannot be overriden in subclasses.

For example with classes:

from runtime_final import final

@final
class Foo:
    ...

class Bar(Foo):  # Raises RuntimeError
    ...

And with methods:

from runtime_final import final

class Foo:
    @final
    def foo(self):
        ...

class Bar(Foo):
    def foo(self):  # Raises RuntimeError
        ...

And with other decorators:

from runtime_final import final

class Foo:
    @final
    @property
    def foo(self):
        ...
    
    @final
    @staticmethod
    def bar():
        ...
    
    @final
    @classmethod
    def baz(cls):
        ...

class Bar(Foo):
    @property
    def foo(self):  # Raises RuntimeError
        ...

    @staticmethod
    def bar():  # Raises RuntimeError
        ...
    
    @classmethod
    def baz(cls):  # Raises RuntimeError
        ...

With property setters:

from runtime_final import final

class Foo:
    @property
    def foo(self):
        ...
    
    # Note that the `final` decorator must only be applied to the last definition.
    
    @final
    @foo.setter
    def foo(self, foo):
        ...

runtime-final's People

Contributors

gentlegiantjgc avatar izxxr avatar

Watchers

 avatar

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.