[question] Proper way to implement `ExceptionGroup.derive` method with the existing overloads?
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 5.1k
- Fork
- 2.1k
- Merge medio
- 1g 19h
- PR unite (30g)
- 82
Descrizione
Good afternoon,
I was working to implement some custom exception groups in an application that I work on and have had a really hard time getting the types for the derive function to work correctly. Based on reading the documentation linked below, derive is a required method when overriding an exception group to return an instance of it's own class.
Reference
https://docs.python.org/3/library/exceptions.html#BaseExceptionGroup.derive
Reproduction / Example
To reproduce this error, I'll use the example shows in the BaseExceptionGroup page, but with type hints:
from __future__ import annotations
from collections.abc import Sequence
from typing import TypeVar
_ExceptionT_co = TypeVar("_ExceptionT_co", bound=Exception, covariant=True)
_ExceptionT = TypeVar("_ExceptionT", bound=Exception)
class MyGroup(ExceptionGroup[_ExceptionT_co]):
def derive(self, excs: Sequence[_ExceptionT]) -> MyGroup[_ExceptionT]:
return MyGroup(self.message, excs)
Mypy output:
❯ mypy ~/Desktop/example.py
/Users/kkirsche/Desktop/example.py:11: error: Signature of "derive" incompatible with supertype "BaseExceptionGroup" [override]
/Users/kkirsche/Desktop/example.py:11: note: Superclass:
/Users/kkirsche/Desktop/example.py:11: note: @overload
/Users/kkirsche/Desktop/example.py:11: note: def [_ExceptionT <: Exception] derive(self, Sequence[_ExceptionT], /) -> ExceptionGroup[_ExceptionT]
/Users/kkirsche/Desktop/example.py:11: note: @overload
/Users/kkirsche/Desktop/example.py:11: note: def [_BaseExceptionT <: BaseException] derive(self, Sequence[_BaseExceptionT], /) -> BaseExceptionGroup[_BaseExceptionT]
/Users/kkirsche/Desktop/example.py:11: note: Subclass:
/Users/kkirsche/Desktop/example.py:11: note: def [_ExceptionT <: Exception] derive(self, excs: Sequence[_ExceptionT]) -> MyGroup[_ExceptionT]
Found 1 error in 1 file (checked 1 source file)
Given that this is working off an exception group rather than a base exception group, I'm unclear on what the proper way to fix this would be and was wondering if the team here was able to provide an example that satisfies mypy / typeshed's overrides for BaseExceptionGroup's derive function.
Thoughts
Would it potentially make sense to add a derive method to ExceptionGroup which returns Self rather than a hardcoded ExceptionGroup? This seems to align with how the documentation describes using this behavior, but I'm not confident I understand fully to submit a pull request with recommended change(s).
Thanks for your time and help, hope you all are having a great day.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia confrontando la documentazione di BaseExceptionGroup.derive con l’esempio MyGroup segnalato e l’output di mypy, quindi esamina gli overload esistenti di typeshed per BaseExceptionGroup.derive. Il lavoro è completato quando viene stabilita un’annotazione supportata o una modifica giustificata dello stub che risolva il conflitto di override, con validazione sulla riproduzione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100