Skip to content

Regression with narrowing with multiple hasattr #20596

Closed

Description

@hauntsaninja
[case testMultipleHasAttr]
# flags: --warn-unreachable
from __future__ import annotations
from typing import Any

def len(obj: object) -> Any: ...

def f(x: type | str):
    if (
        hasattr(x, "__origin__")
        and x.__origin__ is list
        and hasattr(x, "__args__")
        and len(x.__args__) == 1
    ):
        reveal_type(x.__args__[0])  # N: Revealed type is "Any"
[builtins fixtures/module.pyi]

Regression introduced in #20492 that I don't yet have fixed in my commit stack. Probably just need to manage the conditional type maps a little more carefully

Activity

  1. hauntsaninja commented on Jan 29, 2026

    @hauntsaninja
    CollaboratorAuthor

    I have a patch that fixes this that does some reordering of how things get set in the output_map in propagate_up_typemap_info... but it doesn't feel principled and I don't like it

    Maybe if I can clean up some more type map logic a better fix will reveal itself

  2. added a commit that references this issue on Apr 13, 2026
    ff31bb2
  3. added a commit that references this issue on Apr 21, 2026
    210f518
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions