python / python/mypy

Failing to infer type upon concretization of generic (inferred <nothing>)

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

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

I created a generic class (Baz) which except an instance with a generic interface (Foo), and manipulates it.

I made a non-generic class (Bar), subclassing the interface with a concrete type (str).

Inside the class the types work, but when I use the non-generic class with the manipulator class, I have type errors.

I expected mypy to infer the concretization of the generic type from the usage.

I have confirmation that my typing is indeed correct: https://stackoverflow.com/questions/75568405/typing-of-using-a-subclass-of-a-generic-class?noredirect=1#comment133323604_75568405

To Reproduce


from typing import Generic, TypeVar


T = TypeVar("T")


class Foo(Generic[T]):
    def foo(self, arg: T) -> None:
        pass


class Bar(Foo[str]):
    def foo(self, arg: str) -> None:
        print(arg + " boldly runs")


T1 = TypeVar("T1")


class Baz(Generic[T1]):
    def sirRobin(self, instance: Foo[T1], param: T1) -> None:
        instance.foo(param)


# no problem, no output
Baz[str]().sirRobin(Foo(), "she")
Baz[int]().sirRobin(Foo(), 1)
print("----")
# Argument 1 to "sirRobin" of "Baz" has incompatible type "Bar"; expected "Foo[<nothing>]"  [arg-type]
# Argument 2 to "sirRobin" of "Baz" has incompatible type "str"; expected < nothing > [arg-type]
# works as expected though
Baz().sirRobin(Bar(), "he")

Playground url: https://mypy-play.net/?mypy=latest&python=3.11&gist=7bdb14547c7d7ea11185c02e6267c016

Expected Behavior
Correctly infer that T=str
No errors

Actual Behavior

Inferred T = nothing

Argument 1 to "sirRobin" of "Baz" has incompatible type "Bar"; expected "Foo[]" [arg-type]
Argument 2 to "sirRobin" of "Baz" has incompatible type "str"; expected < nothing > [arg-type]

Your Environment

  • Mypy version used: latest
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
strict = True
explicit_package_bases = True
namespace_packages = True
#mypy_path = src:test
files = src,test
mypy_path = $MYPY_CONFIG_FILE_DIR/src:$MYPY_CONFIG_FILE_DIR/test

  • Python version used: 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 con la riproduzione generica minima con Foo, Bar e Baz presente nell’issue ed eseguila con l’esempio mypy-play collegato. Traccia come una chiamata a Baz non parametrizzata inferisce T1 durante il controllo degli argomenti di sirRobin. Il lavoro è completato quando Baz().sirRobin(Bar(), "he") inferisce T1 come str e non produce errori.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.