Thanks to visit codestin.com
Credit goes to github.com

Skip to content

sqlalchemy.ext.mypy.plugin causes internal error #19014

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

Closed
ZhymabekRoman opened this issue May 2, 2025 · 3 comments
Closed

sqlalchemy.ext.mypy.plugin causes internal error #19014

ZhymabekRoman opened this issue May 2, 2025 · 3 comments
Labels

Comments

@ZhymabekRoman
Copy link

Crash Report

sqlalchemy.ext.mypy.plugin causes internal error

Traceback

/home/olge/.cache/pypoetry/virtualenvs/iswim-chat-bot-VvmrzMoq-py3.13/lib/python3.13/site-packages/advanced_alchemy/mixins/slug.py:-1: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.15.0
Traceback (most recent call last):
  File "mypy/checkexpr.py", line 5903, in accept
  File "mypy/nodes.py", line 2274, in accept
  File "mypy/checkexpr.py", line 5360, in visit_lambda_expr
  File "mypy/nodes.py", line 2267, in expr
IndexError: list index out of range
/home/olge/.cache/pypoetry/virtualenvs/iswim-chat-bot-VvmrzMoq-py3.13/lib/python3.13/site-packages/advanced_alchemy/mixins/slug.py:-1: : note: use --pdb to drop into pdb

To Reproduce
slug.py

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from sqlalchemy import Index, String, UniqueConstraint
from sqlalchemy.orm import Mapped, declarative_mixin, declared_attr, mapped_column

if TYPE_CHECKING:
    from sqlalchemy.orm.decl_base import _TableArgsType as TableArgsType  # pyright: ignore[reportPrivateUsage]


@declarative_mixin
class SlugKey:
    """Slug unique Field Model Mixin."""

    @declared_attr
    def slug(cls) -> Mapped[str]:
        """Slug field."""
        return mapped_column(
            String(length=100),
            nullable=False,
        )

    @staticmethod
    def _create_unique_slug_index(*_: Any, **kwargs: Any) -> bool:
        return bool(kwargs["dialect"].name.startswith("spanner"))

    @staticmethod
    def _create_unique_slug_constraint(*_: Any, **kwargs: Any) -> bool:
        return not kwargs["dialect"].name.startswith("spanner")

    @declared_attr.directive
    @classmethod
    def __table_args__(cls) -> TableArgsType:
        return (
            UniqueConstraint(
                cls.slug,
                name=f"uq_{cls.__tablename__}_slug",  # type: ignore[attr-defined]
            ).ddl_if(callable_=cls._create_unique_slug_constraint),
            Index(
                f"ix_{cls.__tablename__}_slug_unique",  # type: ignore[attr-defined]
                cls.slug,
                unique=True,
            ).ddl_if(callable_=cls._create_unique_slug_index),
        )

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: mypy code/
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
plugins = sqlalchemy.ext.mypy.plugin
  • Python version used: 3.13.3
  • Operating system and version: Arch Linux, 6.14.3-zen1-1-zen
@sterliakov sterliakov marked this as a duplicate of #16674 May 3, 2025
@sterliakov
Copy link
Collaborator

I'm somehow unable to reproduce with this example, but the last sample in sqlalchemy/sqlalchemy#10282 works reliably for me.

@sterliakov
Copy link
Collaborator

I am able to confirm that this is a bug in sqlalchemy plugin: they construct a LambdaExpr violating an important invariant (though undocumented, they aren't to blame) in sqlalchemy/ext/mypy/decl_class.py at line 341.

The body of any LambdaExpr must end with a ReturnStmt (lambda x: 1 is essentially a LambdaExpr containing a block of [ReturnStmt] with literal 1).

However, the plugin is officially deprecated and gone from SA main branch:

The SQLAlchemy Mypy Plugin is DEPRECATED, and will be removed in the SQLAlchemy 2.1 release. We would urge users to please migrate away from it ASAP. The mypy plugin also works only up until mypy version 1.10.1. version 1.11.0 and greater may not work properly.

So likely they won't fix it either.

@sterliakov sterliakov closed this as not planned Won't fix, can't repro, duplicate, stale May 3, 2025
@ZhymabekRoman
Copy link
Author

Thank you, @sterliakov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants