python / python/mypy

Constrained `TypeVar` causes issues in type narrowing in attributes

Offen
#14,412 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

A constrained typing.TypeVar object's attributes does not appear to type-narrow properly.

To Reproduce & Actual Behaviour

In the following example, an AST node transformation is trying to unravel the contents of an if typing.TYPE_CHECKING block into the owning namespace:

import ast
from typing import TypeVar

NamespaceNodeT = TypeVar("NamespaceNodeT", ast.Module, ast.ClassDef)

def unravel_TYPE_CHECKING(node: NamespaceNodeT) -> None:
    
    body_stmt: ast.stmt
    for body_stmt in list(node.body):
        if isinstance(body_stmt, ast.If) and (
            "TYPE_CHECKING" in ast.unparse(body_stmt.test)
        ):
            reveal_type(body_stmt)  # note: Revealed type is "_ast.If"
            reveal_type(body_stmt.body)  # note: Revealed type is "builtins.list[_ast.stmt]"
            if_idx: int = node.body.index(body_stmt)
            node.body[:] = [
                *node.body[:if_idx],
                *body_stmt.body,  # error: "stmt" has no attribute "body"  [attr-defined]
                *body_stmt.orelse,  # error: "stmt" has no attribute "orelse"  [attr-defined]
                *node.body[if_idx + 1 :],
            ]

The reveal_type lines see body_stmt as properly type-narrowed, but something else causes an issue when trying to use body_stmt's attributes?

Expected Behavior

I expected node: NamespaceNodeT to behave the same as node: ast.Module | ast.ClassDef, which causes no issues.

Your Environment

  • Mypy version used: 0.991 and master
  • Mypy command-line flags: (default)
  • Mypy configuration options from mypy.ini (and other config files): (default)
  • Python version used: 3.10

See mypy-play example.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, den bereitgestellten Reproducer in mypy-play mit mypy master auszuführen, und vergleiche dann die Annotation des eingeschränkten TypeVar mit der Version ast.Module | ast.ClassDef. Verfolge, wie die Typ-Eingrenzung für den Attributzugriff auf body_stmt erhalten bleibt; abgeschlossen ist die Aufgabe, wenn der Fall mit eingeschränktem TypeVar body_stmt.body und body_stmt.orelse ohne Fehler akzeptiert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.