@@ -10,7 +10,7 @@ class SentinelValue:
10
10
Useful for distinguishing "value is not set" and "value is set to None" cases
11
11
as shown in this example::
12
12
13
- >>> NOT_SET = SentinelValue("NOT_SET", __name__ )
13
+ >>> NOT_SET = SentinelValue(__name__, "NOT_SET")
14
14
15
15
>>> value = getattr(object, "some_attribute", NOT_SET)
16
16
>>> if value is NOT_SET:
@@ -25,7 +25,7 @@ class SentinelValue:
25
25
>>> class Missing(SentinelValue):
26
26
... pass
27
27
28
- >>> MISSING = Missing("MISSING", __name__ )
28
+ >>> MISSING = Missing(__name__, "MISSING")
29
29
30
30
# Here is how the Missing class can be used for type hinting.
31
31
>>> value: Union[str, None, Missing] = getattr(object, "some_attribute", MISSING)
@@ -34,12 +34,12 @@ class SentinelValue:
34
34
value is missing
35
35
"""
36
36
37
- def __init__ (self , instance_name : str , module_name : str ) -> None :
37
+ def __init__ (self , module_name : str , instance_name : str ) -> None :
38
38
"""Initialize :class:`SentinelValue` object.
39
39
40
- :param instance_name: name of Python variable that points to the sentinel value.
41
40
:param module_name: name of Python module that hosts the sentinel value.
42
41
In the majority of cases you should pass ``__name__`` here.
42
+ :param instance_name: name of Python variable that points to the sentinel value.
43
43
"""
44
44
assert not ((module_name == self .__module__ ) and (instance_name == self .__class__ .__name__ ))
45
45
@@ -49,7 +49,7 @@ def __init__(self, instance_name: str, module_name: str) -> None:
49
49
50
50
super ().__init__ ()
51
51
52
- def __new__ (cls , instance_name , module_name ):
52
+ def __new__ (cls , module_name , instance_name ):
53
53
"""Create 1 instance of SentinelValue per name.
54
54
55
55
Usually, when you call a class, you expect to get a new instance on each call.
@@ -64,8 +64,8 @@ def __new__(cls, instance_name, module_name):
64
64
That is, if you call :class:`SentinelValue` multiple times with the same arguments,
65
65
you get the *exactly same* instance, check this out::
66
66
67
- >>> MISSING1 = SentinelValue("MISSING", __name__ )
68
- >>> MISSING2 = SentinelValue("MISSING", __name__ )
67
+ >>> MISSING1 = SentinelValue(__name__, "MISSING")
68
+ >>> MISSING2 = SentinelValue(__name__, "MISSING")
69
69
70
70
>>> MISSING1 is MISSING2
71
71
True
@@ -124,7 +124,7 @@ def __getnewargs__(self):
124
124
# This is needed for pickle serialization.
125
125
# In combination with magic in the overriden __new__() method above,
126
126
# that allows to avoid constructing duplicates when un-pickling the object.
127
- return (self .instance_name , self .module_name )
127
+ return (self .module_name , self .instance_name )
128
128
129
129
@staticmethod
130
130
def _compose_qualified_name (instance_name : str , module_name : str ) -> str :
@@ -153,7 +153,7 @@ def __bool__():
153
153
So it is often handy to do ``if not value`` to check if there is no value
154
154
(like if an attribute is set to ``None``, or not set at all)::
155
155
156
- >>> NOT_SET = SentinelValue("NOT_SET", __name__ )
156
+ >>> NOT_SET = SentinelValue(__name__, "NOT_SET")
157
157
158
158
>>> value = getattr(object, "foobar", NOT_SET)
159
159
@@ -173,9 +173,9 @@ def __bool__():
173
173
This dictionary looks like this::
174
174
175
175
{
176
- "package1.module1.MISSING": SentinelValue("MISSING ", module_name="package1.module1. MISSING"),
177
- "package2.module2.MISSING": SentinelValue("MISSING ", module_name="package2.module2. MISSING"),
178
- "package2.module2.ABSENT": SentinelValue("ABSENT ", module_name="package2.module2. ABSENT"),
176
+ "package1.module1.MISSING": SentinelValue("package1.module1 ", " MISSING"),
177
+ "package2.module2.MISSING": SentinelValue("package2.module2 ", " MISSING"),
178
+ "package2.module2.ABSENT": SentinelValue("package2.module2 ", " ABSENT"),
179
179
}
180
180
181
181
When a :class:`SentinelValue` object is instanciated, it registers itself in this dictionary
@@ -228,12 +228,12 @@ def sentinel(
228
228
module_name = _get_caller_module_name ()
229
229
assert module_name
230
230
231
- SentinelValueSubclass = _create_sentinel_value_subclass (instance_name , module_name )
231
+ SentinelValueSubclass = _create_sentinel_value_subclass (module_name , instance_name )
232
232
233
233
if repr :
234
234
SentinelValueSubclass .__repr__ = lambda self : repr # type: ignore
235
235
236
- return SentinelValueSubclass (instance_name , module_name )
236
+ return SentinelValueSubclass (module_name , instance_name )
237
237
238
238
239
239
def _get_caller_module_name () -> Optional [str ]:
@@ -253,7 +253,7 @@ def _get_caller_module_name() -> Optional[str]:
253
253
return None
254
254
255
255
256
- def _create_sentinel_value_subclass (instance_name : str , module_name : str ) -> Type [SentinelValue ]:
256
+ def _create_sentinel_value_subclass (module_name : str , instance_name : str ) -> Type [SentinelValue ]:
257
257
module = sys .modules [module_name ]
258
258
259
259
# Genarate class name from variable name.
0 commit comments