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
AA-Turner commented on Sep 27, 2023
We should document this in the datamodel, similar to __buffer__, which is also only applicable in certain cases.
A
AlexWaygood commented 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.
sobolevn commented on Sep 27, 2023
Yes, I agree with copy module. This way it would be also more convenient for users:
AA-Turner commented on Sep 27, 2023
Minor, but perhaps consider Replaceable? That's more in line with the collections ABCs.
A
AlexWaygood commented on 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 commented on Sep 27, 2023
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:
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!
A
AlexWaygood commented on Sep 27, 2023
Yes: objects with the __replace__ method may be fungible, but aren't necessarily fungible :)
serhiy-storchaka commented on Sep 27, 2023
Thank you for catching this @sobolevn.
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.
AlexWaygood commented on Sep 27, 2023
Ah, if that's the intention, then adding SupportsReplace would 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 commented on 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 `…