python / python/mypy

Ior assignment operator is flagged while equivalent __ior__() method call is not

Aperta
#11,097 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

A |= operation statement causes "Result type of | incompatible in assignment" while the equivalent method call is accepted without complaint. I haven't figured out to annotate the statement to prevent that complaint, and expect that the complaint is an error.

To Reproduce

I am composing an OrderedSet based on one of Ramond Hettinger's recipes (the weakref one), adding needed functionality and type annotations. As in the recipe, my OrderedSet class is based on collections.MutableSet. Adding an update method, this definition results in Result type of | incompatible in assignment on the "|=" statement:

def update(self, *others):
        # type: (*AbstractSet) -> None
        for o in others:
            self |= o   # Mypy Error: Result type of | incompatible in assignment

However, code for the method that is identical except for using the corresponding method call instead of the |= operator does not cause any complaints:

def update(self, *others):
        # type: (*AbstractSet) -> None
        for o in others:
            self.__ior__(o)   # No error.

I would prefer to use the |= statement, but have been unable to annotate the unaccepted statement in a way that prevents the complaint (short of using ignore). (There are other methods that suffer from similar obstacles, including the original __init__() included in Raymond's recipe. The example I'm using involves fewer incidental details.)

Expected Behavior

I believe the |= statement should be accepted without complaint.

Actual Behavior

Result type of | incompatible in assignment error on the "|=" statement.

Your Environment

  • Mypy version used: mypy 0.902
  • Python version used: Python 3.7.5
  • Operating system and version: Ubuntu Linux 18.04

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 sui frammenti forniti per l’aggiornamento di OrderedSet e confronta l’istruzione |= con la chiamata equivalente a __ior__(). Traccia il modo in cui queste due forme vengono verificate; il lavoro è completato quando la forma |= viene accettata in modo coerente con la chiamata al metodo senza un ignore.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.