Callable with variadic fixed arguments that doesn't mean *args
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
Suppose I have a type of function called CommandHandler, that is a function that receives a command as its first parameter but then could have other parameters.
Examples
class Command:
pass
class RegisterUser(Command):
pass
class DeleteUser(Command):
pass
def register_user(command: RegisterUser, db : Db) -> None:
...
def delete_user(command: DeleteUser, dispatch: EventDispatcher, cache: Cache) -> None:
...
mappings : dict[Command, CommandHandler] = {
RegisterUser: register_user,
DeleteUser: delete_user
}
What would be the definition of CommandHandler?
Ideally it would be:
C= TypeVar("C", bound=Command)
CommandHandler = Callable[[C, ...], None]
But this syntax is not possible.
I've found in the documentation that Concatenate could receive ... at the end,
Concatenate is currently only valid when used as the first argument to a Callable. The last parameter to Concatenate must be a ParamSpec or ellipsis (...).
so it could be:
C= TypeVar("C", bound=Command)
CommandHandler = Callable[Concatenate[C, ...], None]
But this returns an error from Pylance:

One could say Callback Protocol could solve this like:
C= TypeVar("C", bound=Command)
class CommandHandler(Protocol):
def __call__(self, __command: C, *__args: Any) -> None: ...
But it only receives functions that has an implicit variadic argument, it doesn't work with variadic forms of the function.
So far the only way I could solve this is through the Union of many Callables, but it doesn't look like the best solution.
At the end CommandHandler can only be defined as:
C= TypeVar("C", bound=Command)
CommandHandler = Union[
Callable[[C, Any], None],
Callable[[C, Any, Any], None],
Callable[[C, Any, Any, Any], None],
Callable[[C, Any, Any, Any, Any], None],
Callable[[C, Any, Any, Any, Any, Any], None],
Callable[[C, Any, Any, Any, Any, Any, Any], None],
Callable[[C, Any, Any, Any, Any, Any, Any, Any], None],
Callable[
[C, Any, Any, Any, Any, Any, Any, Any, Any], None
],
]
Related:
https://github.com/python/cpython/issues/88954
https://stackoverflow.com/questions/57658879/python-type-hint-for-callable-with-variable-number-of-str-same-type-arguments
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der in der Issue beschriebenen Dokumentation zu Callable und Concatenate, prüfe anschließend die verwandte Issue 88954 und die verlinkte Stack Overflow-Diskussion. Vergleiche die angeforderten CommandHandler-Formen mit den aktuellen Typing-Regeln und ermittle, ob ein unterstütztes Design sie ausdrücken kann; der Abschluss sollte eine abgestimmte Spezifikation sowie einen entsprechenden Dokumentations- oder Implementierungsplan umfassen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 30/100