python / python/mypy

Cannot infer type of generic attributes in `match` statements when inheritance is involved

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

@sobolevn がすでに取り組んでいます。

2022年9月7日 から。

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

説明

Bug Report

mypy has trouble inferring the type of generic attributes in match statements when inheritance is involved.

This issue was encountered while investigating #13612

To Reproduce

from typing import Generic, TypeVar

T = TypeVar("T")


class Base(Generic[T]):
    ...

class A(Base[T]):
    x: T

    __match_args__ = ('x', )

    def __init__(self, x: T):
        self.x = x


a: Base[str] = A("foo")
reveal_type(a)  # Base[str] (correct)

match a:
    case A(b):
        reveal_type(b)  # Any (incorrect! Should be builtins.str)

Expected Behavior

The attribute on the last line above is revealed to be str

Actual Behavior

It is revealed to be Any

Your Environment

  • Mypy version used: mypy-0.980+dev.b031f1c04e1ee4331e4d6957c7a9b727293328a9
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.5
  • Operating system and version: n/a

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

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

はじめの一歩

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

評価

この issue はまだ評価されていません。

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

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