python / python/mypy

Allow defining type for dynamically generated types.

Offen
#19,360 4 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-plugins
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Feature
Add a way to allow defining or overriding types for dynamically generated types, such as pydantic's create_model.

Pitch
Pydantic provides a way to create models dynamically (create_model).
The problem is that mypy always complain about these models:

sample code (a.py)

from pydantic import create_model, BaseModel
from pydantic.fields import Field

GeneratedModel = create_model(
    "MyModel",
    some_field=(int, Field(default=0))
)


class MyModel(BaseModel):
    a: int
    model: GeneratedModel

mypy a.py

a.py:12: error: Variable "a.GeneratedModel" is not valid as a type  [valid-type]
a.py:12: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)

Currently, you are forced to add # type: ignore[valid-type] on each line where this model is used, which is a problem because it adds a lot of noise but also disables the check for all variables in the same line as this type: ignore.

I've tried things like these, with no luck:

# attempt 1
GeneratedModel: type[Any] = create_model(...)

# attempt 2
GeneratedModel = create_model(
    "MyModel",
    some_field=(int, Field(default=0))
)

GeneratedModel = cast(type[Any], GeneratedModel)

# attempt 3
def is_pydantic_model(model: Any) -> TypeGuard[BaseModel]:
    return isinstance(model, BaseModel)

GeneratedModel = create_model(
    "MyModel",
    some_field=(int, Field(default=0))
)
is_pydantic_model(GeneratedModel)

But I always get the same [valid-type] warning everywhere where this type is used.

The actual real workaround today is to define the type in a different file and import from somewhere else, but that is already marked as bug .

edit: Fixed FieldInfo->Field

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das Beispiel in a.py mit mypy zu reproduzieren und die aktuelle valid-type-Diagnose zu überprüfen. Die Änderung ist abgeschlossen, wenn ein von pydantic's create_model erzeugter Typ in Annotationen ohne wiederholte valid-type ignores verwendet werden kann, während unabhängige Prüfungen in derselben Zeile weiterhin aktiv bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.