Skip to content

Commit 72d4379

Browse files
committed
Remove Sentinel import code
No longer needed due to a correctly implemented reduce method. Simplifies code and makes syntax more predictable.
1 parent e29210a commit 72d4379

File tree

3 files changed

+0
-37
lines changed

3 files changed

+0
-37
lines changed

doc/index.rst

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,17 +1067,6 @@ Sentinel objects
10671067
>>> class MyClass:
10681068
... MISSING = Sentinel('MyClass.MISSING')
10691069

1070-
Calling the Sentinel class follows these rules for the return value:
1071-
1072-
1. If *name* and *module_name* were used in a previous call then return the same
1073-
object as that previous call.
1074-
This preserves the identity of the sentinel.
1075-
2. Otherwise if *module_name.name* already exists then return that object
1076-
even if that object is not a :class:`typing_extensions.Sentinel` type.
1077-
This enables forward compatibility with sentinel types from other libraries
1078-
(the inverse may not be true.)
1079-
3. Otherwise a new :class:`typing_extensions.Sentinel` is returned.
1080-
10811070
.. versionadded:: 4.14.0
10821071

10831072
See :pep:`661`

src/test_typing_extensions.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9316,14 +9316,6 @@ def test_sentinel_copy(self):
93169316
self.assertIs(self.SENTINEL, copy.copy(self.SENTINEL))
93179317
self.assertIs(self.SENTINEL, copy.deepcopy(self.SENTINEL))
93189318

9319-
def test_sentinel_import(self):
9320-
self.assertIs(Sentinel("TestSentinels"), TestSentinels)
9321-
self.assertIs(Sentinel._import_sentinel("TestSentinels.SENTINEL", __name__), TestSentinels.SENTINEL)
9322-
self.assertIs(Sentinel._import_sentinel("nonexistent", __name__), None)
9323-
self.assertIs(Sentinel._import_sentinel("TestSentinels.nonexistent", __name__), None)
9324-
self.assertIs(Sentinel._import_sentinel("nonexistent", ""), None)
9325-
self.assertIs(Sentinel._import_sentinel("nonexistent", "nonexistent.nonexistent.nonexistent"), None)
9326-
93279319
def test_sentinel_picklable_qualified(self):
93289320
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
93299321
self.assertIs(self.SENTINEL, pickle.loads(pickle.dumps(self.SENTINEL, protocol=proto)))

src/typing_extensions.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,31 +4211,13 @@ def __new__(
42114211
if sentinel is not None:
42124212
return sentinel
42134213

4214-
# Import sentinel at module_name.name
4215-
sentinel = cls._import_sentinel(name, module_name)
4216-
if sentinel is not None:
4217-
return _sentinel_registry.setdefault(registry_key, sentinel)
4218-
42194214
# Create initial or anonymous sentinel
42204215
sentinel = super().__new__(cls)
42214216
sentinel._name = name
42224217
sentinel.__module__ = module_name # Assign which module defined this instance
42234218
sentinel._repr = repr if repr is not None else name
42244219
return _sentinel_registry.setdefault(registry_key, sentinel)
42254220

4226-
@staticmethod
4227-
def _import_sentinel(name: str, module_name: str):
4228-
"""Return object `name` imported from `module_name`, otherwise return None."""
4229-
if not module_name:
4230-
return None
4231-
try:
4232-
module = importlib.import_module(module_name)
4233-
return operator.attrgetter(name)(module)
4234-
except ImportError:
4235-
return None
4236-
except AttributeError:
4237-
return None
4238-
42394221
def __repr__(self) -> str:
42404222
return self._repr
42414223

0 commit comments

Comments
 (0)