luckylucy.utils.exceptions 1.85rc1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# /// script
|
|
3
|
+
# requires-python = ">=3.0"
|
|
4
|
+
# dependencies = []
|
|
5
|
+
#
|
|
6
|
+
# [tool.luckylucy]
|
|
7
|
+
# module = true
|
|
8
|
+
# ///
|
|
9
|
+
|
|
10
|
+
import logging
|
|
11
|
+
import warnings
|
|
12
|
+
|
|
13
|
+
__author__ = 'luckydonald'
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def assert_type_or_raise(value, expected_type_clazz_or_tuple, *more_clazzes, exception_clazz=TypeError,
|
|
19
|
+
parameter_name=None):
|
|
20
|
+
"""
|
|
21
|
+
A better `assert(isinstance(a, B)` because it supports `None` (as well as some other types except tuple or list), and an nice exception text.
|
|
22
|
+
|
|
23
|
+
:param value: The variable to check
|
|
24
|
+
:param expected_type_clazz_or_tuple: a class or value or list/tuple of that
|
|
25
|
+
:param *more_clazzes: even more classes or values
|
|
26
|
+
:param exception_clazz: type of the exception raised. Defaults to :class:`TypeError`
|
|
27
|
+
:param parameter_name: setting will add information that given parameter was wrong, instead of just saying "Should be type"
|
|
28
|
+
|
|
29
|
+
:raises: exception_clazz
|
|
30
|
+
:return: Nothing
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
Examples/Tests:
|
|
34
|
+
|
|
35
|
+
Check if None:
|
|
36
|
+
|
|
37
|
+
>>> val = None
|
|
38
|
+
>>> assert_type_or_raise(val, None)
|
|
39
|
+
|
|
40
|
+
>>> assert_type_or_raise(val, [None])
|
|
41
|
+
|
|
42
|
+
Check multible:
|
|
43
|
+
|
|
44
|
+
>>> assert_type_or_raise(val, [None, bool, int])
|
|
45
|
+
|
|
46
|
+
>>> assert_type_or_raise(val, (None, bool, int))
|
|
47
|
+
|
|
48
|
+
>>> assert_type_or_raise(val, None, bool, int)
|
|
49
|
+
|
|
50
|
+
>>> assert_type_or_raise(val, [None, bool], int)
|
|
51
|
+
|
|
52
|
+
>>> assert_type_or_raise(val, None, [bool, int])
|
|
53
|
+
|
|
54
|
+
>>> assert_type_or_raise(1, [None, bool, int])
|
|
55
|
+
|
|
56
|
+
>>> assert_type_or_raise(True, [None, bool, int])
|
|
57
|
+
|
|
58
|
+
>>> assert_type_or_raise(1, [None, bool, 1])
|
|
59
|
+
|
|
60
|
+
>>> assert_type_or_raise(True, [None, True, int])
|
|
61
|
+
|
|
62
|
+
>>> assert_type_or_raise("lel", [None, True, int, "lel"])
|
|
63
|
+
|
|
64
|
+
>>> assert_type_or_raise(True, [None, True, int, "lel"])
|
|
65
|
+
|
|
66
|
+
>>> assert_type_or_raise(True, [None, True, int, "lel"], exception_clazz=ValueError)
|
|
67
|
+
|
|
68
|
+
>>> assert_type_or_raise(True, [None, True, int, "lel"], parameter_name="foobar")
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
Failing asserts:
|
|
72
|
+
|
|
73
|
+
>>> assert_type_or_raise("True", [None, True, int])
|
|
74
|
+
Traceback (most recent call last):
|
|
75
|
+
...
|
|
76
|
+
TypeError: Should be one of the types [None, True, <class 'int'>], but is type <class 'str'>: 'True'
|
|
77
|
+
|
|
78
|
+
>>> assert_type_or_raise(None, [True, int])
|
|
79
|
+
Traceback (most recent call last):
|
|
80
|
+
...
|
|
81
|
+
TypeError: Should be one of the types [True, <class 'int'>], but is type <class 'NoneType'>: None
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
>>> assert_type_or_raise("lelz", [None, True, int, "lel"])
|
|
85
|
+
Traceback (most recent call last):
|
|
86
|
+
...
|
|
87
|
+
TypeError: Should be one of the types [None, True, <class 'int'>, 'lel'], but is type <class 'str'>: 'lelz'
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
Custom Exception Type:
|
|
91
|
+
|
|
92
|
+
>>> assert_type_or_raise(None, [True, int], exception_clazz=ValueError)
|
|
93
|
+
Traceback (most recent call last):
|
|
94
|
+
...
|
|
95
|
+
ValueError: Should be one of the types [True, <class 'int'>], but is type <class 'NoneType'>: None
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
Parameter name supplied:
|
|
99
|
+
|
|
100
|
+
>>> assert_type_or_raise(None, [True, int], parameter_name="foobar")
|
|
101
|
+
Traceback (most recent call last):
|
|
102
|
+
...
|
|
103
|
+
TypeError: The parameter foobar should be one of the types [True, <class 'int'>], but is type <class 'NoneType'>: None
|
|
104
|
+
|
|
105
|
+
"""
|
|
106
|
+
if expected_type_clazz_or_tuple is None and value is None:
|
|
107
|
+
return # None == None
|
|
108
|
+
# end if
|
|
109
|
+
if isinstance(expected_type_clazz_or_tuple, tuple):
|
|
110
|
+
expected_type_clazz_or_tuple = list(expected_type_clazz_or_tuple)
|
|
111
|
+
if not isinstance(expected_type_clazz_or_tuple, list):
|
|
112
|
+
expected_type_clazz_or_tuple = [expected_type_clazz_or_tuple]
|
|
113
|
+
# end if
|
|
114
|
+
expected_type_clazz_or_tuple.extend(more_clazzes)
|
|
115
|
+
parameter_string = "the parameter {name} ".format(name=parameter_name) if parameter_name else ""
|
|
116
|
+
for list_item in expected_type_clazz_or_tuple:
|
|
117
|
+
if isinstance(list_item, (list, tuple)):
|
|
118
|
+
expected_type_clazz_or_tuple.extend(list(list_item))
|
|
119
|
+
continue
|
|
120
|
+
if list_item is None:
|
|
121
|
+
if value is None:
|
|
122
|
+
assert value is None
|
|
123
|
+
break # is valid
|
|
124
|
+
else:
|
|
125
|
+
continue # is None, but value is not.
|
|
126
|
+
# end if
|
|
127
|
+
if isinstance(list_item, type):
|
|
128
|
+
if isinstance(value, list_item):
|
|
129
|
+
assert isinstance(value, list_item)
|
|
130
|
+
break # is valid
|
|
131
|
+
# end if
|
|
132
|
+
else:
|
|
133
|
+
if value is list_item or value == list_item:
|
|
134
|
+
assert value is list_item or value == list_item
|
|
135
|
+
break # is valid
|
|
136
|
+
# end if
|
|
137
|
+
# end if
|
|
138
|
+
else: # for: -> is not valid, no break was used
|
|
139
|
+
if len(expected_type_clazz_or_tuple) != 1:
|
|
140
|
+
exception_text = (
|
|
141
|
+
"{parameter_string}should be one of the types [{expected_type}], but is type {real_type}: {real_value!r}".format(
|
|
142
|
+
parameter_string=parameter_string, real_type=type(value), real_value=value,
|
|
143
|
+
expected_type=", ".join(repr(x) for x in expected_type_clazz_or_tuple)
|
|
144
|
+
)
|
|
145
|
+
)
|
|
146
|
+
else:
|
|
147
|
+
exception_text = (
|
|
148
|
+
"{parameter_string}should be of type {expected_type!r}, but is type {real_type}: {real_value!r}".format(
|
|
149
|
+
parameter_string=parameter_string, expected_type=expected_type_clazz_or_tuple,
|
|
150
|
+
real_type=type(value), real_value=value
|
|
151
|
+
)
|
|
152
|
+
)
|
|
153
|
+
# end if
|
|
154
|
+
exception_text = exception_text[0].upper() + exception_text[1:]
|
|
155
|
+
raise exception_clazz(exception_text)
|
|
156
|
+
# end for
|
|
157
|
+
# end def assert_or_raise
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def assert_or_raise(*args, **kwargs):
|
|
161
|
+
warnings.warn("The `luckylucy.utils.exceptions.assert_or_raise` method is deprecated, "
|
|
162
|
+
"use `assert_type_or_raise` instead", DeprecationWarning, 2)
|
|
163
|
+
logger.warn("Deprecated: `luckylucy.utils.exceptions.assert_or_raise` was renamed to `assert_type_or_raise`.")
|
|
164
|
+
return assert_type_or_raise(*args, **kwargs)
|
|
165
|
+
|
|
166
|
+
# end def
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: luckylucy.utils.exceptions
|
|
3
|
+
Version: 1.85.pre1
|
|
4
|
+
Summary: A collection of utilities I use across different projects (luckylucy.utils.exceptions)
|
|
5
|
+
Project-URL: Homepage, https://github.com/luckydonald/luckylucy.utils
|
|
6
|
+
Author-email: luckylucy <luckylucy.utils._.code@luckydonald.de>
|
|
7
|
+
License-Expression: GPL-2.0-or-later
|
|
8
|
+
Requires-Python: >=3.0
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
luckylucy/utils/exceptions.py,sha256=o7vK88SeRKP4TQo7zMbsksFvL5GCJkbFPh_BhbJtJmo,6088
|
|
2
|
+
luckylucy_utils_exceptions-1.85rc1.dist-info/METADATA,sha256=EK1ThDfSu-9QNTavdmgYd4zkZfepbyDgczhU_cMvv6w,364
|
|
3
|
+
luckylucy_utils_exceptions-1.85rc1.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
4
|
+
luckylucy_utils_exceptions-1.85rc1.dist-info/RECORD,,
|