python / python/mypy

With `--disallow-any-decorated`, `Any` detection logic changes w/wo decorator

オープン
#15,211 コメント 3 件 リアクション 2 件 担当者 0 名 GitHub で見る

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

bug topic-disallow-any
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Bug Report

(A clear and concise description of what the bug is.)

We expect that adding a decorator to a method will never produce a new mypy error unless the decorator itself introduces a new Any annotation, i.e. expect no new error if the decorator itself does not introduce any additional Any.

We also believe that it is difficult to call this behavior a bug. The behavior of the option itself seems self-evident in its own way.

But we would expect a situation where simply applying a harmless decorator would not change the presence or absence of errors.

To Reproduce

Here is a test case.

[case testDisallowAnyDecoratedShouldNeverAffect]
# flags: --disallow-any-decorated

import dataclasses
from typing import Any, Callable

from typing_extensions import TypedDict, TypeVar

class CtxAny(TypedDict):
    place: Any

CallableT = TypeVar("CallableT")

def custom_decorator(func: CallableT) -> CallableT:
    return func

class Something:
    # No error
    def do(self, ctx: CtxAny) -> None:
        pass

    @classmethod
    # This emits an error, unexpected.
    def do_classmethod(cls, ctx: CtxAny) -> None:  # E: Type of decorated function contains type "Any" ("Callable[[Type[Something], CtxAny], None]")
        pass

    @custom_decorator
    # This emits an error, unexpected.
    def do_custom_decorator(self, ctx: CtxAny) -> None:  # E: Type of decorated function contains type "Any" ("Callable[[Something, CtxAny], None]")
        pass
[builtins fixtures/classmethod.pyi]

Expected Behavior

We expect do and others to be the same error-condition.

i.e. one of:

  • No mypy errors emitted.
  • All (containing do method) emits an error.
    • If some option allows us to emit an error on do method, it will be fine.

Actual Behavior

As shown in reproduction code.

Your Environment

  • Mypy version used:current master
  • Mypy command-line flags:--disallow-any-decorated
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.8.16, 3.11.3

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

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

はじめの一歩

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

調査の方向性

まず、--disallow-any-decorated と提供された builtins fixtures/classmethod.pyi fixture を使って、テストケース testDisallowAnyDecoratedShouldNeverAffect を実行します。デコレーターなし、classmethod、カスタムデコレーターの各ケースを比較し、その後、動作を一貫させ、選択したエラー条件を回帰テストでカバーします。

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

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

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

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