Warn if `__exit__` doesn't have the right signature.

Offen
#4,885 2 Kommentare 9 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
38/100
Issue-Typ
Feature
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
devtools

Rechercherichtung

Start by tracing mypy's existing checks for binary operators and its handling of the ContextManager protocol. Compare those entry points with the standard exit signature shown in the issue; done means incompatible exit methods produce a warning without rejecting compatible implementations.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

feature needs discussion priority-2-low

A typeshed user found that zipfile.ZipFile wasn't compatible with the ContextManager protocol, and it turns out that this was because its __exit__ method was declared as taking Exception instead of BaseException: python/typeshed#2043.

I imagine this kind of mistake is pretty common in user code, and it won't be noticed until somebody tries to pass the object to an API that accepts a ContextManager and the user gets an obscure error. Perhaps mypy should warn for __exit__ methods that are not compatible with the standard signature:

    def __exit__(self, t: Optional[Type[BaseException]] = ..., value: Optional[BaseException] = ...,
                 traceback: Optional[TracebackType] = ...) -> bool: ...

Doing this makes more sense for __exit__ than for many other special methods because __exit__'s signature is rather more complicated than that of other dunders, so it's easier to make mistakes. mypy already does something similar for binary operators.

Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
54

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/mypy

Alle Issues in python/mypy

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.