This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: forbid redefinition of specializations in singledispatch
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.snow, lukasz.langa, ncoghlan, pitrou, r.david.murray, rhettinger
Priority: normal Keywords:

Created on 2015-07-09 13:41 by pitrou, last changed 2022-04-11 14:58 by admin.

Messages (9)
msg246493 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-07-09 13:41
singledispatch currently doesn't defend against unwanted redefinition of an existing specialization, e.g.:

>>> def f(x): return "default"
... 
>>> f = functools.singledispatch(f)
>>> @f.register(int)
... def _(x): return "1"
... 
>>> @f.register(int)
... def _(x): return "2"
... 
>>> f(42)
'2'

This can be annoying when used as an extension mechanism. It would be nice if at least an option in the singledispatch() constructor could prevent this.
msg246506 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2015-07-09 14:57
Is it too late to have the default for that option be to not allow the replacement?  That would be the safer course.
msg246508 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-07-09 15:15
I don't know. I'm assuming some people actually want to redefine existing specializations.
msg246510 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2015-07-09 16:15
Sure.  I just saying that

@f.register(int, replace=True)

requires opt-in to replacing, whilst

@f.register(int, replace=False) # don't replace if one already exists

is still prone to bugs.
msg246512 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-07-09 17:15
Yes it is too late.  You'd have to do a couple of deprecation cycles to change the default.
msg246514 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-07-09 17:45
Ah, but I wasn't suggesting to add an argument to the .register() call, but to the singledispatch() call; i.e. it would be a function-wide parameter.
msg246516 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2015-07-09 18:30
Ah, I see.

So you say up-front if you are willing to have redefinition occur later.

That doesn't feel like a consenting-adults attitude, and could also make testing harder.

I prefer adding an option to the register method, and move towards making the default be "don't allow".

If we don't want to go that route, would having singledispatch issue a warning on redefinition be sufficient?
msg246528 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-07-09 21:53
Le 09/07/2015 20:30, Ethan Furman a écrit :
> 
> That doesn't feel like a consenting-adults attitude, and could also
make testing harder.

Testing of what? The point is that it's the authority providing the
generic function which decides how lenient extending the generic
function is. That sounds rather reasonable to me...

> I prefer adding an option to the register method, and move towards
> making the default be "don't allow".

But that default won't happen, for the compatibility reasons already
explained.
msg246529 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2015-07-09 22:27
I agree with Antoine.
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68785
2015-07-21 07:04:39ethan.furmansetnosy: - ethan.furman
2015-07-09 22:27:05eric.snowsetnosy: + eric.snow
messages: + msg246529
2015-07-09 21:53:50pitrousetmessages: + msg246528
2015-07-09 18:30:56ethan.furmansetmessages: + msg246516
2015-07-09 17:45:07pitrousetmessages: + msg246514
2015-07-09 17:15:53r.david.murraysetnosy: + r.david.murray
messages: + msg246512
2015-07-09 16:15:44ethan.furmansetmessages: + msg246510
2015-07-09 15:15:31pitrousetmessages: + msg246508
2015-07-09 14:57:14ethan.furmansetnosy: + ethan.furman
messages: + msg246506
2015-07-09 13:41:12pitroucreate