Skip to main content

Improved abstraction for dealing with union and named types.

Project description

slightly-better-types

... arguably

Improved abstraction for dealing with union and named types.

Installation

pip install slightly-better-types==1.0.0

Usage

Using the Parameter class directly

from inspect import signature
from typing import get_type_hints
from slightly_better_types.parameter import Parameter


class Foo:
    pass


def accepts_string(a: Foo):
    pass


signature = signature(accepts_string)
type_hints = get_type_hints(get_type_hints(accepts_string))

parameter = list((signature.parameters.values()))[0]
parameter = Parameter(parameter=parameter, type_hint=type_hints.get(parameter.name))

parameter.accepts(1)  # False
parameter.accepts(Foo())  # True

Using the Function class:

from slightly_better_types.function import Function


def accepts_string(a: str, b: int):
    pass


sb_function = Function(accepts_string)

sb_function.accepts('string', 1)  # True
sb_function.accepts(1, 'string')  # False
sb_function.accepts('string')  # False

Using Handlers to determine which methods accept a given set of input:

from slightly_better_types.handlers import Handlers


class Bar:
    def accepts_string(self, a: str):
        pass

    def accepts_string_too(self, a: str):
        pass

    def accepts_int(self, a: int):
        pass


handlers = Handlers(Bar)
list(handlers.accepts('a string').all().keys())  # ['accepts_string', 'accepts_string_too']
list(handlers.accepts(1).first().get_name())  # 'accepts_int'

Tests

python3 -m unittest

License

The MIT License (MIT). Please see License File for more information.

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

slightly-better-types-1.1.0.tar.gz (7.4 kB view details)

Uploaded Source

File details

Details for the file slightly-better-types-1.1.0.tar.gz.

File metadata

  • Download URL: slightly-better-types-1.1.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for slightly-better-types-1.1.0.tar.gz
Algorithm Hash digest
SHA256 9a7ddadfcc5bacb04c4361587417d51a64a8923a536cbb60e61551ac6294acbd
MD5 55ec6ae13cd8e714d527d2f3ff32ac16
BLAKE2b-256 d41e94544579590af7d9fccc1a2d5ef0fb4fcccc0555ada12501c26e7d15b5a9

See more details on using hashes here.

Supported by

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