python / python/mypy

Plugin initialization hook

オープン
#5,911 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

feature priority-2-low topic-plugins
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

ClassDefContext から始め、mypy/build.py の BuildManager.parse_file() とどのように関連しているかを追跡し、次に mypy/semanal.py の lookup_fully_qualified() と self.modules コレクションを調べます。プラグインが、設定された Django アプリモジュールをモデル参照の解決に利用可能にする、サポートされた方法を備えた時点で、この issue は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
django, python
領域
devtools
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。