Closed kasium closed 7 months ago
Hi,
The linked issues seems to provide the solution, ie manually adding the module to the sys.modules dict. I'm not sure if there is a reason for not adding them in alembic, @zzzeek do you know if it was left out on purpose?
Interestingly when alembic still supported py27 this code actively removed the module from sys.modules in the py2 code: https://github.com/sqlalchemy/alembic/blob/b1e5a23c0f53482dd06b013ce8a9453dd0e5eb5d/alembic/util/compat.py#L176-L241
But I guess if the current behaviour is wanted we could add the module to sys.modules
, exec it, remove the module from sys.modules
Hi,
The linked issues seems to provide the solution, ie manually adding the module to the sys.modules dict. I'm not sure if there is a reason for not adding them in alembic, @zzzeek do you know if it was left out on purpose?
it is on purpose, since these aren't "modules" that are imported by "import", and I didnt think we wanted these python modules to live in the Python interpreter permanently until the interpreter ends..there could be hundreds of files taking up memory etc.
we do the same thing in mako FYI. so...might want to try over there too.
(which...how do you clean up ...shrugs)
well in python2 alembic was quite blunt about it del sys.modules[module_name]
. Maybe it works also on python3?
Alternatively, since this seems a very rare case we do a "try current version; catch Attribute error and re-try with the module added in sys.modules"?
trying to clean up after the fact seems kind of messy
at the moment I feel like new config flag in alembic.ini that turns on "add to sys.modules ; note: migration files will remain persistent in the python interpreter until interpreter close unless removed manually" somethign like that
don't really like having yet another config knob, but it's probably the best option
any chance someone reported this as a shortcoming in python dataclasses?
I think what python does here is very similar to what sqlalchemy does to evaluate a string annotation: exec it using the module dict to use the proper globals.
Not sure if anything different could be done in the dataclass module
honestly I'm not 100% it makes sense adding a new config flag just for this, since it's utility seems very low.
I'm for closing with "won't fix", since the workaround is simple: define the dataclass on another module and import it from the migration.
OK that's fine
Thanks for having a look
Describe the bug If
from __future__ import annotations
is used with adataclass
, alembic crashesExpected behavior No crash
To Reproduce
alembic heads
Error
Versions.
Additional context Found also other project suffering from this issue: https://github.com/mkdocs/mkdocs/issues/3141
Have a nice day!