mirror of
https://github.com/davidhalter/typeshed.git
synced 2026-07-09 09:59:16 +08:00
Refine the copy._SupportsReplace.__replace__ signature (#14786)
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import sys
|
||||
from typing import Generic, TypeVar
|
||||
from typing_extensions import Self, assert_type
|
||||
|
||||
|
||||
@@ -19,3 +20,20 @@ if sys.version_info >= (3, 13):
|
||||
obj = ReplaceableClass(42)
|
||||
cpy = copy.replace(obj, val=23)
|
||||
assert_type(cpy, ReplaceableClass)
|
||||
|
||||
|
||||
_T_co = TypeVar("_T_co", covariant=True)
|
||||
|
||||
|
||||
class Box(Generic[_T_co]):
|
||||
def __init__(self, value: _T_co, /) -> None:
|
||||
self.value = value
|
||||
|
||||
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")
|
||||
assert_type(box2, Box[str])
|
||||
|
||||
Reference in New Issue
Block a user