Skip to content
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

Error when using write hooks in python < 3.9 #934

Closed
CaselIT opened this issue Sep 27, 2021 · 4 comments
Closed

Error when using write hooks in python < 3.9 #934

CaselIT opened this issue Sep 27, 2021 · 4 comments
Labels
blocker bug Something isn't working regression

Comments

@CaselIT
Copy link
Member

CaselIT commented Sep 27, 2021

Hi, I have an issue which boils down to the same point in alembic code, but I'm not using any development-only script, but [post_write_hooks] feature.

Relevant part of alembic.ini:

[post_write_hooks]

# format using "black" - use the console_scripts runner, against the "black" entrypoint
hooks = black, isort
black.type = console_scripts
black.entrypoint = black
black.options = REVISION_SCRIPT_FILENAME

isort.type = console_scripts
isort.entrypoint = isort
isort.options = REVISION_SCRIPT_FILENAME
16:18 $ poetry run alembic revision --autogenerate -m "Add foobar"
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
  Generating /home/foobar/myproject/api/migrations/versions/2021_09_27_6c36df7b6df9_add_foobar.py ...  done
  Running post write hook "black" ...
  FAILED
Traceback (most recent call last):
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/bin/alembic", line 8, in <module>
    sys.exit(main())
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/config.py", line 588, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/config.py", line 582, in main
    self.run_cmd(cfg, options)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/config.py", line 559, in run_cmd
    fn(
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/command.py", line 234, in revision
    scripts = [script for script in revision_context.generate_scripts()]
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/command.py", line 234, in <listcomp>
    scripts = [script for script in revision_context.generate_scripts()]
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/autogenerate/api.py", line 600, in generate_scripts
    yield self._to_script(generated_revision)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/autogenerate/api.py", line 505, in _to_script
    return self.script_directory.generate_revision(
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/script/base.py", line 736, in generate_revision
    write_hooks._run_hooks(path, post_write_hooks)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/script/write_hooks.py", line 84, in _run_hooks
    util.status(
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/util/messaging.py", line 56, in status
    ret = fn(*arg, **kw)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/script/write_hooks.py", line 58, in _invoke
    return hook(revision, options)
  File "/home/foobar/.cache/pypoetry/virtualenvs/someenv-py3.8/lib/python3.8/site-packages/alembic/script/write_hooks.py", line 145, in console_scripts
    "import %s; %s.%s()" % (impl.module, impl.module, impl.attr),
AttributeError: 'EntryPoint' object has no attribute 'module'

The origin of the issue is of course the same, alembic/script/write_hooks.py seems to be working with python 3.9+ as discovered above:

This happens, because the code assumes (write_hooks.py:141), that the returned object EntryPoint has a module or attr property (which it has on 3.9, but not on 3.8).

While write_pyi.py is an script for development use only, I guess that all post-write hooks deal with the same issue as well?

Originally posted by @antoni-szych-rtbhouse in #915 (comment)

@CaselIT
Copy link
Member Author

CaselIT commented Sep 30, 2021

The issue can be solved by installing importlib_metadata also on 3.8.

I wonder why the test did not catch it. I guess we need to add a covering case for this

@jmrodriguez-lab
Copy link

I'm on Python 3.8.8 with alembic==1.7.3 and installing importlib_metadata does not solve the issue.
Maybe there's something else to install? (this error happens with the black hook from the .ini example).

Any hint would be appreciated :-)

@CaselIT
Copy link
Member Author

CaselIT commented Sep 30, 2021

Sorry, I was not clear, a change in the code is still needed, changing this condition to if py39

if py38:
from importlib import metadata as importlib_metadata
else:
import importlib_metadata # type:ignore[no-redef] # noqa

will try to solve today so we can hopefully release a fix

@sqla-tester
Copy link
Collaborator

Federico Caselli has proposed a fix for this issue in the master branch:

Fix write hook issue in python < 3.9. https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/3115

abitrolly added a commit to abitrolly/warehouse that referenced this issue Oct 9, 2021
abitrolly added a commit to abitrolly/warehouse that referenced this issue Oct 9, 2021
abitrolly referenced this issue in pypi/warehouse Oct 12, 2021
Bumps [alembic](https://alembic.sqlalchemy.org) from 1.7.1 to 1.7.4.

---
updated-dependencies:
- dependency-name: alembic
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Dustin Ingram <di@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker bug Something isn't working regression
Projects
None yet
Development

No branches or pull requests

3 participants