Skip to main content

A simple decorator to control the type matching of the arguments passed

Project description

Check Type Wrapper

This is a simple function decorator that takes data types as parameters. These types must match the types of the arguments to the function. In case of type mismatch, an exception is thrown informing about the mismatch of the expected type with the received function

Installation

Сreate a virtual environment in the project folder using the venv tool

$ python3 -m venv env

Activate the virtual environment

$ . env/bin/activate

Using PIP, install the third party decorator package

$ pip install Quastrado_check_type_wrapper

Usage

Import the decorator. Write a simple function and wrap

from check_type_wrapper import check_type

@check_type(int, int)
def func(arg1, arg2):
    return a + b

result = func('1', '2')

By passing parameters to the decorator, we indicate that the arguments that the function will accept must be of a numeric type. But, when calling the function, we will pass it two strings

Having run the code, in the terminal we should see the following in the terminal

...
    raise TypeMissMatchException(discrepancies)
check_type_wrapper.exception.TypeMissMatchException: 
Invalid type of argument 1. Expect int, not str
Invalid type of argument 2. Expect int, not str

Functions as an arguments

You can also use a decorator if you want to pass another function as an argument to the wrapped function

You will need to import the types module

import types

As a parameter for the wrapper, pass a tuple, where the first element will be types.FunctionType, and the next - the types of arguments that the function will accept

import types
from check_type_wrapper import check_type

@check_type((types.FunctionType, int), int)
def func(arg1, arg2):
    return arg1(arg2)

arg1 = 3
arg2 = lambda a : a + 10

result = func(arg2, arg1)

Status

Still in development

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

Quastrado_check_type_wrapper-1.0.0.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file Quastrado_check_type_wrapper-1.0.0.tar.gz.

File metadata

  • Download URL: Quastrado_check_type_wrapper-1.0.0.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for Quastrado_check_type_wrapper-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f077fd34698bfbd22a7f11e624dc373509a578b227c3daa9a9da1e63d51a15a6
MD5 2669a64aeb6bb00daa60a323f2c1b23e
BLAKE2b-256 ffccbb377f359ea9d30771c04c62cd94620f84dd2e8001f2b4e79d794021d7c2

See more details on using hashes here.

File details

Details for the file Quastrado_check_type_wrapper-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: Quastrado_check_type_wrapper-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for Quastrado_check_type_wrapper-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67dc0604b709ea371931d423ca7d9277986b54cd4e89f703c0f8b8804b9fc1ca
MD5 9ab1b575891c35ae0e163408a7fcaaee
BLAKE2b-256 80dbf462ab3e31aadc028b7ca3b99b4d05447465b9634a467eff8345abcb97b5

See more details on using hashes here.

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