python / python/mypy

Support file-level assertions as inline configuration

Ouverte
#19,013 4 commentaires 3 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

feature topic-configuration
Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Merge moyen
1 j 18 h
PR mergées (30 j)
54

Description

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 PtySession

and attempt importing this symbol from somewhere else, Mypy does not respect the platform condition.

For example, if you call pty.openpty() inside my_pkg._pty.unix and 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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par retracer la manière dont Mypy gère les assertions existantes de niveau supérieur concernant la plateforme et la version de Python, puis examinez le chemin de configuration ou de preprocessing dans lequel une directive de fichier inline pourrait être reconnue. Comparez l’évaluation proposée à deux variables avec le comportement actuel de la configuration de la plateforme. Le travail est considéré comme terminé lorsqu’une assertion au niveau du fichier peut activer conditionnellement la vérification sans modifier le comportement à l’exécution, avec une couverture des expressions de plateforme et de version prises en charge.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
compilers, devtools
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
42/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.