| Conditions | 4 |
| Total Lines | 24 |
| Code Lines | 14 |
| Lines | 24 |
| Ratio | 100 % |
| Changes | 0 | ||
| 1 | View Code Duplication | def instance_of(obj: object, *args: type) -> bool: |
|
|
|
|||
| 2 | """ |
||
| 3 | Check whether ``obj`` is an instance of all types in ``args``, while also |
||
| 4 | considering generics. |
||
| 5 | :param obj: the object in subject. |
||
| 6 | :param args: the type(s) of which ``obj`` is an instance or not. |
||
| 7 | :return: ``True`` if ``obj`` is an instance of all types in ``args``. |
||
| 8 | """ |
||
| 9 | from typish.classes._literal import Literal, LiteralAlias |
||
| 10 | from typish.functions._subclass_of import subclass_of |
||
| 11 | from typish.functions._get_type import get_type |
||
| 12 | |||
| 13 | try: |
||
| 14 | return all(isinstance(obj, arg) for arg in args) |
||
| 15 | except Exception: |
||
| 16 | ... # If the regular check didn't work, continue below. |
||
| 17 | |||
| 18 | if args and issubclass(args[0], Literal): |
||
| 19 | leftovers = args[1:] |
||
| 20 | return (LiteralAlias.__instancecheck__(args[0], obj) |
||
| 21 | and (not leftovers or instance_of(obj, leftovers))) |
||
| 22 | |||
| 23 | type_ = get_type(obj, use_union=True) |
||
| 24 | return subclass_of(type_, *args) |
||
| 25 |