Fix dialect import on SQLAlchemy 1.4#113
Merged
Merged
Conversation
`@reflection.cache` on SQLAlchemy 1.4 regenerates the wrapped method from its source string and exec's it in a namespace that only contains the target/fn symbols. The `**kw: Any` annotation on get_view_names and get_view_definition was preserved literally in that source and failed to evaluate (NameError: name 'Any' is not defined), breaking `import ydb_sqlalchemy` entirely on 1.4. Drop the kwargs annotation to match the other reflection methods. No behavior change on SQLAlchemy 2.0. Fixes #112
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
import ydb_sqlalchemyraisesNameError: name 'Any' is not definedunder SQLAlchemy 1.4.x, so the dialect cannot be imported at all there (closes #112).Root cause (verified)
On SQLAlchemy 1.4,
@reflection.cachedoesn't wrap the method with a closure — it regenerates the method from a source string (util.langhelpers.decorate→format_argspec_plus+exec) and exec's it in a namespace containing only thetarget/fn0symbols. The generated source looks like:The
**kw: Anyannotation is copied verbatim and evaluated at definition time, butAnyis not in that exec namespace →NameError, raised on class definition (i.e. import).get_view_names/get_view_definitionwere the only reflection methods carrying a kwargs annotation; the rest use plain**kw. SQLAlchemy 2.0 reworked the decorator and no longer reproduces annotations, which is why this was invisible on 2.0.Fix
Drop the
: Anyannotation from the kwargs of the two@reflection.cacheview methods to match the other reflection methods. No behavior change on 2.0.Verification
NameErroron a clean SQLAlchemy 1.4.54 env with a minimal@reflection.cache+**kw: Anyexample, and inspected the generated wrapper source to confirm the mechanism.import ydb_sqlalchemysucceeds on SQLAlchemy 1.4.54.