Skip to main content

A library for enforcing strict typing of class members and function parameters in Python.

Project description

Python Strict Typing Library

This is a library written in pure Python that enables runtime type-checking for classes and functions via the use of a single @strictly_typed decorator. Classes may enforce strict typing of their members and functions are able to have type-checked parameters and return values via type hints.

Installation

This library is available via PyPI. It may be installed with the following pip command:

pip install py-strict-typing

Usage

With Classes

Any class may declare itself as having strictly-typed members using the @strictly_typed decorator:

from strict_typing import *

@strictly_typed
class example_class:
    a: int
    b: int

print(example_class.a)    # None

Upon assignment, any member of an instance of the class that provides a type hint will be type checked:

example_inst = example_class()

example_inst.a = 123
print(example_inst.a)       # 123
example_inst.a = 'hello'    # results in TypeError

Any members that do not have a type hint will not be checked. Also note that static assignments at the time of class declaration cannot be checked.

from strict_typing import *

@strictly_typed
class example_class:
    a = 'hello'
    b: list = 123   # valid. static assignment at declaration is not checked

inst = example_class()
inst.a = 123        # valid. 'a' has no hint so it is not checked

With Functions

Any Python function may declare its parameters and return type as strictly-typed using the @strictly_typed decorator:

from strict_typing import *

@strictly_typed
def test(a: int, b: str) -> str:
    return str(a) + b

print(test(123, 'hello'))           # returns '123hello'
print(test(10.0, 'invalid'))        # results in TypeError

As with strictly-typed classes, any parameters that lack a hint will not be checked.

from strict_typing import *

@strictly_typed
def test(a, b: str) -> str:
    return str(a) + b

print(test(10.0, 'not invalid'))    # returns '10.0not invalid' as 'a' is no longer checked.

This rule also applies to the return type hint:

from strict_typing import *

@strictly_typed
def test(a: int):
    return a + 123

@strictly_typed
def test2(a: int) -> str:
    return a + 123

print(test(111))        # outputs '234'
print(test2(111))       # results in TypeError

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

py-strict-typing-0.1.1.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file py-strict-typing-0.1.1.tar.gz.

File metadata

  • Download URL: py-strict-typing-0.1.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for py-strict-typing-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0da5afb9431eed172f183c66ac8a946ab44690591aea77005e5cc70b3114e5d2
MD5 8e71cb060d7251f492ec39a5e8b4dd2b
BLAKE2b-256 f56b3f640879bf0e0fbe89c1889269270b8d53bfee86e63e61b352a70b6fd9ae

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