Repository navigation
Closed as not planned
Labels
bugmypy got something wrongmypy got something wrong
Description
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 NoneArgument "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
Labels
bugmypy got something wrongmypy got something wrong
Activity
Pin mypy to not block lint.
A5rocks commentedon Feb 7, 2025
This is intentional. You can have coroutines without
cr_code. This works:Comment I left on typeshed for more information: python/typeshed#10816 (comment)
A5rocks commentedon Feb 7, 2025
Note of course that the more correct thing to check is
isinstance(coro, types.CoroutineType):Pin mypy to not block lint.
Pin mypy to not block lint.
Pin mypy to not block lint. (#609)
Pin mypy to not block lint. (canonical#609)