Skip to main content

A modern library for creating decorators. Static-typed.

Project description

Smart Decorator

A static-typed library for creating decorators.

from smart_decorator import simple_decorator

_events = {}

@simple_decorator
def register(callback, *, event_type: str = 'normal'):
    _events[event_type] = callback
    return callback


@register # Parentheses optional
def on_normal():
    print('an event happened!')

@register(event_type='error')
def on_error():
    print('an error!')

# can be used as a normal function, too
def on_close():
    print('closing!')

register(on_close, event_type='close')

Documentation

@simple_decorator

Turn a function into a smart decorator, as shown above. The decorator must accept a func, and some optional keyword arguments. Simple decorators must either return the func unchanged, or a wrapper with the same signature (see @decorator for signature morphing!).

@simple_decorator
def my_function(func, *, option1, option2 = ...):
    def wrapper(*args, **kwargs):
        ... # do something
        return func(*args, **kwargs)
    return wrapper

@decorator

The same as @simple_decorator but the decorator can return anything, such as a function with a different signature. Decorators declared this way should ideally be static-typed.

@decorator
def inject_foo[**P, R](
    func: Callable[Concatenate[str, P], R], # static-typing to inject a parameter
    *,
    what_to_inject: str = 'foo'
) -> Callable[P, R]:
    
    def wrapper_func(*args: P.args, **kwargs: P.kwargs) -> R:
        return func(what_to_inject, *args, **kwargs)

    return wrapper_func


@inject_foo
def add_stuff(what: str, cool: str) -> str:
    return what + cool

assert add_stuff('bar') == 'foobar'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smart_decorator-0.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smart_decorator-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file smart_decorator-0.1.0.tar.gz.

File metadata

  • Download URL: smart_decorator-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for smart_decorator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 697da5713803ff021f1e175c16149808bb00b5eddaee079b76b48c724fc5e60f
MD5 88169615b61a9c314fdb3dea1b5062c5
BLAKE2b-256 6d87569ab6133ee3c09f1e51f7233a381bd2cdd364b88c21b340a201ed273c5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for smart_decorator-0.1.0.tar.gz:

Publisher: python-publish.yml on mkzeender/smart_decorator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file smart_decorator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for smart_decorator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ec28b08470a58ec390cb12f1ca6d537509b0217673555a3cd15f5a042d0b512
MD5 cc1cfa0801fc952afa0a43607c3663fa
BLAKE2b-256 a5c1014bab820c7bc623e24d5984bb349a30f3ace87c07b0fce38cf8086b0a06

See more details on using hashes here.

Provenance

The following attestation bundles were made for smart_decorator-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on mkzeender/smart_decorator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page