python / python/mypy

functools.partialmethod not supported properly?

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

Personne n'a encore pris cette issue.

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

Description

  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy.
import operator
from functools import partialmethod


class IntCmp:
    def __init__(self, value):
        self._value = value

    def _cmp(self, opr, other: int) -> bool:
        if isinstance(other, int):
            return opr(self._value, other)
        return NotImplemented

    __eq__ = partialmethod(_cmp, operator.eq)  # Incompatible types in assignment (expression has type "partialmethod[bool]", base class "object" defined the type as "Callable[[object, object], bool]")
    __ne__ = partialmethod(_cmp, operator.ne)  # Incompatible types in assignment (expression has type "partialmethod[bool]", base class "object" defined the type as "Callable[[object, object], bool]")
    # rest of the comparison operators (lt, le, gt, ge) type checks correctly


c = IntCmp(1)
assert c == 1  # Cannot determine type of '__eq__'
  • What is the actual behavior/output?

MyPy reports an error on the comparison, saying Cannot determine type of '__eq__', as well as incompatible types on assignment as shown.
The code correctly passes at runtime.

  • What is the behavior/output you expect?

__eq__ and __ne__ should correctly type-check the partialmethod, and comparison should give no error. This particular partialmethod usage is shown in Python docs: https://docs.python.org/3/library/functools.html#functools.partialmethod

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

Using latest git master, Python 3.8.1

  • What are the mypy flags you are using? (For example --strict-optional)
--follow-imports=silent
--show-column-numbers
--ignore-missing-imports
--check-untyped-defs

EDIT: I am aware of #2967 and #1484 being a thing, however this covers partialmethod usage which wasn't mentioned as a topic in the two other issues.

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 mypy sur l’extrait Python fourni et suivez la manière dont partialmethod gère eq et ne. Aucun fichier ni test n’est nommé dans l’issue ; c’est terminé lorsque les deux affectations et la comparaison passent la vérification de types sans erreur, tandis que le comportement à l’exécution reste inchangé.

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

Évaluation

Stack technique
python
Domaine
devtools
Type d'issue
Bug
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.