Classes to create strongly typed structures in Python
Project description
strong_typing is a Python package containing some classes to create strongly typed structures in Python
strong_typing in a few words
In Python, all variables are weakly typed, which means that a variable can take all values of any type. The Python interpreter will then infer at runtime which operations this variable can undergo depending on what it contains. This is called “type inference”.
This can be a problem in different situations
A function that does not receive the expected type as input
A variable or a class attribute whose type is changed through assignment
To avoid functions being called with bad arguments, you can use Python’s typing module) (however only with Python3). To check if a variable is not incorrectly used, you can install and run mypy module).
But if the latest is great for static check (without running the code), it does not work on the code you don’t own.
If, for instance you design a class expecting a certain type of attributes, mypy can very easily detect if you don’t mistakenly override these attributes with wrong typed data.
But if you put this class in a Python package and that someone else uses it, there is no way to be sure they will respect your attribute’s type.
To make sure they do, you would need to define a descriptor’s class for each attribute and define a setter function protecting your value against abusive set. That’s what we did :)
In the end, your class could look like this:
class MyTypedStruct(Struct): __ATTRIBUTES__ = [IntegerParameter(name="my_int"), FloatParameter(name="my_float")] __DESCRIPTION__ = "A sample of class with typed attributes"
Want to know more ?
Find the complete documentation here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file strong_typing-0.3.1.tar.gz
.
File metadata
- Download URL: strong_typing-0.3.1.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad1dec2f4a2fd6f7aa315e5369d8b7c7fc07ae046b5c12a2e01e3b5d038657dd |
|
MD5 | 29b0f3ad8f6e820636a58b14e0158e56 |
|
BLAKE2b-256 | 296269c1911679e18c47bcb45eb36b09b718aa53749057998b06c2352211f4bc |
File details
Details for the file strong_typing-0.3.1-py2-none-any.whl
.
File metadata
- Download URL: strong_typing-0.3.1-py2-none-any.whl
- Upload date:
- Size: 25.8 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa5a21800d481d3d0750e494f690de8b34c9629eb65fa7fdb3f70e33dc33be11 |
|
MD5 | 96b536b570c1b4bf95fdac76425df93c |
|
BLAKE2b-256 | 69f84c0bc386b24622a80cf027de1a403c6fc5d9db463e3246e3058e0926fdc1 |