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.2.1

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.2.1.tar.gz (7.4 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: slightly-better-types-1.2.1.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.2.1.tar.gz
Algorithm Hash digest
SHA256 7c2dba4ee6f745c8be1e68374b8eb6cf2e93f42d366e3e1ed4825c56fefff5cf
MD5 f252c38beca3cae63490912649bceeb7
BLAKE2b-256 3c12344541ac33be678b3440dd848cc682235a88ff20fab4d5171fc6beb23a4c

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