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"),
  __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.

Source Distribution

strong_typing-0.3.1.tar.gz (12.0 kB view hashes)

Uploaded Source

Built Distribution

strong_typing-0.3.1-py2-none-any.whl (25.8 kB view hashes)

Uploaded Python 2

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