Skip to content

enumerate causes incompatible type mypy error #11934

Closed
@jroberts07

Description

@jroberts07

Bug Report

When using the enumerate function mypy seems to generalise the output to object instead of respecting the type of the input

To Reproduce

from typing import Union


def process(actions: Union[list[str], list[int]]) -> None:
    for pos, action in enumerate(actions):
        act(action)


def act(action: Union[str, int]) -> None:
    print(action)

Expected Behavior

Mypy raises no errors

Actual Behavior

Mypy raises the following error: Argument 1 to "act" has incompatible type "object"; expected "Union[str, int]"

Its worth noting that the same code without using enumerate does not raise this error:

from typing import Union


def process(actions: Union[list[str], list[int]]) -> None:
    for action in actions:
        act(action)


def act(action: Union[str, int]) -> None:
    print(action)

Your Environment

  • Mypy version used: 0.921
  • Python version used: 0.9

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