You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect rendering of __replace__ method in copy docs: consider adding copy.SupportsReplace? #109961
changed the title [-]Incorrect rendering of `__replace__` method in `copy` docs[/-][+]Incorrect rendering of `__replace__` method in `copy` docs: consider adding `typing.SupportsReplace`?[/+]on Sep 27, 2023
I like the idea of SupportsReplace. I think it should be added to the copy module, though, not typing, similar to how PathLike is in the os module and AbstractContextManager is in contextlib.
We can take the same approach we do with os.PathLike and contextlib.AbstractContextManager in order to avoid importing typing in those modules: have it be an abc.ABC at runtime, but special-case it in typing so that it behaves like a protocol (and pretend in typeshed that it is a protocol). That gives us the best of both worlds.
changed the title [-]Incorrect rendering of `__replace__` method in `copy` docs: consider adding `typing.SupportsReplace`?[/-][+]Incorrect rendering of `__replace__` method in `copy` docs: consider adding `copy.SupportsReplace`?[/+]on Sep 27, 2023
Minor, but perhaps consider Replaceable? That's more in line with the collections ABCs.
I think I prefer Nikita's name, which is inline with the Supports* naming convention in the typing module -- SupportsInt, SupportsFloat, etc. "Replaceable" feels like you're making some comment about the fungibility of the object in question, which isn't necessarily true ;)
Fair enough -- the suggestion was in large part as currently it is only the typing module which uses the SupportsDunder convention. Outwith typing, the following usages appear:
typing.Protocol subclasses. All of the following are 'complex' protocols with multiple attributes, save for _Readable, which only defines the read() method.
abc.ABC subclasses or abc.ABCMeta subclasses. Contrary to my earlier note, it seems only 6 of the collections.abc types are named -ble. However, these 6 form a plurality of the 'single method' types (rounding out the set are Buffer, Container, Sized, PathLike, and various number types.
numbers.Number (& by extension the whole numeric tower)
os.PathLike
selectors.BaseSelector
So it seems consistency is not quite here! I think I sympathise most with your fungability argument, given that this replaces data within the object, not the object itself--though for immutable types the object will be replaced!
I think adding object. would be enough. There are no SupportCopy, SupportDeepCopy or SupportPickle. While currently copy.replace() only supports classes with __replace__ method, in future it may support other objects. For example it could support pure Python classes without __new__ and __init__ methods, or some pickleable classes. For now it requires the __replace__ method, but after it became widely used we can analyze patterns of its use and make defining trivial __replace__ methods unneeded.
Reacted by Adam Turner, Alex Waygood, sobolevn and Éric
While currently copy.replace() only supports classes with __replace__ method, in future it may support other objects. For example it could support pure Python classes without __new__ and __init__ methods, or some pickleable classes. For now it requires the __replace__ method, but after it became widely used we can analyze patterns of its use and make defining trivial __replace__ methods unneeded.
Ah, if that's the intention, then adding SupportsReplace would indeed be a bad idea.
Activity
[-]Incorrect rendering of `__replace__` method in `copy` docs[/-][+]Incorrect rendering of `__replace__` method in `copy` docs: consider adding `typing.SupportsReplace`?[/+]AA-Turner commentedon Sep 27, 2023
We should document this in the datamodel, similar to
__buffer__, which is also only applicable in certain cases.A
AlexWaygood commentedon Sep 27, 2023
I like the idea of
SupportsReplace. I think it should be added to thecopymodule, though, nottyping, similar to howPathLikeis in theosmodule andAbstractContextManageris incontextlib.We can take the same approach we do with
os.PathLikeandcontextlib.AbstractContextManagerin order to avoid importingtypingin those modules: have it be anabc.ABCat runtime, but special-case it intypingso that it behaves like a protocol (and pretend in typeshed that it is a protocol). That gives us the best of both worlds.sobolevn commentedon Sep 27, 2023
Yes, I agree with
copymodule. This way it would be also more convenient for users:AA-Turner commentedon Sep 27, 2023
Minor, but perhaps consider
Replaceable? That's more in line with the collections ABCs.A
[-]Incorrect rendering of `__replace__` method in `copy` docs: consider adding `typing.SupportsReplace`?[/-][+]Incorrect rendering of `__replace__` method in `copy` docs: consider adding `copy.SupportsReplace`?[/+]AlexWaygood commentedon Sep 27, 2023
I think I prefer Nikita's name, which is inline with the
Supports*naming convention in the typing module --SupportsInt,SupportsFloat, etc. "Replaceable" feels like you're making some comment about the fungibility of the object in question, which isn't necessarily true ;)AA-Turner commentedon Sep 27, 2023
Fair enough -- the suggestion was in large part as currently it is only the
typingmodule which uses theSupportsDunderconvention. Outwithtyping, the following usages appear:typing.Protocolsubclasses. All of the following are 'complex' protocols with multiple attributes, save for_Readable, which only defines theread()method.importlib.metadata.{PackageMetadata,SimplePath}importlib.resources.abc.Traversablewsgiref.types.{StartResponse,InputStream,ErrorStream,_Readable,FileWrapper}abc.ABCsubclasses orabc.ABCMetasubclasses. Contrary to my earlier note, it seems only 6 of thecollections.abctypes are named-ble. However, these 6 form a plurality of the 'single method' types (rounding out the set areBuffer,Container,Sized,PathLike, and variousnumbertypes.collections.abc.{Awaitable,AsyncIterable,Hashable,Iterable,Reversible,Callable }collections.abc.{AsyncGenerator,AsyncIterator,Buffer,ByteString,Collection,Container,Coroutine,Generator,ItemsView,Iterator,KeysView,Mapping,MappingView,MutableMapping,MutableSequence,MutableSet,Sequence,Set,Sized,ValuesView}contextlib.{AbstractContextManager,AbstractAsyncContextManager}importlib.resources.simple.SimpleReadermultiprocessing.reduction.AbstractReducernumbers.Number(& by extension the whole numeric tower)os.PathLikeselectors.BaseSelectorSo it seems consistency is not quite here! I think I sympathise most with your fungability argument, given that this replaces data within the object, not the object itself--though for immutable types the object will be replaced!
A
AlexWaygood commentedon Sep 27, 2023
Yes: objects with the
__replace__method may be fungible, but aren't necessarily fungible :)serhiy-storchaka commentedon Sep 27, 2023
Thank you for catching this @sobolevn.
I think adding
object.would be enough. There are no SupportCopy, SupportDeepCopy or SupportPickle. While currentlycopy.replace()only supports classes with__replace__method, in future it may support other objects. For example it could support pure Python classes without__new__and__init__methods, or some pickleable classes. For now it requires the__replace__method, but after it became widely used we can analyze patterns of its use and make defining trivial__replace__methods unneeded.AlexWaygood commentedon Sep 27, 2023
Ah, if that's the intention, then adding
SupportsReplacewould indeed be a bad idea.pythongh-109961: Fix incorrect rendering of `__replace__` in `library…
gh-109961: Docs: Fix incorrect rendering of `__replace__` in `copy.rs…
pythongh-109961: Use proper `module` for `copy` method docs
pythongh-109961: Docs: Fix incorrect rendering of `__replace__` in `c…
gh-109961: Use proper `module` for `copy` method docs (#110027)
serhiy-storchaka commentedon Oct 7, 2023
Can it be closed already?
pythongh-109961: Docs: Fix incorrect rendering of `__replace__` in `c…
pythongh-109961: Use proper `module` for `copy` method docs (python#1…
[3.12] pythongh-109961: Docs: Fix incorrect rendering of `__replace__…
[3.12] gh-109961: Docs: Fix incorrect rendering of `__replace__` in `…