python / python/mypy

Add typed attribute to third party class.

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

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

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

説明

Feature

I would like to be able to add a (typed) attribute to a third-party class instance.

To give a specific example, Flask's current_app, and Flask-AppBuilder. Some example code:

    def my_view(self):
        reveal_type(current_app.appbuilder)
        if current_app.appbuilder.sm.has_access(...):
            ...

The reveal_type here shows Any: airflow/www/views.py:850: note: Revealed type is 'Any'

Things I tried:

  1. To add a typed attribute myself:

    if TYPE_CHECKING:
        current_app.appbuilder = AppBuilder(None)
        reveal_type(current_app.appbuilder)
    

    Didn't work: airflow/www/views.py:115: note: Revealed type is 'Any'

  2. A partial type stub file

    I could use stubgen etc to create a stub file for all of flask and then edit the type in flask.globals, but this presents a maintenance burden that I don't want (keeping it in sync).

    I searched for any way I could to have a "partial" stub file -- if I could have a stubs/flask/globals.pyi with out neededing all the rest of the type stubs for this module, but couldn't get it working - likely not supported.

Pitch

I don't know what the feature would look like, but for the flask ecosystem it is fairly common to add properties on to the global flask object, or the g global variable, and I would love to be able to type-check my use of them, without needing to cast it at each use.

This issue is mostly a request for help/ "is there any sensible way of doing this?"

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

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

はじめの一歩

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

調査の方向性

まず、airflow/www/views.py:850 の reveal_type の結果と、115 行目付近の TYPE_CHECKING の試行を再現します。Flask's flask.globals の型と stubgen がサードパーティ属性をどのように扱うかを確認します。Issue に、文書化された保守可能なアプローチ、または完全な置き換え用スタブなしでそのような属性を型付けするための、明確にスコープが定められた type-checker 機能があれば完了です。

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

評価

技術スタック
flask, python
領域
tooling
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

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

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