#๐Ÿ”’ Small mypy issue

13 messages ยท Page 1 of 1 (latest)

knotty herald
#

I have the following method:

async def get_by_email(self, email: str) -> User | None:
    """Get a user by email."""
    return await self.session.scalar(select(User).where(User.email == email))

And mypy gives the following error:

error: Returning Any from function declared to return "User | None" [no-any-return]

If i change it to this though:

async def get_by_email(self, email: str) -> User | None:
      """Get a user by email."""
    result = await self.session.scalar(select(User).where(User.email == email))
    return result

The error disappears. I cant seem to find any information about why this happens. Any help would be appreciated ducky_sphere

wispy domeBOT
#

@knotty herald

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fluid citrus
knotty herald
#

For an MRE

#

This shows the error

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))
#

error: Returning Any from function declared to return "Model | None" [no-any-return]

#

And same thing, if i assign the result to a variable and return it

#

It dissapears

#

What exactly should i check with reveal_type?

half shuttle
#

There're similar issues with return await on mypy's bug tracker.
mypy hangs when checking this for me, do you have an example without sqlalchemy?

knotty herald
#

Ill see if i can make one

wispy domeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.