EnforceTypes
Simple decorators for enforcing types during runtime.
The idea behind this is to reduce time spent developing and using packages that are computationally intensive by allowing for early raising and exiting rather than running for a long time and failing on type or class problems. Currently the easiest way to do this, which is most commonly seen in packages and modules that have type hints applied, is by adding isinstance checks and assert statements throughout the code. This quickly becomes repetitive, doesn’t help readability of the code and can easily be forgotten.
The decorators provided in this module resolve that problem by handling these type checks (as annotated) and raising when unexpected types are provided. They also aid in debugging and development by providing both the expected type and the provided argument’s runtime type. There are separate decorators for functions and classes, please do not use them the wrong way around.
Due to some very iffy results and instability in the way decorators work when combined, please try using decorators in a different order before submitting bug reports. I can’t help that, it’s a limitation with the way Python itself handles it.
I’m working on combining common use cases (such as @dataclass which has already been implemented) and would gladly take suggestions! I’ve also been putting some effort into splitting the package, so auto-imports and __all__ might be somewhat affected until v0.1.0, which I’ll release once I’m confident that it’s mostly stable (except for the API)
from EnforceTypes import classtypes, functypes, methtypes
@functypes
def add(a: int, b: int) -> None:
print(f"Adding {a} to {b} equals {a + b}")
add(2, 2) # prints "Adding 2 to 2 equals 4"
add("a", 2) # This causes the decorator to raise a TypeError!
@classtypes
class Add:
def __init__(a: int, b: int):
self.a = a
self.b = b
@property
def printadd(self):
print(f"Adding {self.a} to {self.b} equals {self.add()}")
@classmethod
@methtypes
def add_values(cls, *, a: int = 1, b: int = 1) -> int:
obj = cls(a, b)
return obj.add()
@methtypes
def add(self) -> int:
return self.a + self.b
Add(1, 1).printadd # prints 2
a = Add(1, "a") # TypeError raised because of a `str` instead of an `int`.
Add(1, "b").printadd # This causes a TypeError too, before instantiating an `Add` object!
Add.add_values(b=5) # returns 6
a = Add(10, 20)
a.add() # returns 30
Metadata
Release files for EnforceTypes 0.0.8
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 |
|---|---|---|---|---|
| EnforceTypes-0.0.8.tar.gz | 7.3 kB | Without provenance | Details for EnforceTypes-0.0.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| enforcetypes-0.0.8-py3-none-any.whl | Python 3 | none | any | Details for enforcetypes-0.0.8-py3-none-any.whl |
Total release size: 15.0 kB
Release files / EnforceTypes-0.0.8.tar.gz
| Download URL | EnforceTypes-0.0.8.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
29833905e0ba35f2a264e735780c653ab5d3e3fd19d69d22c02c8da817f04757
|
|
BLAKE2b-256 checksum How to use checksums |
7e929dcfc7950048ed9f7c907d2041e9c0bf99cd1fdafe64ed7410c169b8db6a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.1
|
Release files / enforcetypes-0.0.8-py3-none-any.whl
| Download URL | enforcetypes-0.0.8-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
33d80c7364b20da9603d731cee640d5e5c7eaa20517a6889e98309ff57721197
|
|
BLAKE2b-256 checksum How to use checksums |
f6f43e286ef8c4f2bd8216c65c07d431f264886388f77c75807366998b376cfc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.28.1
|
Security for EnforceTypes 0.0.8
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.