Open
Description
Bug Report
Mypy incorrectly raises an error when i return await ...
To Reproduce
I cant seem to reproduce without using SQLAlchemy, but here is an MRE with it. Its possible this issue belongs in SQLAlchemy, however i was more inclined to ask here because the second implementation works correctly
from __future__ import annotations
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class Base(DeclarativeBase):
pass
class Model(Base):
__tablename__ = "model"
id: Mapped[int] = mapped_column(autoincrement=True, primary_key=True)
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
session = AsyncSession(engine)
async def get_by_id(id: int) -> Model | None:
return await session.scalar(select(Model).where(Model.id == id))
I get the following error for get_by_id
:
error: Returning Any from function declared to return "Model | None" [no-any-return]
If i change the implementation to this though:
async def get_by_id(id: int) -> Model | None:
result = await session.scalar(select(Model).where(Model.id == id))
return result
The error dissapears
Expected Behavior
Mypy should not show an error for the first implementation
Actual Behavior
It does
Your Environment
- Mypy version used: 1.15.0
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini
(and other config files):
[tool.mypy]
python_version = "3.13"
disallow_untyped_defs = true
disallow_any_unimported = true
no_implicit_optional = true
check_untyped_defs = true
warn_return_any = true
warn_unused_ignores = true
show_error_codes = true
- Python version used: 3.13.2