Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/austinv11/tinkertest
Super simple and readable testing/validation in python 3
https://github.com/austinv11/tinkertest
python python3 testing unittest validation
Last synced: 2 days ago
JSON representation
Super simple and readable testing/validation in python 3
- Host: GitHub
- URL: https://github.com/austinv11/tinkertest
- Owner: austinv11
- License: apache-2.0
- Created: 2018-08-18T19:13:43.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-08-19T03:10:01.000Z (about 6 years ago)
- Last Synced: 2024-09-28T09:12:32.337Z (about 2 months ago)
- Topics: python, python3, testing, unittest, validation
- Language: Python
- Homepage: https://pypi.org/project/TinkerTest/
- Size: 17.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
TinkerTest
==========
Inline testing/validation in python 3, based on this wonderful blog post: https://tinkering.xyz/abusing-type-annotations/## What is this?
TinkerTest aims to simplify data validation checking in both unit tests and production environments. In order to do this
conveniently, TinkerTest ~~abuses~~ makes use of the fact that python type annotations can be any arbitrary python
expression. Using this fact allows us to inject validation logic into your scripts which simply evaluate your type
annotations.## Helpful Hint
Starting from python 3.7, the `annotations` future import exists. This may be helpful for you, as its lazy evaluation
should prevent some common mistakes in using this library.## Trivial Example
```python
# my_script.py
from tinkertest import inject_into_typeclass MyClass:
my_field: 'my_field < 0' # This field only accepts values < 0
my_str_field: str # This field only accepts strings
def __init__(self, my_field, my_str_field):
self.my_field = my_field
self.my_str_field = my_str_field
# This function only accepts a non-None argument and should always return 1
def some_func(self, input_val: 'input_val is not None') -> 'returned == 1': # 'returned' is a magic variable for use in return annotations
...
inject_into_type(MyClass) # Wires all the injections, now any calls will be validated!
```