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

Make `copy.replace` more strongly typed by decorator-factory · Pull Request #14819 · python/typeshed · GitHub

Repository navigation

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

Filter by extension

Filter by extension .py  (1) .pyi  (1) .txt  (3) All 3 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
3 changes: 2 additions & 1 deletion stdlib/@tests/stubtest_allowlists/py313.txt
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,7 +1,8 @@
# =========================
# New errors in Python 3.13
# =========================

# No way to express keyword-only ParamSpec
copy.replace
# ====================================
# Pre-existing errors from Python 3.12
# ====================================
Expand Down
2 changes: 2 additions & 0 deletions stdlib/@tests/stubtest_allowlists/py314.txt
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 @@ -35,6 +35,8 @@ multiprocessing.process.BaseProcess.__init__
# Pre-existing errors from Python 3.13
# ====================================

# No way to express keyword-only ParamSpec
copy.replace

# =======
# >= 3.12
Expand Down
2 changes: 2 additions & 0 deletions stdlib/@tests/stubtest_allowlists/py315.txt
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 @@ -175,6 +175,8 @@ types.MappingProxyType.get

typing_extensions.Protocol # Super-special typing primitive

copy.replace # Cannot have keyword-only ParamSpec


# =============================================================
# Allowlist entries that cannot or should not be fixed; >= 3.12
Expand Down
6 changes: 3 additions & 3 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 @@ -10,7 +10,7 @@ class ReplaceableClass:
def __init__(self, val: int) -> None:
self.val = val

def __replace__(self, val: int) -> Self:
def __replace__(self, /, *, val: int) -> Self:
cpy = copy.copy(self)
cpy.val = val
return cpy
Expand All @@ -29,11 +29,11 @@ class Box(Generic[_T_co]):
def __init__(self, value: _T_co, /) -> None:
self.value = value

def __replace__(self, value: str) -> Box[str]:
def __replace__(self, /, *, value: str) -> Box[str]:
return Box(value)


if sys.version_info >= (3, 13):
box1: Box[int] = Box(42)
box2 = copy.replace(box1, val="spam")
box2 = copy.replace(box1, value="spam")
assert_type(box2, Box[str])
12 changes: 8 additions & 4 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,15 +1,17 @@
import sys
from typing import Any, Protocol, TypeVar, type_check_only
from typing_extensions import ParamSpec

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

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

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

# None in CPython but non-None in Jython
PyStringMap: Any
Expand All @@ -20,8 +22,10 @@ def copy(x: _T) -> _T: ...

if sys.version_info >= (3, 13):
__all__ += ["replace"]
# The types accepted by `**changes` match those of `obj.__replace__`.
def replace(obj: _SupportsReplace[_RT_co], /, **changes: Any) -> _RT_co: ...

def replace(
obj: _SupportsReplace[_P, _RT_co], /, *_: _P.args, **changes: _P.kwargs # does not accept positional arguments at runtime
) -> _RT_co: ...

class Error(Exception): ...

Expand Down
Loading

Back | FazBrowse Home | New Git URL