Skip to main content

Runtime type checking and not only for function arguments and class variables but even for the ordinary local and global variables!

Project description

typed-everywhere

Runtime type checking and not only for function arguments and class variables but even for the ordinary local and global variables!

Installation

pip install typed-everywhere

How To Use

We have a proxy class:

class typed_everywhere.Typed(wrapped)

class @typed_everywhere.Typed(wrapped)

This class inherits a variant of ObjectProxy from wrapt. Along with all augmented assignment operators, this class also overloads the normal assignment operator (=) thanks to assign-overload. wrapped can be any object and the resulting instance of this class will act like wrapped in every way. If you use this class, you need to call typed_everywhere.patch_and_reload_module(). You can also access this function from assign_overload module. You can find documentation about this function in assign-overload. Usage example:

import typed_everywhere

def main():
    a = typed_everywhere.Typed(10)
    a = 20 # Ok
    print(a) # prints 20
    a = "abc" # Error

if typed_everywhere.patch_and_reload_module():
    main()

One thing to note is if you assign a variable holding a Typed instance to a new variable, new variable takes the value of the Typed instance, not the value of its underlying object. However, most operators return the underlying object. Example:

import typed_everywhere

def main():
    a = typed_everywhere.Typed(10)
    print(type(a+5)) # prints int
    b = a
    print(type(b)) # prints Typed

if typed_everywhere.patch_and_reload_module():
    main()

If you want to access the underlying object, use the __wrapped__ attribute:

import typed_everywhere

def main():
    a = typed_everywhere.Typed(10)
    b = a.__wrapped__
    print(type(b)) # prints int

if typed_everywhere.patch_and_reload_module():
    main()

Typed is also compatible with typeguard. Function argument type enforcement example:

import typeguard
import typed_everywhere

@typeguard.typechecked
def test4(a: typed_everywhere.Typed[int]):
    pass

def main():
    a = typed_everywhere.Typed(10)
    b = typed_everywhere.Typed("abc")
    test4(a) # Ok
    test4(b) # Error

if typed_everywhere.patch_and_reload_module():
    main()

@typed_everywhere.typed_method(func)

Use this decorator for functions inside a class. Use @Typed for functions defined outside any class.

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

typed_everywhere-1.2.0.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

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

typed_everywhere-1.2.0-py2.py3-none-any.whl (3.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file typed_everywhere-1.2.0.tar.gz.

File metadata

  • Download URL: typed_everywhere-1.2.0.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for typed_everywhere-1.2.0.tar.gz
Algorithm Hash digest
SHA256 69600be857ff93a262f00d57608e0c6311d0179c6e4dc1c982ec545f913eedf7
MD5 a13802d3dab2ab920d7c3a5939a96f2b
BLAKE2b-256 ad00ae12a8189fca1313bb690df843a4e17389ce5dd09b92ada9907db5a4d884

See more details on using hashes here.

File details

Details for the file typed_everywhere-1.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for typed_everywhere-1.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4b20363037507b8eecba0053544655a784308ab328adb0bb17787bf048825754
MD5 10a7b0c7e0a35f40aa1d91026ace7905
BLAKE2b-256 2763345e8009a1b45ed61f4e872e3b56f9ff1d542bf2c2734a73e4e38ceb021e

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