python / python/mypy

Order of declarations matters, but it shouldn't

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

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

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

説明

This replaces #14509.

Bug Report

The order of declarations shouldn't matter to mypy w.r.t.[^wrt] type annotations when from __future__ import annotations is in effect, but in the example that follows, they do.

To Reproduce

Analyze the following code with --strict (or run on mypy-play.net):

from __future__ import annotations

import functools
from typing import (
        Any,
        Callable,
        cast,
        Counter,
        Optional,
        TypeVar,
        )


Tc = TypeVar('Tc', bound=Callable[..., Any])


def func_wraps(wraps: Tc) -> Callable[[Callable[..., Any]], Tc]:
    '''declare that one function wraps another

    Args:
        wraps: the function being wrapped

    Returns:
        a function 'decorator()', where...
        Args:
            the function which wraps 'wraps'
        Returns:
            the input argument, unchanged

    The type annotations of the return value of the 'decorator()' will
    be those of 'wraps'.
    '''
    def inner(wrapper: Callable[..., Any], /) -> Tc:
        '''decorator

        Args:
            wrapper: the function which wraps 'wraps'
        Returns:
            the same function, unchanged
        '''
        wrapper_ = cast(Tc, wrapper)
        return functools.wraps(wraps)(wrapper_)
    return inner


def function1() -> ChildClass:
    return ChildClass()


class ParentClass:
    def __init__(self, *, param: Optional[int] = None) -> None:
        _ = param

class ChildClass(ParentClass):
    some_counter: Counter[int]

    @func_wraps(ParentClass.__init__)
    def __init__(self, *args: Any, **kwargs: Any):
        super().__init__(*args, **kwargs)
        self.some_counter = Counter()


def function2() -> ChildClass:
    return ChildClass()

Expected Behavior

There should be no errors.

Actual Behavior

The following error is issued w.r.t.[^wrt] function1:

bug.py:47: error: Returning Any from function declared to return "ChildClass"  [no-any-return]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.991, master (2023-01-24)
  • Mypy command-line flags: --show-error-codes --strict --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.8, 3.11

[^wrt]: with respect to

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

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

はじめの一歩

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

調査の方向性

まず、提供されている bug.py の再現プログラムを mypy の strict フラグ付きで実行するか、リンクされた mypy-play の例を使用し、function1 と function2 の診断を比較します。遅延アノテーションとデコレートされた初期化子に対する宣言順序の処理を追跡します。完了の条件は、function1 の no-any-return も含め、再現プログラムがエラーを報告しないことです。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

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

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