Python -Typ Hinweise mit Standardwert als Typ [Duplikat]Python

Python-Programme
Anonymous
 Python -Typ Hinweise mit Standardwert als Typ [Duplikat]

Post by Anonymous »

unten ist ein vereinfachtes Beispiel für eine Objektfabrik in meiner Python -Anwendung. < /p>

Code: Select all

from typing import Type
class Animal:
def speak(self):
assert False

class Dog(Animal):
def speak(self):
print("bark")

def make_animal[T : Animal](animal_class: Type[T] = Animal) -> T:
return animal_class()

dog = make_animal(Dog)
dog.speak()
>>>bark
Der Code wird so ausgeführt, wie ich es erwarte.

Code: Select all

$ mypy test.py
error: Incompatible default for argument "animal_class" (default has type "type[Animal]", argument has type "type[T]")  [assignment]
$ mypy --version
mypy 1.15.0 (compiled: yes)
Habe ich etwas falsch gemacht oder meldet MyPy einen Fehler falsch?>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post