Skip to main content

statictypes

statictypes helps your Python functions do exactly what you asked them to do, and not anything else. This package delivers three simple decorators:

  • @statictypes.enforce: Raise an error if the incorrect argument or return type is given.
  • @statictypes.warn: Give a warning if the incorrect argument or return type is given.
  • @statictypes.convert: Try to convert into the given argument type, or return an error.

Example 1

Let's define a simple function, and enforce Python's type annotations.

import statictypes

@statictypes.enforce
def myfunc(text: str, number: int) -> str:
    return text + " " + str(number)

myfunc("my number is", 1)  # This works as intended
myfunc("my number is", 1.1)  # This raises an error

Calling myfunc("my number is", 1) is valid, but myfunc("my number is", 1.1) results in:

statictypes.StaticTypeError: Argument 'number' got incorrect type <class'float'>, expected <class'int'>.

Example 2

Let's instead choose to convert the arguments to the given type annotations.

import statictypes

@statictypes.convert
def myfunc(text: str, number: int) -> str:
    return text + " " + str(number)

myfunc("my number is", 1)  # This works as intended
myfunc("my number is", 1.1)  # This gives the same output as above

This time, both myfunc("my number is", 1) and myfunc("my number is", 1.1) is valid. Note, however, that both expressions give the output "my number is 1", since number is in this case always converted to an integer.

Limitations

  1. @statictypes.convert only works for simple types from where a constructor method can be called, e.g. str, int. The decorator does not work on e.g. List[float] -> List[int] since the following incorrect conversion is attempted: List[int](list_of_floats).
  2. Only the builtin generic types Any, Union, Optional, Dict, List and Tuple are currently supported.
  3. When using @statictypes.convert, if a list is given to a function expecting a numpy.ndarray , an empty numpy.ndarray is returned with the shape of the list. This is because the numpy.ndarray has shape as its first positional argument, leading to a working but arguably unexpected result.

Requirements

  • Python 3.6 or above.

Installation

pip install statictypes

Metadata

Release files for statictypes 0.0.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 statictypes 0.0.2
File Size Uploaded Provenance
statictypes-0.0.2.tar.gz 3.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for statictypes 0.0.2
File Interpreter ABI Platform
statictypes-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.8 kB

Release files / statictypes-0.0.2.tar.gz

Download URL statictypes-0.0.2.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
56e4810fca3d5890b013874957f4b9d05e1e61c1e65184e28f6f8a15846235c6
BLAKE2b-256 checksum
How to use checksums
2da12d2fecfeb4e4169e80f5133da97b6320e84a26c49d2e841589653626ef90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

Release files / statictypes-0.0.2-py3-none-any.whl

Download URL statictypes-0.0.2-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22072b51df34c90da4db237eba52820a7c3565b79e545ac16e84e0170a504132
BLAKE2b-256 checksum
How to use checksums
8e83786fe4a9d607ccda76b1c3f3c60bf4fb90d37055bef2e8253e2133ff82d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6

Security for statictypes 0.0.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.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

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