Skip to content

Commit 5c0e8a7

Browse files
committed
Store Sentinel parameters in reduce method
Additional data stored as a dict to ensure backwards and forwards compatibility
1 parent cda6fbb commit 5c0e8a7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/typing_extensions.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4158,10 +4158,15 @@ 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'."""
4161+
def _unpickle_sentinel(
4162+
name: str,
4163+
module_name: str,
4164+
config: typing.Dict[str, typing.Any],
4165+
/,
4166+
):
4167+
"""Stable Sentinel unpickling function, get Sentinel at 'module_name.name'."""
41634168
# Explicit repr=name because a saved module_name is known to be valid
4164-
return Sentinel(name, module_name, repr=name)
4169+
return Sentinel(name, module_name, repr=config.get("repr", name))
41654170

41664171
class Sentinel:
41674172
"""A sentinel object.
@@ -4258,13 +4263,15 @@ def __ror__(self, other):
42584263
return typing.Union[other, self]
42594264

42604265
def __reduce__(self):
4261-
"""Record where this sentinel is defined."""
4266+
"""Record where this sentinel is defined and its current parameters."""
4267+
config = {"repr": self._repr}
42624268
# Reduce callable must be at the top-level to be stable whenever Sentinel changes
42634269
return (
4264-
_unpickle_fetch_sentinel,
4265-
( # Only the location of the sentinel needs to be stored
4270+
_unpickle_sentinel,
4271+
(
42664272
self._name,
42674273
self._module_name,
4274+
config,
42684275
),
42694276
)
42704277

0 commit comments

Comments
 (0)