Skip to main content

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.

Metadata

Release files for func-prototypes 0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for func-prototypes 0.5
File Size Uploaded
func_prototypes-0.5.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for func-prototypes 0.5
File Interpreter ABI Platform
func_prototypes-0.5-py3-none-any.whl Python 3 none any Details

Total release size: 8.4 kB

Release files / func_prototypes-0.5.tar.gz

Download URL func_prototypes-0.5.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7c491921caa422634f929aaf3431296d9b367177a775e7d7e84052b311a36116
BLAKE2b-256 checksum
How to use checksums
be798767920ec7b9a6c69f72b454ee3ab0e2be454fff563383ca871b77e94eeb
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / func_prototypes-0.5-py3-none-any.whl

Download URL func_prototypes-0.5-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f3f967433f87679343220526d9e8cb03cb1b6e31914fe8e21e2442a7535ca420
BLAKE2b-256 checksum
How to use checksums
75a6ac22512331bfd689398fcb69fe08bc33988afaad809fa672ba88686103ce
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.5 This release

2 release files

0.4

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page