FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix copy.replace widening a bound TypeVar to its bound by apoorva-01 · Pull Request #15997 · python/typeshed · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .pyi  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 26 additions & 0 deletions stdlib/@tests/test_cases/check_copy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import copy
import sys
from dataclasses import dataclass
from typing import Generic, TypeVar
from typing_extensions import Self, assert_type

Expand Down Expand Up @@ -37,3 +38,28 @@ def __replace__(self, value: str) -> Box[str]:
box1: Box[int] = Box(42)
box2 = copy.replace(box1, val="spam")
assert_type(box2, Box[str])


# Regression test for #15973: replace() must preserve a bound TypeVar whose
# __replace__ returns Self (the dataclass/namedtuple case), rather than widening
# to the bound.
@dataclass(frozen=True)
class BaseConfig:
pg_ssl_key: str | None = None


@dataclass(frozen=True)
class SubConfig(BaseConfig):
pg_host: str = "localhost"


_ConfigT = TypeVar("_ConfigT", bound=BaseConfig)

if sys.version_info >= (3, 13):

def replace_config(config: _ConfigT) -> _ConfigT:
result = copy.replace(config, pg_ssl_key="replaced")
assert_type(result, _ConfigT)
return result

assert_type(copy.replace(SubConfig(), pg_host="example.com"), SubConfig)
17 changes: 15 additions & 2 deletions stdlib/copy.pyi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import sys
from typing import Any, Protocol, TypeVar, type_check_only
from typing import Any, Protocol, TypeVar, overload, type_check_only
from typing_extensions import Self

__all__ = ["Error", "copy", "deepcopy"]

_T = TypeVar("_T")
_RT_co = TypeVar("_RT_co", covariant=True)

@type_check_only
class _SupportsReplace(Protocol[_RT_co]):
class _SupportsReplaceSelf(Protocol):
# In reality doesn't support args, but there's no great way to express this.
def __replace__(self, /, *_: Any, **changes: Any) -> Self: ...

@type_check_only
class _SupportsReplace(Protocol[_RT_co]):
def __replace__(self, /, *_: Any, **changes: Any) -> _RT_co: ...

_SR = TypeVar("_SR", bound=_SupportsReplaceSelf)

# None in CPython but non-None in Jython
PyStringMap: Any

Expand All @@ -20,7 +27,13 @@ def copy(x: _T) -> _T: ...

if sys.version_info >= (3, 13):
__all__ += ["replace"]

# The types accepted by `**changes` match those of `obj.__replace__`.
# When `__replace__` returns `Self`, keep the argument's own type (so a bound
# TypeVar is preserved); otherwise return whatever `__replace__` declares.
@overload
def replace(obj: _SR, /, **changes: Any) -> _SR: ...
@overload
def replace(obj: _SupportsReplace[_RT_co], /, **changes: Any) -> _RT_co: ...

class Error(Exception): ...
Expand Down
Loading

Back | FazBrowse Home | New Git URL