Skip to main content

Type corrector

Important

This project has merged with typesys and will no longer be maintained. Typesys can be found on github or pypi or it can be installed with pip install typesys.

Intro

Type corrector is module that contains the decorator type_corrector. This decorator lets the user specify what types the arguments to a function should have. It’s not 100% safe to use as it might result in a ValueError or TypeError if the user is not careful enough. The motivation behing this module was to find a way that makes it easier for the programmer to see what types the arguments should be, and at the same time allow some margin of error. I’m not sure if this is a good idea, or if it’s a good approach. It was mostly developed for fun while playing around with decorators.

Installation

pip install typecorrector

Usage

First import type_corrector from the typecorrector module:

from typecorrector import type_corrector

Then you can decorate your functions with type_corrector

@type_corrector(int, int)
def add(x,y):
    return x+y


@type_corrector(float, float)
def div(x,y):
    return x/y

A call to add(1,’2’) will cast ‘2’ to an int, since that is what we specified as the type of the second paramater in the decorator. We can also call div as div(‘10’, ‘3’), and div will return 3.3333333333333335 as expected.

This decorator also works with *args and **kwargs

@type_corrector(int)
def mult(*numbers):
    result = 1
    for num in numbers:
        result *= num
    return result


@type_corrector(int)
def kw_mult(**kwargs):
    first = kwargs.get('first')
    second = kwargs.get('second')
    third = kwargs.get('third')
    return first * second * third

This allows us to call the functions like this:

  • mult(‘2’, ‘3’, ‘4’)

  • kw_mult(first=’2’, second=’3’, third=’4’)

When looking at the function definitions of add, mult and kw_mult we can easily see that the arguments are supposed to be integers. By decorating the functions like this it should be a clear hint what types we want the parameters to be passed in as, even though it allows some margin of error.

Known issues

When calling help on a decorated function the parameters are not shown correctly, instead it will just say <function name>(*args, **kwargs) Also, when using inspect to get the argument specification with inspect.getargspec or getting the source code from inspect.getsourcelines it will fail. Thanks to the functools.wraps decorator the docstring of a wrapped function will still be shown correctly.

Metadata

Release files for typecorrector 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typecorrector 0.2.2
File Size Uploaded Provenance
typecorrector-0.2.2.tar.gz 3.3 kB Without provenance Details

Release files / typecorrector-0.2.2.tar.gz

Download URL typecorrector-0.2.2.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7adc0294b5da46d5548d0f6303bcbc11178ff885406bdfd64f19cdbd861c1b6f
BLAKE2b-256 checksum
How to use checksums
4dbd0eebd2299717c74644ddcbe49250611019a30f02aafd933bc2efd477f1aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for typecorrector 0.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page