Mypy cannot infer types of initialized variables
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
I have the following code:
class Service:
def __init__():
self.another_service = Service()
def my_method() -> None:
return self.another_service.another_method_call()
Mypy should gives an error of incompatible types because another_method_call is not returnig None but it is not type checking because it cannot identify what is the type of another_service.
The only way to makes it gives an error is initializing another_service using:
self.another_service: Service = Service()
I think mypy has enough static information to guess what is the type of another_service wihout needing to someone explicit type it.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
In der Issue wird keine Datei und kein Test genannt. Beginne damit, das Klassenbeispiel mit und ohne die explizite Service-Annotation zu reproduzieren, und verfolge dann mypy's Typinferenz für Instanzvariablen und die zugehörigen Tests. Erledigt ist es, wenn mypy den Typ des initialisierten Attributs ableitet und den inkompatiblen Rückgabetyp meldet, ohne die Annotation zu benötigen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100