Python type enforcer
Project description
What do I need?
- Python (3.5+)
Description
With py-strongly-typed, now typing annotations metters! Use this lib to validate provided args typing.
Be free to contribute with our GitHub project.
Get Started
-
Install
pip install py-strongly-typed
Usage
-
Typing classes
from py_strongly_typed import TypedClass class SomeClass(TypedClass): def __init__(self, first_arg: str, second_arg: int): self.first_arg = first_arg self.second_arg = second_arg def some_method(*, method_arg: list): pass instance_class = SomeClass('arg_value', second_arg=1) istance_class.some_method(method_arg=[1])You can also to set only
__init__method to be typed doing:from py_strongly_typed import TypedClass class SomeClass(TypedClass): type_all_methods: bool = False def __init__(self, first_arg: str, second_arg: int): self.first_arg = first_arg self.second_arg = second_arg def some_method(*, method_arg: list): pass instance_class = SomeClass('arg_value', second_arg=1) istance_class.some_method(method_arg=[1])or:
from py_strongly_typed import TypedInit class SomeClass(TypedInit): def __init__(self, first_arg: str, second_arg: int): self.first_arg = first_arg self.second_arg = second_arg def some_method(*, method_arg: list): pass instance_class = SomeClass('arg_value', second_arg=1) istance_class.some_method(method_arg=[1])
-
Typing function or methods
You can also use decorators for methods or functions typing:
from py_strongly_typed import typed_function @typed_function def some_function(arg: str): pass
Objects
-
TypedClass class
Used to set a class to has typing annotations validated.- TypedInit class
Used to set a class to has typing annotations validated on __init__ only.
- TypedInit class
-
typed_function decorator
Used to set a function or method to has typing annotations validated. -
IValidateAnnotationsUseCase abstract class
Interface that should be used to implement custom typing validations. -
ValidateAnnotationsUseCase class (inherit IValidateAnnotationsUseCase)
Default typing validation core.
Argument options
-
TypedClass and TypedInit
-
typing_validator [Type[IValidateAnnotationsUseCase]] [default=ValidateAnnotationsUseCase]:
You can provide an custom class that validates typing. -
type_all_methods [bool] [default=True]:
Defines if all methods of the class will be type validated or only __init__. -
ignore_float_and_integer_difference [bool] [default=True]:
If True, providing an integer on a float expected or inverse will be ignored.
-
-
@typed_function decorator
-
typing_validator [Type[IValidateAnnotationsUseCase]]:
You can provide an custom class that validates typing. -
ignore_float_and_integer_difference [bool]:
If True, providing an integer on a float expected or inverse will be ignored.
-
-
IValidateAnnotationsUseCase
- func [Callable]: Function that will has be called after typing validation.
- ignore_float_and_integer_difference [bool]:
If True, providing an integer on a float expected or inverse will be ignored.
Behavior
- When an invalid value type is provided, WrongType exception will be raised.
- When function or method is defined with any argument missing annotation, MissingAnnotation will be raised.
Limitations
- Generics (example: List[str]) are not supported yet.
- Typing using or operator will validate only first one. Example: providing str or int,
only str will be considered. To provide more than one expected type, use
|operator orUnion. *argsand**kwargsare not supported due to be impossible to provide typing annotations for them without Generics.
License
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
File details
Details for the file py-strongly-typed-1.1.3.tar.gz.
File metadata
- Download URL: py-strongly-typed-1.1.3.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7f3a199c847708824ca43ae4a8f876b6127470905d8f6171bf06f2c37562f91
|
|
| MD5 |
f0ff1580570a6b4d1c55b0a322f8351c
|
|
| BLAKE2b-256 |
cea260011a78d0635c3253e417e2bcd02e22fe6486986bb081c30180e6772232
|