sqlalchemy / sqlalchemy2-stubs

PEP-484 typing stubs for SQLAlchemy 1.4
MIT License
159 stars 41 forks source link

Crash when temporary base class is resolved from mypy cache #218

Open aberres opened 2 years ago

aberres commented 2 years ago

Describe the bug Given the code below I regularly run into a mypy crash when the cache is warm.

Expected behavior No crash

To Reproduce I am having a hard time to isolate the crash. It seems to be a combination of different (namespace?) packages and a change of the cache is needed. I tried to come up with a minimal example but failed.

Maybe it does ring a bell nevertheless... As the class is created in a function there is no module which can be looked up.

We ran into the issue when introducing a new test which creates a base class within the test function. Whenever we switched branches mypy crashed. The workaound is to move the instantiation out into the model.

from __future__ import annotations

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

def test_soft():
    Base = declarative_base()

    class User(Base):
        __tablename__ = "user"

        id = Column(Integer, primary_key=True)
        name = Column(String)

Error

Traceback (most recent call last):
  File "/Users/armin/venv/weplan3.10/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/armin/venv/weplan3.10/lib/python3.10/site-packages/mypy/__main__.py", line 12, in console_entry
    main(None, sys.stdout, sys.stderr)
  File "mypy/main.py", line 96, in main
  File "mypy/main.py", line 173, in run_build
  File "mypy/build.py", line 154, in build
  File "mypy/build.py", line 230, in _build
  File "mypy/build.py", line 2729, in dispatch
  File "mypy/build.py", line 3080, in process_graph
  File "mypy/build.py", line 3158, in process_fresh_modules
  File "mypy/build.py", line 1991, in fix_cross_refs
  File "mypy/fixup.py", line 26, in fixup_module
  File "mypy/fixup.py", line 91, in visit_symbol_table
  File "mypy/fixup.py", line 49, in visit_type_info
  File "mypy/types.py", line 1174, in accept
  File "mypy/fixup.py", line 156, in visit_instance
  File "mypy/fixup.py", line 282, in lookup_fully_qualified_typeinfo
  File "mypy/lookup.py", line 29, in lookup_fully_qualified
AssertionError: Cannot find module for Base

Versions.

aberres commented 2 years ago

Might be related to https://github.com/sqlalchemy/sqlalchemy2-stubs/issues/141

CaselIT commented 2 years ago

This is related to the mypy plugin. Since v2 will not use it it's unlikely that it will be fixed. PR are welcome though. (The plugin code lives in the main sqlalchemy repo)