`return await ...` not working properly
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il riproduttore Python fornito usando SQLAlchemy's AsyncSession.scalar e confronta la forma con await inline con la forma che usa una variabile locale con mypy 1.15.0 e la configurazione warn_return_any mostrata. Traccia il modo in cui mypy gestisce l'espressione sottoposta ad await e verifica la correzione con un test di regressione che dimostri che entrambe le forme evitano l'errore no-any-return.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python, sqlalchemy, sqlite
- Ambito
- databases, devtools, tooling
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100