Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for strong-typing, version 0.3.1
Filename, size File type Python version Upload date Hashes
Filename, size strong_typing-0.3.1-py2-none-any.whl (25.8 kB) File type Wheel Python version py2 Upload date Hashes View
Filename, size strong_typing-0.3.1.tar.gz (12.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page