defining self.__call__ makes the class callable when it isn't

Aperta
#2,113 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Start by running the example with mypy and Python 2, confirming that instance-level call is accepted statically but rejected at runtime. Trace mypy's handling of callable checks and instance attribute assignments; done means the checker reports this mismatch or otherwise matches Python's behavior.

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

Descrizione

bug priority-1-normal topic-runtime-semantics

I thought I had a found a great workaround for a problem I'm having. I'll define __call__ in the __init__

class A(object):

    def __init__(self):
        # type: () -> None
        self.__call__ = self.stuff

    def stuff(self, a):
        # type: (int) -> int
        return a + 2

a = A()
print a(7)

Mypy has no issues with this code at all. However python2 disagrees:

Traceback (most recent call last):
  File "example.py", line 12, in <module>
    print a(7)
TypeError: 'A' object is not callable

Looks like __call__ has to be defined on the class not the instance.

Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

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.

Altre issue di python/mypy

Tutte le issue di python/mypy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.