python / python/mypy

Assigning wrapped class members to a member defined by a base class fails with incompatible types in assignment

Aperta
#14,408 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-inference topic-type-context
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Consider the following code, where wrap is some decorator that preserves the function signature:

class Base:
    def f(self, a: int) -> int:
        return a + 1

    def g(self, a: int) -> int:
        return a + 1

class Derived(Base):
    @wrap
    def f(self, a: int) -> int:
        return a + 2

    def _g(self, a: int) -> int:
        return a + 2 

    g = wrap(_g)

The assignment g = wrap(_g) fails with Argument 1 to "wrap" has incompatible type "Callable[[Dervied, int], int]"; expected "Callable[[Base, int], int]". I would have expected both ways of wrapping a function to be the same (ans as far as I can tell they do so at runtime) and therefore both should be allowed.

This is also not a general issue with assignments as the following code type checks:

class Base:
    def f(self, a: int) -> int:
        return a + 1

class Derived(Base):
    def _f(self, a: int) -> int:
        return a + 2

    f = _f

To Reproduce

https://mypy-play.net/?mypy=master&python=3.11&gist=2455e6265bf5ec9cc5ffac2d95accf98

Note that mypy-play seems to use c660354846688ff8158d0f0178eb298171b74f5b rather than the latest commit from master for some reason. The output on current master is slightly different (the type of C.abc is no longer consider def (self: __main__.A, a: builtins.int) -> builtins.int):

test_assign_function_subclass.py:32: error: Incompatible types in assignment (expression has type "Callable[[C, int], int]", base class "A" defined the type as "Callable[[A, int], int]")  [assignment]
test_assign_function_subclass.py:50: error: Cannot determine type of "abc"  [has-type]
test_assign_function_subclass.py:55: note: Revealed type is "def (self: test_assign_function_subclass.A, a: builtins.int) -> builtins.int"
test_assign_function_subclass.py:56: note: Revealed type is "def (self: test_assign_function_subclass.B, a: builtins.int) -> builtins.int"
test_assign_function_subclass.py:57: error: Cannot determine type of "abc"  [has-type]
test_assign_function_subclass.py:57: note: Revealed type is "Any"
test_assign_function_subclass.py:58: note: Revealed type is "def (self: test_assign_function_subclass.D, a: builtins.int) -> builtins.int"
test_assign_function_subclass.py:59: note: Revealed type is "def (self: test_assign_function_subclass.E, a: builtins.int) -> builtins.int"
Found 3 errors in 1 file (checked 1 source file)

Expected Behavior

No error, code type checks

Actual Behavior

main.py:32: error: Argument 1 to "wrap" has incompatible type "Callable[[C, int], int]"; expected "Callable[[A, int], int]"  [arg-type]

Your Environment

  • Mypy version used: 98cc165a657a316accb93f1ed57fdc128b086d9f
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10 & 3.11

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 il riproduttore mypy-play collegato con la versione corrente di mypy e confronta la forma con decorator con la forma di assegnazione diretta. Analizza come mypy verifica i membri di classe sottoposti a wrapping rispetto ai membri ereditati; il lavoro è completo quando entrambi gli esempi equivalenti superano il controllo dei tipi senza gli errori incompatible-argument o assignment.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.