Support file-level assertions as inline configuration
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Feature
One would be able to do:
# mypy: assert=platform == "win32" and python_version >= "3.10"
and the file would only be checked if sys.platform is win32 and the tuple sys.version_info is greater than or equal to (3, 10). This is similar to the environment markers used in dependency definitions but with only support for the two static platform variables that are already used by Mypy.
Implementation-wise, there would be a pre-processing step that transforms the Python version string (if present) into a tuple. This way you can pass those two variables directly to eval.
Pitch
This would significantly help in situations such as https://github.com/python/mypy/issues/19009. Brief recap:
When you have the following provider module:
if sys.platform == "win32": from my_pkg._pty.windows import PtySession else: from my_pkg._pty.unix import PtySessionand attempt importing this symbol from somewhere else, Mypy does not respect the platform condition.
For example, if you call
pty.openpty()insidemy_pkg._pty.unixand run Mypy on Windows it will show:Module has no attribute "openpty" [attr-defined]
The best solution available is adding a top-level assertion as introduced here. For example, on top of the module my_pkg._pty.unix you would put assert sys.platform != "win32".
This is suboptimal because it requires modifying runtime behavior. The sys import is required, perhaps only for the assertion, and if there are imports that only exist conditionally the assertion has to be in the middle of the standard import block on top.
Adding a file-level comment is the most pragmatic option at hand, in lieu of adding an option that automatically propagates conditional static assertions to enclosed imports.
Beitragsleitfaden
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 damit nachzuverfolgen, wie Mypy mit vorhandenen Assertions auf Top-Level-Ebene für Plattformen und Python-Versionen umgeht, und untersuche dann den Konfigurations- oder Preprocessing-Pfad, in dem eine Inline-Dateidirektive erkannt werden könnte. Vergleiche die vorgeschlagene Auswertung mit zwei Variablen mit dem aktuellen Verhalten der Plattformkonfiguration. Als erledigt gilt die Aufgabe, wenn eine Assertion auf Dateiebene die Überprüfung bedingt aktivieren kann, ohne das Laufzeitverhalten zu ändern, und Abdeckung für unterstützte Plattform- und Versionsausdrücke vorhanden ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100