Skip to content

v1.15.0 breaking checks using Coroutine #18634

@gabrielcocenza

Description

@gabrielcocenza

Bug Report

Since the new release, one of the projects that I use mypy is failing for the type Coroutine.

The code is like this:

from typing import Coroutine,

def compare_step_coroutines(
    coro1: Optional[Coroutine], coro2: Optional[Coroutine]
) -> bool:
    """Compare two coroutines.

    :param coro1: coroutine to compare
    :type coro1: Optional[coroutine]
    :param coro2: coroutine to compare
    :type coro2: Optional[coroutine]
    :return: True if coroutines are equal
    :rtype: bool
    """
    if coro1 is None or coro2 is None:
        # compare two None or one None and one Coroutine
        return coro1 == coro2

    return (
        # check if same coroutine was used
        coro1.cr_code == coro2.cr_code
        # check coroutine arguments
        and inspect.getcoroutinelocals(coro1) == inspect.getcoroutinelocals(coro2)
    )
lint: commands[2]> mypy --install-types --non-interactive .
cou/steps/__init__.py:48: error: "Coroutine[Any, Any, Any]" has no attribute "cr_code"  [attr-defined]

With this new release Coroutine doesn't recognize cr_code as a valid property.
I've tried to change to use types.CoroutineType and fixed for this issue, but errors started showing on other parts of the code. mypy still consider as Coroutine. E.g:

class Foo
def __init__(self, coro: Optional[types.CoroutineType] = None):
    self._coro: Optional[[types.CoroutineType] = coro

foo = Foo(coro=my_coro_func)

async def my_coro_func():
    return None
Argument "coro" to "Foo" has incompatible type "Coroutine[Any, Any, None]"; expected "CoroutineType[Any, Any, Any] | None"  [arg-type] note: Maybe you forgot to use "await"?

Basically Coroutine cannot be used anymore like before and I don't see a possible workaround without ignoring the line that is complaining or pinning mypy to use a version < 1.15.0

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: mypy --install-types --non-interactive .
  • Mypy configuration options from mypy.ini (and other config files):
warn_unused_ignores = true
warn_unused_configs = true
warn_unreachable = true
disallow_untyped_defs = true
ignore_missing_imports = true
exclude = [
    ".eggs",
    ".git",
    ".tox",
    ".venv",
    ".build",
    "build",
    "lib",
    "report",
    "tests",
    "docs"
]
  • Python version used:3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions