Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return await ... not working properly #18805

Open
xAkre opened this issue Mar 15, 2025 · 0 comments
Open

return await ... not working properly #18805

xAkre opened this issue Mar 15, 2025 · 0 comments
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference

Comments

@xAkre
Copy link

xAkre commented Mar 15, 2025

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
@xAkre xAkre added the bug mypy got something wrong label Mar 15, 2025
@brianschubert brianschubert added the topic-type-context Type context / bidirectional inference label Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-context Type context / bidirectional inference
Projects
None yet
Development

No branches or pull requests

2 participants