No project description provided
Project description
isinstance2
isinstance2
is a module that provides a powerful runtime type checker for Python's built-in generic classes and
generic type hints. It allows you to perform runtime instance type checks on objects that are instances of a generic
class, as well as subclass checks on generic classes, even if you don't know the exact type of the generic parameters.
Features
- Perform runtime instance and subclass checks on generic classes
- Supports built-in generic classes such as
list
,tuple
,dict
,set
, andfrozenset
, as well asOptional
andLiteral
. - Check if an object is an instance of a
tuple
with variadic arguments. - Register custom class or function with
isinstance2
's instance checker registry.
Installation
pip install isinstance2
Basic Usage
Instance Checks
from typing import Iterable, Literal
from isinstance2 import isinstance2
# Basic instance checks
assert isinstance2([1, 2, 3], list[int])
assert isinstance2((1, 2.0, 'three'), tuple[int, float, str])
assert isinstance2({1, 2, 3}, set[int])
assert isinstance2({"foo": 1, "bar": 2}, dict[str, int])
assert isinstance2(frozenset([1, 2, 'Hi! 😊']), frozenset[int | Literal['Hi! 😊']])
# Ellipses in tuples work
assert isinstance2((1, 'two', 3.0, 'four'), tuple[int | float | str, ...])
# You can also check against abstract generic classes
assert isinstance2(range(10), Iterable[int])
assert not isinstance2(range(10), Iterable[float])
Subclass Checks
from typing import Collection, Iterable
from isinstance2 import issubclass2
# Basic subclass checks
assert issubclass2(list[int], list[int | float])
assert issubclass2(tuple[int, float], tuple[int | float, ...])
# Classes without generic parameters are presumed to match
assert issubclass2(list, list[int])
assert issubclass2(list[int], list)
# Abstract generic classes
assert issubclass2(list[int], Iterable[int])
assert issubclass2(Collection[bool], Iterable[int]) # Yes, bool is a subclass of int
Advanced Usage
To check if an object is an instance of a custom generic class, register it with isinstance2
's instance checker
from typing import Generic, TypeVar, Any
from isinstance2 import isinstance2, register_instance_checker
T = TypeVar('T')
class MyClass(Generic[T]):
...
@register_instance_checker
def is_instance_of_my_class(obj: Any) -> bool:
return isinstance(obj, MyClass)
assert isinstance2(MyClass(), MyClass)
Limitations
- Does not yet support
TypeVar
Container
- And likely quite a few other generic classes that I've missed. Please open an issue if you find one.
- Subclass checks for custom classes (instance checks are supported)
License
isinstance2
is released under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
isinstance2-0.1.0.tar.gz
(5.4 kB
view hashes)
Built Distribution
Close
Hashes for isinstance2-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dbb7d078039530130a24524fb5dbb751594b843743c5e88f8e3043b6cef59d0b |
|
MD5 | 755eade59fd4eada2203d93b67c147af |
|
BLAKE2b-256 | 2f0f43c76f83995e3772cf2338c00884bcc4b5754a09f4edf6f2ee7577cd43dc |