Skip to content

variable annotated as tuple[SomeProtocol, ...] loses protocol information on assignment #12360

Closed
@asottile

Description

@asottile

Bug Report

re-assigning of a variable which is annotated with tuple[P, ...] loses its typing (it becomes Tuple[The, Concrete, Classes]) which breaks accessing of protocol members

To Reproduce

from __future__ import annotations

from typing import Protocol

class P(Protocol):
    def f(self) -> int: ...

class C:
    def f(self) -> int:
        return 5

class D:
    def f(self) -> int:
        return 6

x: tuple[P, ...] = ()
reveal_type(x)

x = (C(), D())
reveal_type(x)

for thing in x:
    print(thing.f())

Expected Behavior

I expect both of the reveal_types to be the same and there to be no error on access of thing.f()

Actual Behavior

$ mypy t.py
t.py:17: note: Revealed type is "builtins.tuple[t.P, ...]"
t.py:20: note: Revealed type is "Tuple[t.C, t.D]"
t.py:23: error: "object" has no attribute "f"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.941
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: python 3.8.10
  • Operating system and version: ubuntu 20.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-join-v-unionUsing join vs. using unions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions