Skip to content

Commit cda6fbb

Browse files
committed
Move Sentinel reduce callable to the module top-level
Changes in Sentinel such as swapping it with a theoretical typing.Sentinel will affect private methods, so the callable used with `__reduce__` must be at the top-level to be stable.
1 parent add7fdb commit cda6fbb

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/typing_extensions.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4158,6 +4158,10 @@ def evaluate_forward_ref(
41584158

41594159
_sentinel_registry = {}
41604160

4161+
def _unpickle_fetch_sentinel(name: str, module_name: str):
4162+
"""Stable Sentinel unpickling function, fetch Sentinel at 'module_name.name'."""
4163+
# Explicit repr=name because a saved module_name is known to be valid
4164+
return Sentinel(name, module_name, repr=name)
41614165

41624166
class Sentinel:
41634167
"""A sentinel object.
@@ -4253,17 +4257,11 @@ def __or__(self, other):
42534257
def __ror__(self, other):
42544258
return typing.Union[other, self]
42554259

4256-
@classmethod
4257-
def _unpickle_fetch_sentinel(cls, name: str, module_name: str):
4258-
"""Unpickle using the sentinels location."""
4259-
# Explicit repr=name because a saved module_name is known to be valid
4260-
return cls(name, module_name, repr=name)
4261-
42624260
def __reduce__(self):
42634261
"""Record where this sentinel is defined."""
4264-
# Avoid self.__class__ to ensure pickle data does not get locked to a subclass
4262+
# Reduce callable must be at the top-level to be stable whenever Sentinel changes
42654263
return (
4266-
Sentinel._unpickle_fetch_sentinel,
4264+
_unpickle_fetch_sentinel,
42674265
( # Only the location of the sentinel needs to be stored
42684266
self._name,
42694267
self._module_name,

0 commit comments

Comments
 (0)