python / python/mypy

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

Abierto
#14,673 5 comentarios 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature topic-stubgen
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza reproduciendo el informe con el ejemplo de Python mostrado y stubgen ./foo_bar -o .; después, inspecciona cómo gestiona stubgen los atributos de instancia anotados explícitamente a los que se asigna el resultado de un método que devuelve Any. Se considera completado cuando el stub generado contiene test: str en lugar de importar y usar Incomplete, sin producir el error de sintaxis indicado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
tooling
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.