1#!/usr/bin/env python3
2"""
3
4"""
5# Imports:
6from __future__ import annotations
7
8# ##-- stdlib imports
9import datetime
10import enum
11import functools as ftz
12import itertools as itz
13import logging as logmod
14import pathlib as pl
15import re
16import time
17import collections
18import contextlib
19import hashlib
20from copy import deepcopy
21from uuid import UUID, uuid1
22from weakref import ref
23import atexit # for @atexit.register
24import faulthandler
25# ##-- end stdlib imports
26
27# ##-- types
28# isort: off
29# General
30import abc
31import collections.abc
32import typing
33import types
34from typing import cast, assert_type, assert_never
35from typing import Generic, NewType, Never
36from typing import no_type_check, final, override, overload
37# Protocols and Interfaces:
38from typing import Protocol, runtime_checkable
39if typing.TYPE_CHECKING:
40 from typing import Final, ClassVar, Any, Self
41 from typing import Literal, LiteralString
42 from typing import TypeGuard
43 from collections.abc import Iterable, Iterator, Callable, Generator
44 from collections.abc import Sequence, Mapping, MutableMapping, Hashable
45
46 from jgdv import Maybe
47
48 type CheckType = type | types.UnionType
49 type CheckCancel = Literal[False]
50
51# isort: on
52# ##-- end types
53
54##-- logging
55logging = logmod.getLogger(__name__)
56##-- end logging
57
58# Vars:
59
60# Body:
61
[docs]
62class CheckType_m:
63 """ A Mixin for runtime type checking """
64
[docs]
65 def _check_type(self, value:Any, check:Maybe[CheckType|CheckCancel]=None) -> None:
66 is_type : bool
67 check_target : Maybe[CheckType]
68
69 if check is False or value is None:
70 return
71
72 check_target = check
73 is_type = isinstance(value, type)
74
75 match check_target, value:
76 case None, _:
77 return
78 case x, type() as val if isinstance(x, type|UnionTypes) and x is not None and issubclass(val, x):
79 return
80 case type() | types.UnionType(), val if isinstance(val, check_target):
81 return
82 case _:
83 raise ImportError(errors.CodeRefImportUnknownFail, self, check_target)