aboutsummaryrefslogtreecommitdiffstats
path: root/README.md
blob: 496aad01d936031f807e78f3a2cb6260cfa72c8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# bearcheck

Tiny extension to [beartype](https://github.com/beartype/),
which allows inline type assertion and narrowing similar to
[check_type](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.check_type)
from [typeguard](https://github.com/agronholm/typeguard).

Using `bearcheck()` is *faster* and *safer* than `check_type()`, since it
leverages the incredibly fast *beartype* library for type checking, and
allows specifying the exact return type.

### Usage

```python
from bearcheck import bearcheck, Check

var = bearcheck("a", Check[Literal["a", "b"]])
reveal_type(var)  # Type of "var" is "Literal['a', 'b']"
```

.. as opposed to ..

```python
from typeguard import check_type

var = check_type("a", Literal["a", "b"])
reveal_type(var)  # Type of "var" is "Any"
```

`type_check()` returns *Any* if the type hint is not primitive, a common pitfall!