python / python/mypy

Plugin initialization hook

Offen
#5,911 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

I have already asked about augmenting AST with new nodes in the initialization hook for plugins in
https://github.com/python/mypy/issues/5910

I was able to figure it out with get_back_class_hook and dummy base class like here
https://github.com/mkurnikov/django-stubs/blob/master/django-stubs/conf/__init__.pyi#L7

There's another use case.
In Django there's an INSTALLED_APPS setting:

INSTALLED_APPS = ('app1', 'app2')

which specifies apps to use to retrieve model definitions for database schema (using models.py files).

Those could be used in the model definition code like this

from django.db import models

class User(models.Model):
    member = models.ForeignKey(to='app1.Model1')

In plugin, I can parse to= value, figure out the right type of a model, and set as return type of __get__ to have a proper value for member attribute.
For that I need to use lookup_fully_qualified
https://github.com/python/mypy/blob/master/mypy/semanal.py#L3453
and this requires module to be present in the self.modules list.

I need to create new MypyFile instances to populate that attr myself, but parse_file() method belongs to BuildManager class
https://github.com/python/mypy/blob/master/mypy/build.py#L648
and I wasn't able to find how to access it from the ClassDefContext.

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 mit ClassDefContext und verfolge, wie es mit BuildManager.parse_file() in mypy/build.py zusammenhängt. Untersuche anschließend lookup_fully_qualified() in mypy/semanal.py und die self.modules-Sammlung. Das Issue ist abgeschlossen, wenn ein Plugin eine unterstützte Möglichkeit bietet, konfigurierte Django-App-Module für die Auflösung von Modellreferenzen verfügbar zu machen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
django, python
Bereich
devtools
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.