Skip to main content

library which helps people constrain python functions by type.

Project description

prototype

Framework for adding strict typing to python.

Why would I want to add type checking to python?

Python's flexable duck typing system is great for allowing people to write generic funcitons which can be re-used with various kinds of input. However, not all functions are written with this generality in mind.

By annotating your functions you give a clear signal to callers about what inputs are expected. Additionally, annotations can help you write functions with fewer type checking conditionals. Annotations allow you to find bugs at the top of your functions rather than in the middle of a calculation.

Implicit Converters:

These decorators implicitly convert arguments are return values to the type you expect. When a value cannot be coerced into the specified type, ValueError is raised.

Constructors

Implicitly passes function arguments to a type constructor. Raises ValueError when invalid inputs are provided.

Before:

def integer_adder(a, b):
  a = int(a)
  b = int(b)
  return a+b

After:

@constructors(int, int)
def int_adder(a, b):
  return a+b

Examples:

int_adder(3,4) = 7
int_adder(3.9,4.7) = 7
int_adder("3", "4") = 7
int_adder("asdf", "4") Raises ValueError

Returns:

Converts the result of a function to a given type. Raises ValueError on error.

Before:

def int_adder(a, b):
  return int(a+b)

After:

@returns(int)
def int_adder(a, b):
  return a+b

Examples:

int_adder(3,4) = 7
int_adder(3.9, 4.7) = 8
int_adder("3", "4") Raises ValueError

Type Constrainers:

These decorators raise TypeError when a constraint is violated.

Typed

Enforces instanceof checks to arguments of a function.

Before:

def int_adder(a, b):
  if not instanceof(a, int):
    raise TypeError("%s is not of type int" % a)
  if not instanceof(b, int):
    raise TypeError("%s is not of type int" % b)
  return a+b

After:

@typed(int, int)
def int_adder(a, b):
  return a+b

Examples:

int_adder(3,4) = 7
int_adder(3.9,4.7) Raises TypeError
int_adder("3", "4") Raises TypeError
int_adder("asdf", "4") Raises TypeError

Returned

Enforces the result type of a function.

Before:

def integer_adder(a, b):
  result = a+b
  if not instanceof(result, int):
    raise TypeError("%s is not an int" % result)
  else:
    return result

After:

@returned(int)
def integer_adder(a, b):
  return a+b

Examples:

int_adder(3,4) = 7
int_adder(3.9,4.7) Raises TypeError
int_adder("3", "4") Raises TypeError
int_adder("asdf", "4") Raises TypeError

Limitiations:

Order of application matters:

@constructors and @typed are unable to deal functions which use *args and **kwargs. Since virtually all decorators convert a function from explicit args to args & kwargs, @constructors and @typed must be the first decorator on a function.

Example:

@returned(int)
@typed(int, int)
def int_adder(a, b):
  return a+b

Run time evaluation:

Prototype only able to enforce annotations at run time. This means that prototype is not a replacement for proper testing or due diligence. Rather, prototype allows you to make your code better, with less typing.

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

func_prototypes-0.5.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

func_prototypes-0.5-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file func_prototypes-0.5.tar.gz.

File metadata

  • Download URL: func_prototypes-0.5.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.3

File hashes

Hashes for func_prototypes-0.5.tar.gz
Algorithm Hash digest
SHA256 7c491921caa422634f929aaf3431296d9b367177a775e7d7e84052b311a36116
MD5 ac3c2989b2ff69d46447119ea4a26375
BLAKE2b-256 be798767920ec7b9a6c69f72b454ee3ab0e2be454fff563383ca871b77e94eeb

See more details on using hashes here.

File details

Details for the file func_prototypes-0.5-py3-none-any.whl.

File metadata

  • Download URL: func_prototypes-0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.3

File hashes

Hashes for func_prototypes-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f3f967433f87679343220526d9e8cb03cb1b6e31914fe8e21e2442a7535ca420
MD5 0ac028c1a6a13f3589611860de2c769d
BLAKE2b-256 75a6ac22512331bfd689398fcb69fe08bc33988afaad809fa672ba88686103ce

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