python / python/mypy

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

Aperta
#13,620 7 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@sobolevn ci sta già lavorando.

Dal 7/9/2022.

bug topic-match-statement
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.