https://github.com/zwimer/simple_type_checker
A small extendable python type checker library
https://github.com/zwimer/simple_type_checker
Last synced: 4 months ago
JSON representation
A small extendable python type checker library
- Host: GitHub
- URL: https://github.com/zwimer/simple_type_checker
- Owner: zwimer
- License: gpl-3.0
- Created: 2024-01-17T15:40:50.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-08-26T06:12:10.000Z (almost 2 years ago)
- Last Synced: 2025-09-04T12:02:27.508Z (10 months ago)
- Language: Python
- Size: 40 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simple\_type\_checker
A small extendable python type checker library
### Basic Usage
Checking a type:
```python
from simple_type_check import type_check
assert type_check("A string", str)
```
As a decorator:
```python
from simple_type_check import type_check, TypeCheckFailed
@type_check.returns
def id_(x) -> int:
return x
_ = id_(1) # Passes
try:
_ = id_("string") # Raises
except TypeCheckFailed as e:
pass
```
Decorators for aruments exist as well: `type_check.args`.
Both can be done at once with `type_check.decorate`.
### With custom types
Basic types where `isinstance` is sufficient:
```python
from simple_type_check import TypeChecker
class Foo:
pass
type_check = TypeChecker(Foo)
assert type_check(Foo(), Foo)
```
Special type checking logic desired:
```python
from simple_type_check import TopLevelCheck, TypeChecker
Bar = list[int] | list["Bar"]
class BarCheck(TopLevelCheck):
def __call__(self, obj, type_) -> bool:
return type_ == "Bar" and self._recurse(obj, Bar)
type_check = TypeChecker(advanced=[BarCheck])
type_check([1, [2], [[3]]], Bar) # Should pass
```
This use case is useful for container types that take arguments, for example.