python / python/mypy

truthy-function incorrectly reported on sqlalchemy's hybrid_property with expression

Open
#14,314 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug false-positive topic-descriptors
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

import sqlalchemy as sa
from sqlalchemy.orm import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property


Base = declarative_base()


class Foo(Base):
    deleted_at = sa.Column(sa.DateTime(), nullable=True)

    @hybrid_property
    def is_deleted(cls) -> bool:
        return cls.deleted_at is not None

    @is_deleted.expression  # type: ignore[no-redef]
    def is_deleted(cls, v):
        return cls.deleted_at.is_not(None)


foo = Foo()

assert foo.is_deleted

Expected Behavior

mypy should report no error.

Actual Behavior

mypy reports the following error on the last line:

error: Function "Callable[..., Any]" could always be true in boolean context  [truthy-function]

BTW, if there is no @is_deleted.expression,just one @hybrid_property,mypy reports no error.

Your Environment

  • mypy version: 0.991
  • python version: 3.11.0
  • sqlalchemy2-stubs version: 0.0.2a29
  • mypy config (in pyproject.toml)
[tool.mypy]
plugins = ['sqlalchemy.ext.mypy.plugin']
show_error_codes = true
warn_unused_ignores = true

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported snippet with mypy 0.991, the SQLAlchemy plugin, and the shown configuration, then compare behavior with and without the @is_deleted.expression decorator. Trace how the hybrid_property expression is typed at the final assert; done means the false-positive truthy-function error no longer appears while the reported case remains checked.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlalchemy
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.