Methods with the same name as a type sometimes cause error "not valid as a type"
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
When using a decorator on a class method with the same name as a type used in its return type, and the method is decorated with a decorator that spans multiple lines, an error is thrown.
What puzzles me is that this only causes an error if the decorator spans multiple lines, not if it is just on a single line.
Neither pylint nur flake8 do complain about having a method that shadows another name in general. So I am assuming that this should be generally ok.
To Reproduce
class Foo:
@property
# the existence of this comment triggers the error
def int(self) -> int:
return 0
Some more examples show that this
- The error occurs in any situation where the decorator starts more than one line before the function definition.
- with a blank line in between
- with a comment line in between
- with a multi-line argument decorator (this is the case where I initially encountered it when using @deprecated.
- The error occurs with both built-in decorators (
@property) and custom defined decorators. - The error occurs both with built-in types and imported types.
Expected Behavior
Use the types from the outer scope to construct the return type and validate correctly.
Actual Behavior
error: Function "__main__.Foo.int" is not valid as a type [valid-type]
note: Perhaps you need "Callable[...]" or a callback protocol?
Your Environment
- Mypy version used: 1.2.0, master branch via mypy-play.net
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10 and 3.11 tested
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
レポートにある最小限の Python 例から始め、mypy 1.2.0 または現在の master を実行してから、1 行のデコレーターと、コメント、空行、または複数行の引数で区切ったバージョンを比較します。戻り値のアノテーションがチェックされる際に、メソッド名がどのように解決されるかを追跡します。示されているすべてのデコレーターのレイアウトで、外側のスコープの型が valid-type エラーなしに受け入れられれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 35/100