python / python/mypy

Support using Literal on __getattr__ and __getattribute__

Ouverte
#8,203 4 commentaires 24 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

feature needs discussion priority-1-normal topic-literal-types
Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Métriques de merge des PR
Métriques de PR en attente

Description

Hi! Feature request for using Literal on __getattr__ and __getattribute__.

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
from typing import Literal

LiteralStrHello = Literal["hello"]
LiteralStrWorld = Literal["world"]


class A:
    def __getattr__(self, name: LiteralStrHello) -> LiteralStrWorld:
        assert name == "hello"
        return "world"

    def __getattribute__(self, name: LiteralStrHello) -> LiteralStrWorld:
        assert name == "hello"
        return "world"
  • What is the actual behavior/output?
main.py:8: error: Invalid signature "def (main.A, Literal['hello']) -> Literal['world']" for "__getattr__"
main.py:12: error: Invalid signature "def (main.A, Literal['hello']) -> Literal['world']" for "__getattribute__"
main.py:12: error: Argument 1 of "__getattribute__" is incompatible with supertype "object"; supertype defines the argument type as "str"
Found 3 errors in 1 file (checked 1 source file)
  • What is the behavior/output you expect?
Success: no issues found in 1 source file
  • What are the versions of mypy and Python you are using? Mypy:mypy 0.770+dev.5336f272c2ac625bd40a79ab3dbbe3d548f147b8, Python:3.8.0
    Do you see the same issue after installing mypy from Git master? yes

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 exécuter la reproduction fournie dans main.py avec mypy et comparez les diagnostics signalés pour getattr et getattribute. Suivez les points d’entrée de la vérification de types pour ces méthodes spéciales ; c’est terminé lorsque la reproduction produit « Success: no issues found in 1 source file » sans affaiblir les vérifications ordinaires de object.getattribute.

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é
4/5
Temps estimé
3-5 jours
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.