Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex0Blackwell committed Oct 2, 2023
1 parent 4783f61 commit c195bbd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
78 changes: 74 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Py-Strict
## Python's missing runtime type checker
Python's missing runtime type checker

Readme incoming.
## Installation

Basic usage:
```
pip install py-strict
```

## Usage

```py
from py_strict import strict
Expand All @@ -14,4 +18,70 @@ def person_age(name: str, age: int) -> str

# If the values mismatch the types, a TypeError is thrown.
person_age(...)
```
```

## Use Cases
If you ever find yourself asserting all your types are correct, for example...
```py
def func(person: Person, employee: Employee):
if not isinstance(person, Person):
raise TypeError("Expected person to be of type Person")
if not isinstance(employee, Employee):
raise TypeError("Expected employee to be of type Employee")
...
```
this library can simplify the same functionality into the following:
```py
@strict
def func(person: Person, employee: Employee):
...
```

## Features
1. Python 3.9+ support.
2. Works with the `typing` library and default/custom Python types.
3. Checks embedded types recursively. (Eg `Dict[str, List[int]]`.)
4. Checks return type if specified.
5. Works with any combination of args, kwargs, and defaults.
6. Informative error messages.
7. In the event of a failure, Py-Strict swallows the error to avoid user impact.

## Limitations
1. **Performance:** Type checking is done at _runtime_. So, there is a performance
impact—especially if the value has many members, for example, a long list. For
static type checking consider [mypy](https://pypi.org/project/mypy/).
2. Ignores variable-length args (varargs).

## Examples
Wrong value.
```py
@strict
def my_func(arg1: List[int]):
...

# Raises:
# TypeError('Value (whoops!) in "my_func(arg1=typing.List[int])" is not of type int')
my_func([1, 2, "whoops!", 3])
```

Wrong data structure.
```py
@strict
def my_func(arg1: Dict[str, int]):
...

# Raises:
# TypeError('Expected type typing.Dict[str, int] in "my_func(arg1=typing.Dict[str, int])" got list')
my_func([1, 2, 3])
```

Wrong return type.
```py
@strict
def my_func() -> int:
return "one"

# Raises:
# TypeError('Return value (one) in "my_func() -> int" is not of type int')
my_func()
```
4 changes: 2 additions & 2 deletions py_strict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Python's missing runtime type checker
"""

from strict import strict
from .strict import strict

__version__ = "0.1.1"
__version__ = "0.1.0"
__author__ = "Alex Blackwell"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="py-strict",
version="0.1.1",
version="0.1.0",
description="Python's missing runtime type checker",
# url='https://github.com/',
author="Alex Blackwell",
Expand Down

0 comments on commit c195bbd

Please sign in to comment.