alembic icon indicating copy to clipboard operation
alembic copied to clipboard

[mypy] Errors in `env.py` after upgrade to 1.7.1

Open acelletti opened this issue 3 years ago • 4 comments

Describe the bug After upgrading from v1.6.5 to v1.7.1 mypy returns an error on our env.py where before it passed with no issue.

Expected behaviour The breaking change that moves from alembic import EnvironmentContext to from alembic.environment import EnvironmentContext should be documented, while the remaining errors should be fixed.

To Reproduce I have created a snippet of our old code that used to pass with v1.6.5 with mypy errors in the comments:

from alembic import EnvironmentContext, context # error: Module "alembic" has no attribute "EnvironmentContext"

def get_config(env_context: EnvironmentContext):
    return env_context.config

config = get_config(context)
config_2 = context.config # error: Module has no attribute "config"; maybe "configure"?

After fixing the EnvironmentContext import, I get a different error:

from alembic import context
from alembic.environment import EnvironmentContext

def get_config(env_context: EnvironmentContext):
    return env_context.config

config = get_config(context) # Argument 1 to "get_config" has incompatible type Module; expected "EnvironmentContext"
config_2 = context.config # error: Module has no attribute "config"; maybe "configure"?

acelletti avatar Sep 17 '21 11:09 acelletti

Hi,

I'm not sure what's your configuration, but EnvironmentContext was never exported at the top level by alembic:

> docker run --rm -ti python:3.9-slim bash
root@0fd3d4bc628f:/# pip install 'alembic<1.7'
Collecting alembic<1.7
  Downloading alembic-1.6.5-py2.py3-none-any.whl (164 kB)
     |████████████████████████████████| 164 kB 3.9 MB/s
Collecting python-dateutil
  Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
     |████████████████████████████████| 247 kB 9.4 MB/s
Collecting python-editor>=0.3
  Downloading python_editor-1.0.4-py3-none-any.whl (4.9 kB)
Collecting Mako
  Downloading Mako-1.1.5-py2.py3-none-any.whl (75 kB)
     |████████████████████████████████| 75 kB 3.4 MB/s
Collecting SQLAlchemy>=1.3.0
  Downloading SQLAlchemy-1.4.23-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 8.5 MB/s
Collecting greenlet!=0.4.17
  Downloading greenlet-1.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (147 kB)
     |████████████████████████████████| 147 kB 6.5 MB/s
Collecting MarkupSafe>=0.9.2
  Downloading MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
Collecting six>=1.5
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, MarkupSafe, greenlet, SQLAlchemy, python-editor, python-dateutil, Mako, alembic
Successfully installed Mako-1.1.5 MarkupSafe-2.0.1 SQLAlchemy-1.4.23 alembic-1.6.5 greenlet-1.1.1 python-dateutil-2.8.2 python-editor-1.0.4 six-1.16.0
root@0fd3d4bc628f:/# pip list | grep alembic
alembic         1.6.5
root@0fd3d4bc628f:/# python -c 'from alembic import EnvironmentContext'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name 'EnvironmentContext' from 'alembic' (/usr/local/lib/python3.9/site-packages/alembic/__init__.py)
root@0fd3d4bc628f:/#

CaselIT avatar Sep 17 '21 12:09 CaselIT

hi -

isn't this #900? it's fixed in the next release.

zzzeek avatar Sep 17 '21 13:09 zzzeek

can confirm this code absolutely does not work in 1.6.5:

from alembic import EnvironmentContext, context # error: Module "alembic" has no attribute "EnvironmentContext"

def get_config(env_context: EnvironmentContext):
    return env_context.config

config = get_config(context)
config_2 = context.config # error: Module has no attribute "config"; maybe "configure"?

output:

  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "foo/env.py", line 1, in <module>
    from alembic import EnvironmentContext, context # error: Module "alembic" has no attribute "EnvironmentContext"
ImportError: cannot import name 'EnvironmentContext' from 'alembic' (/home/classic/dev/alembic/alembic/__init__.py)

mypy wasn't really supported in 1.6.5 but against a 1.6.5 + env.py it does produce the same two errors:

foo/env.py:1: error: Module "alembic" has no attribute "EnvironmentContext"
foo/env.py:7: error: Module has no attribute "config"

the first because there is not in fact EnvironmentContext in the root alembic, the second because 1.6.5 doesn't have the mypy stubs

zzzeek avatar Sep 17 '21 13:09 zzzeek

hi -

isn't this #900? it's fixed in the next release.

not really, #900 were missing attributes in the stub file

still, I think we can close / move to discussion

CaselIT avatar Sep 17 '21 13:09 CaselIT