python / python/mypy

Stubgen: Instance Attributes set to `Incomplete` even though explicit declared

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

Nessuno ha ancora preso questa issue.

feature topic-stubgen
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

I am using Stubgen to type my files.
Here, I have a class with a lot of instance attributes.
They are all declared within the __init__ method.
They are set using a method which returns Any as type, but manually set to the correct type:

When generating the stub file, all are set to Incomplete instead of the highlighted code.
Trying to use # type: str behind it results in a error: Critical error during semantic analysis: foo/bar.py:424242: error: invalid syntax [syntax]

To Reproduce

from typing import Any


class MyClass:

    def __init__(self) -> None:
        bar = Bar("test")
        self.test: str = bar.foo("test")


class Bar:
    def __init__(self, anything: str) -> None:
          pass

    def foo(self, test: str) -> Any:
         return test

results in:

from _typeshed import Incomplete
from typing import Any

class MyClass:
    test: Incomplete
    def __init__(self) -> None: ...

class Bar:
    def __init__(self, anything: str) -> None: ...
    def foo(self, test: str) -> Any: ...

Gist URL: https://gist.github.com/27dfb95516013fa15ed80c1411ce5a4e
Playground URL: https://mypy-play.net/?mypy=latest&python=3.9&gist=27dfb95516013fa15ed80c1411ce5a4e

Expected Behavior

I want the attribute to have the correct, annotated type.

from typing import Any

class MyClass:
    test: str
    def __init__(self) -> None: ...

class Bar:
    def __init__(self, anything: str) -> None: ...
    def foo(self, test: str) -> Any: ...

Actual Behavior

It is exported as incomplete

from _typeshed import Incomplete
from typing import Any

class MyClass:
    test: Incomplete
    def __init__(self) -> None: ...

class Bar:
    def __init__(self, anything: str) -> None: ...
    def foo(self, test: str) -> Any: ...

Your Environment

  • Mypy version used: mypy 1.0.0 (compiled: yes)
  • Mypy command-line flags: stubgen ./foo_bar -o .
  • Mypy configuration options from mypy.ini (and other config files): None used
  • Python version used: Python 3.10.9

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 riproducendo il report con l’esempio Python mostrato e stubgen ./foo_bar -o ., quindi esamina come stubgen gestisce gli attributi di istanza annotati esplicitamente a cui viene assegnato il risultato di un metodo che restituisce Any. Il lavoro è completato quando lo stub generato contiene test: str invece di importare e usare Incomplete, senza produrre l’errore di sintassi segnalato.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.