Pystrong. Type enforcement for python objects
Abstract
This package is inspired by the Python dataclasses. While dataclasses are amazing, I never really liked the way they go about type checking/enforcement. This package offers some clean and simple tools to help with type enforcement in Python.
Usage
BaseClasses
Use the TypeEnforcer class as a base class and the types you set for the attributes of your derived classes will be enforced throughout the life of the object. When creating the class Person, the first thing the constructor must do is call base classes constructor passing the all attributes with their intended type as key value pairs. Thereafter, the object should behave as normal.
from pystrong import TypeEnforcer
class Person(TypeEnforcer):
def __init__(self, name, age):
super().__init__(name=str, age=int)
self.name = name
self.age = age
p = Person('Jake', 32)
Instances of the person class now have a clean repr, and are type enforced.
p
Person(name:str='Jake', age:int=32)
p.name = 55
EnforcedTypeError: Can not assign type: <class 'int'> to attribute 'name'. Must be of type: <class 'str'>
EnforcedTypeError is thrown because the value of the name attribute on a Person object must always be a string.
One caveat with the TypeEnforcer is that you can not add attributes to the class after initializing it due to no type value being set. If you want a object that can have attributes added you can use the InferredTypeEnforcer. This base class will just take the initial value of any attribute infer the type, and treat that as the type to enforce.
from pystrong import InferredTypeEnforcer
class Person(InferredTypeEnforcer):
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("Jake", 32)
p.height = 34.65
p
Person(name:str='Jake', age:int=32, height:float=34.65)
p.name = 55
EnforcedTypeError: Can not assign type: <class 'int'> to attribute 'name'. Must be of type: <class 'str'>
Decorators
There are also some handy decorators to ensure that a method arguments are the right type, or that the method returns the proper type. Using the check_return_types decorator will throw an error if the method returns an invalid type.
from pystrong import check_return_types
@check_return_types(str, int)
def test_success():
return "Hello World", 500
@check_return_types(str, int)
def test_fail():
return 500, {"test": "error"}
test_success()
("Hello World", 500)
test_fail()
EnforcedReturnTypeError: Function 'test_fail' returned '500' of type '<class 'int'>'. Expected return type is '<class 'str'>'.
The check_arg_type decorator can be used to enforce that arguments to the function are of the proper type.
from pystrong import check_arg_type
@check_arg_type(int, str)
def add_10(num, string):
print(string)
return num+10
@check_arg_type(int, str)
def error(num, string):
print(string)
return "error"
add_10(5, "Hello World")
Hello World
15
add_10("throw", "error")
EnforcedArgTypeError: Argument 'throw' must be of type <class 'int'>
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please update tests accordingly.
License
Metadata
Release files for PyStrong 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| PyStrong-0.0.7.tar.gz | 5.1 kB | Without provenance | Details for PyStrong-0.0.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| PyStrong-0.0.7-py3-none-any.whl | Python 3 | none | any | Details for PyStrong-0.0.7-py3-none-any.whl |
Total release size: 11.1 kB
Release files / PyStrong-0.0.7.tar.gz
| Download URL | PyStrong-0.0.7.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0dd4b509238d4fda86ead9d635d0f1c1e2665441fdda736e85d1aa40d2631e42
|
|
BLAKE2b-256 checksum How to use checksums |
f2b0d8ac354991a1570ab0c0b5c4e1fd5da3afeea06ec9b21741ecf85c2f55f3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
|
Release files / PyStrong-0.0.7-py3-none-any.whl
| Download URL | PyStrong-0.0.7-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4f65d65e93c9c62a4d0b8e95921c9db60ce7f1bc1bbd4d20fa22bd3400f4d5ec
|
|
BLAKE2b-256 checksum How to use checksums |
9e73e776a76136a7571cac7378e9e2a7f0915dfc86c03dff0196a49be5b49c9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10
|
Security for PyStrong 0.0.7
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.