[issue47030] singledispatch does not work with positional arguments with default values.

2022-03-21 Thread Andrei Kulakov


Andrei Kulakov  added the comment:

This would be problematic for two reasons:

 - possible confusion between the default function that runs when an argument 
doesn't match any registered types, and another "default" which runs when 
argument is omitted.

 - to see which function will run when argument is omitted, you would need to 
check in two places - the default value of arg and then find the registered 
function that matches it. If we end up deciding there is a need to run a 
default handler when an argument is omitted, it would be more explicit and 
convenient and visually obvious to decorate the "default if no arg" handler in 
some way, which also means there'd be a single place where this behavior is 
defined.

We can also consider adding a note to documentation that the first argument 
used for dispatch should not have a default value, to make it more explicit 
that dispatching on default value is not supported.

--
nosy: +andrei.avk

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue47030] singledispatch does not work with positional arguments with default values.

2022-03-15 Thread Randolf Scholz


New submission from Randolf Scholz :

from functools import singledispatch
from typing import Optional

@singledispatch
def load(key: Optional[str] = None, /) -> None:
raise NotImplementedError

@load.register
def _(key: None, /) -> None:
print(f"loaded {key=}") 

@load.register
def _(key: str, /) -> None:
print(f"loaded {key=}")

load()  # TypeError: load requires at least 1 positional argument

--
messages: 415274
nosy: randolf.scholz
priority: normal
severity: normal
status: open
title: singledispatch does not work with positional arguments with default 
values.
type: enhancement
versions: Python 3.10, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com