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

Skip to content

Mypy: @declared_attr crash mypy when using "--follow-imports=silent" #10282

@k4nar

Description

@k4nar

Ensure stubs packages are not installed

  • No sqlalchemy stub packages is installed (both sqlalchemy-stubs and sqlalchemy2-stubs are not compatible with v2)

Verify if the api is typed

  • The api is not in a module listed in #6810 so it should pass type checking

Describe the typing issue

When mypy is configured to follow import but suppress error messages (eg: with --follow-imports=silent), any @declared_attr in a module imported will crash mypy >= 1.4.0.

To Reproduce

# example.py

from sqlalchemy import Column, String
from sqlalchemy.orm import Mapped, declared_attr, declarative_mixin


@declarative_mixin
class Foo:
    @declared_attr
    def bar(cls) -> Mapped[str]:
        return Column(String)


# example2.py
from example import Foo

Error

example.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.5.1
Traceback (most recent call last):
  File "mypy/checkexpr.py", line 5141, in accept
  File "mypy/nodes.py", line 2207, in accept
  File "mypy/checkexpr.py", line 4633, in visit_lambda_expr
  File "mypy/nodes.py", line 2200, in expr
IndexError: list index out of range
example.py:-1: : note: use --pdb to drop into pdb

Versions

  • OS: linux
  • Python: 3.11.3
  • SQLAlchemy: 2.0.20 & 1.4.49
  • Type checker: mypy >= 1.4.0 (for some reason, it works with mypy 1.3.1)

Additional context

The error happens in mypy at this line. This is because the Block passed when creating the LambdaExpr here is empty.

Metadata

Metadata

Assignees

No one assigned

    Labels

    SQLA mypy pluginmypy plugin issues only. general pep-484 issues should be "typing"bugSomething isn't workingwontfix / out of scopesomething we decided we aren't doing, for whatever reason

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions