python / python/mypy

functools.partialmethod not supported properly?

Aperta
#8,619 4 commenti 8 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature priority-1-normal topic-plugins
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

  • 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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo mypy sul frammento Python fornito e traccia il modo in cui partialmethod gestisce eq e ne. Nell'issue non sono indicati né un file né un test; il lavoro è completato quando sia le assegnazioni sia il confronto superano il controllo dei tipi senza errori, mentre il comportamento a runtime rimane invariato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.