Skip to content

notarealdeveloper/is_instance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

is_instance

A better isinstance for python.

examples

>>> import is_instance

>>> is_instance(['spam', 'and', 'eggs'], list[str])
True

>>> is_instance(['spam', 'and', 'eggs'], list[int])
False

>>> is_instance({'bird': True, 'alive': False}, dict[str, bool])
True

>>> is_instance({(),(1,2),(4,5,'6')}, set[tuple[int]])
False

>>> is_instance({(),(1,2),(4,5,6)}, set[tuple[int]])
True

>>> is_instance([{'a': 1, 'b': None}, {'a': 3, 'b': 4}], list[dict[str, int]])
False

>>> is_instance([{'a': 1, 'b': None}, {'a': 3, 'b': 4}], list[dict[str, int|None]])
True

>>> from collections.abc import Sequence

>>> is_instance('cake', Sequence[str])
True

The following type slang is also supported, inspired by the Haskell type system.

>>> import is_instance

>>> is_instance(['spam', 'and', 'eggs'], [str])
True

>>> is_instance(['spam', 'and', 'eggs'], [int])
False

>>> is_instance({'bird': True, 'alive': False}, {str: bool})
True

>>> is_instance([{'a': 1, 'b': None}, {'a': 3, 'b': 4}], [{str: int}])
False

>>> is_instance([{'a': 1, 'b': None}, {'a': 3, 'b': 4}], [{str: int | None}])
True