Skip to main content

Python type enforcer

Project description

Supported Python Versions pypi version Last commit Downloads

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.
  • 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__.

  • @typed_function decorator

    • typing_validator [Type[IValidateAnnotationsUseCase]]: You can provide an custom class that validates typing.
  • IValidateAnnotationsUseCase

    • func [Callable]: Function that will has be called after typing validation.

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

  • 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 or Union.
  • *args and **kwargs are not supported yet.

License

MIT

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

py-strongly-typed-2.0.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_strongly_typed-2.0.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file py-strongly-typed-2.0.0.tar.gz.

File metadata

  • Download URL: py-strongly-typed-2.0.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for py-strongly-typed-2.0.0.tar.gz
Algorithm Hash digest
SHA256 0557f482df79d68f83d523ee213a6a39d42b9232c6f29fd2b1998383c3fa4dc0
MD5 ad175bb5ed2302225155617f40373bca
BLAKE2b-256 5fe070d563cf13124e06ec45daffdac65376b858a3000ec0d4022032e93d6137

See more details on using hashes here.

File details

Details for the file py_strongly_typed-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py_strongly_typed-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64165f748713387054b14b34b372143be5bd97ec27bd2cbf1f07b0718b858e15
MD5 3cd9e7e93a8f12eb17a114d6e5b8724a
BLAKE2b-256 7934ecf328903f0697018c7556f6cbf386bc0be6ce78692f8e20f5ff325f6690

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