python / python/mypy

Plugin hook to customize __repr__ of an Instance

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

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

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

説明

For example, in Django, there's this QuerySet object with a lot of methods, and to properly support those methods we need two (and maybe more in the future) generic parameters.

With get_type_analyze_hook I can convert QuerySet[MyModel] into QuerySet[MyModel, MyModel], so in the code user don't need to think about number of generics. But in error messages, all those generics values are present, though they don't have any semantics, it's just implementation detail.

Or, for Field, I need to have two generic params, one for type of __set__, one for __get__, so they appear in error message as (CharField for example)

django.db.models.fields.CharField[Union[builtins.str, builtins.int, django.db.models.expressions.Combinable], builtins.str]

None of this info makes any sense to users, so I want it to just be django.db.models.fields.CharField.

This is monkeypatching version that works for one of the cases, should be simple enough to add hook for that,

def make_queryset_repr_return_only_one_type():
    from mypy.types import TypeStrVisitor

    old_visit_instance = TypeStrVisitor.visit_instance

    def patched_visit_instance(self, t: Instance) -> str:
        if t.type.has_base(helpers.QUERYSET_CLASS_FULLNAME):
            return old_visit_instance(self, helpers.reparametrize_instance(t, [t.args[0]]))
        else:
            return old_visit_instance(self, t)

    TypeStrVisitor.visit_instance = patched_visit_instance

I can make a PR, if you approve it as a viable feature.

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

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

はじめの一歩

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

調査の方向性

TypeStrVisitor.visit_instance と既存の get_type_analyze_hook メカニズムから始め、示されている monkeypatch を動作のリファレンスとして使用してください。サポートされる plugin hook によって Instance のレンダリングをどのようにカスタマイズできるかを確認し、QuerySet と Field の例を完了条件として検討してください。実装専用のジェネリック引数は、ユーザー向けの型エラーに表示されなくなる必要があります。

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

評価

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

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

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