python / python/mypy

Constrained `TypeVar` causes issues in type narrowing in attributes

Open
#14,412 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied reproducer in mypy-play with mypy master, then compare the constrained TypeVar annotation with the ast.Module | ast.ClassDef version. Trace how type narrowing is preserved for body_stmt attribute access; done means the constrained TypeVar case accepts body_stmt.body and body_stmt.orelse without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.