Skip to content

iter(type(enum_instance)) loses type information #11784

Closed
@asottile

Description

@asottile

Bug Report

(this is extracted from a larger example)

from __future__ import annotations

import enum


class E(enum.Enum):
    A = (1,)
    B = (2,)

    @property
    def x(self) -> E:
        reveal_type(type(self).__iter__)
        reveal_type(iter(type(self)))
        reveal_type(tuple(type(self)))
        vals = tuple(type(self))
        return vals[(vals.index(self) + 1) % len(vals)]


reveal_type(iter(E))

To Reproduce

  1. mypy t.py

Expected Behavior

I expect:

$ mypy t.py
t.py:12: note: Revealed type is "def () -> typing.Iterator[t.E*]"
t.py:13: note: Revealed type is "typing.Iterator[t.E*]"
t.py:14: note: Revealed type is "builtins.tuple[t.E*, ...]"
t.py:19: note: Revealed type is "typing.Iterator[t.E*]"
Found 1 error in 1 file (checked 1 source file)

notably, iter(type(self)) should be Iterator[t.E*]

even adding self: E explicitly does not change the output

oddly enough, iter(E) correctly inferences the iterator type

Actual Behavior

$ mypy t.py
t.py:12: note: Revealed type is "def () -> typing.Iterator[t.E*]"
t.py:13: note: Revealed type is "typing.Iterator[<nothing>]"
t.py:14: note: Revealed type is "builtins.tuple[<nothing>]"
t.py:15: error: Need type annotation for "vals"
t.py:19: note: Revealed type is "typing.Iterator[t.E*]"
Found 1 error in 1 file (checked 1 source file)

(Write what happened.)

Your Environment

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions