Skip to main content

Type checker for functions and classes.

Project description

strict-types

Python type checking for functions and classes. Check if the datatypes of the input parameters match the datatypes you want.

from strict import *

StrictClass

Use StrictClass to ensure you are assigning the correct content (with the right datatype) to the desired attribute. This class will result in an object that contains all keyword arguments passed to the class and will check the datatype of each parameter with the datatype referenced in the annotation.

It is required a child class that inherits StrictClass. That class may have some annotations describing the desired attributes and their datatypes.

>>> class StrictClassChild(StrictClass):
...  key: str

Note that key is referring to the attribute that will store a str datatype — i.g., <attribute>: <datatype> — and, in this case, any content assigned to key that is not str will raise an AssingError.

Once the class is defined, you may want to initialize the object by entering the parameters (keyword arguments).

>>> obj = StrictClassChild(key = 'word')
>>> obj.key
'word'

StrictFunction

The StrictFunction is similar to StrictClass, the difference is that this class needs a function to call. The function can be executed calling the class object.

>>> class StrictFunctionChild(StrictFunction):
...  data: str
...
>>> def uppercase_string(data):
...  return data.upper()
...
>>> obj = StrictFunctionChild(uppercase_string, data = 'keyword')
>>> obj()
'KEYWORD'

Note that the parameter data of the uppercase_string function must be a str, otherwise it will raise an exception if the upper method is called but doesn't exist. In addition, if the datatype of data is bytes (when using the function directly), it will convert the binary content to uppercase and return a bytes datatype. This may cause some problems especially if we are working with client input. This problem is avoided by using this class.

It is also possible to access the keyword arguments just like the other class.

>>> obj.data
'keyword'

@strict_datatype

The strict_datatype acts like StrictFunction class, but it is used as a decorator. It will check the desired datatypes of a function (annotation) and compare with the parameters datatypes.

>>> @strict_datatype
... def uppercase_string(data: str):
...  return data.upper()
...
>>> uppercase_string('in all caps!')
'IN ALL CAPS!'

Exceptions

The AssingError is the only exceptions used by this script, but other may occur dealing with functions.

Everytime this exception is called, the parameter that generated the exception is stored and can be accessed calling the parameter attribute (str). The invalid and the required datatypes may be stored as well, and both can be accessed calling the invalid attribute (type) and the required attribute (tuple) respectively.

>>> from strict import *
>>> class StrictClassChild(StrictClass):
...     data: str
...     multiply: int
...
>>> obj = StrictClassChild(data='Hello', multiply='5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/strict/strict.py", line 44, in __init__
    raise AssignError(
strict.AssignError: Trying to assign an invalid datatype to the parameter "multiply". Datatype must be <class 'int'>, and not <class 'str'>.

Note that the exception was raised because the multiply parameter in the StrictClassChild class is a string and not an integer, as required. Wrapping it all using try and except and an invalid code, we can catch the error and the attributes previously mentioned:

>>> try:
...     (...)
... except AssignError as err:
...     print('error message:', err)
...     print('invalid parameter:', err.parameter)
...     print('invalid datatype:', err.invalid)
...     print('required datatypes:', err.required)
...
error message: Trying to assign an invalid datatype to the parameter "multiply". Datatype must be <class 'int'>, and not <class 'str'>.
invalid parameter: multiply
invalid datatype: <class 'str'>
required datatypes: (<class 'int'>,)

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

strict-types-1.1.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file strict-types-1.1.tar.gz.

File metadata

  • Download URL: strict-types-1.1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for strict-types-1.1.tar.gz
Algorithm Hash digest
SHA256 6da83469d587174dce46b91219c0ee4b179bc005f684eee13febe05c02ef16c0
MD5 7cd34f6cc7f0b0a603e96dab6314fd6b
BLAKE2b-256 e3996bde3b6b5be9f2e7cd39c3300bc955967e0f93519c006c8f2252863e2944

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page