python / python/mypy

Mypy fails to type check function signature after plugin pass

Ouverte
#18,166 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

bug topic-plugins
Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Métriques de merge des PR
Métriques de PR en attente

Description

Bug Report

I wrote a plugin to replace types, but Mypy fails to typecheck after my changes. I start off trying to fix the type error in this signature with a plugin:

def almost_id(a: str) -> str:
    return int(a)

almost_id("6")

with my plugin:

def replace_str(self, ctx: FunctionSigContext) -> FunctionLike:
        api = ctx.api

        new_signature = ctx.default_signature.copy_modified(
            ret_type=api.named_type('builtins.int')
        )

        un_anal = new_signature.definition.unanalyzed_type.copy_modified(
            ret_type = api.named_type('builtins.int')
        )

        new_signature.definition.unanalyzed_type = un_anal

        typ = new_signature.definition.type.copy_modified(
            ret_type = api.named_type('builtins.int')
        )

        new_signature.definition.type = typ

        return new_signature

I have tried going through the attrs of the functionsigcontext, api and the expr_checker but can't seem to find some other functionality to either:

  • force mypy to proceed with type checking
  • modify the existing type analysis or map
  • delay type checking until the plugin mods have been completed, something like defer() in the semantic analyzer.

To Reproduce
gist: https://gist.github.com/dibrinsofor/bddcbd2eedbb233b131bb1b8d562369a

mypy tests/str_int.py

Expected Behavior

I expect mypy to pass

Actual Behavior

tests/str_int.py:2: error: Incompatible return value type (got "int", expected "str")  [return-value]

even though printing the new signature returns:

def (a: builtins.str) -> builtins.int

Your Environment

  • Mypy version used: 1.14.0+dev.8c7db13f71098a6facea66ab165b6bc3ef688bdf
  • Mypy command-line flags: --show-traceback
  • Python version used: Python 3.11.5

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

Exécutez mypy tests/str_int.py en utilisant la reproduction du gist et comparez l’erreur signalée avec la signature affichée après replace_str. Suivez la manière dont le résultat de FunctionSigContext est utilisé pendant l’analyse sémantique et la vérification des expressions, en utilisant les chemins expr_checker et defer() mentionnés dans le rapport. La tâche est terminée lorsque le type de retour modifié par le plugin est respecté et que la reproduction réussit.

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

Évaluation

Stack technique
python
Domaine
compilers, devtools
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

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