Skip to content

Fix dialect import on SQLAlchemy 1.4#113

Merged
vgvoleg merged 1 commit into
mainfrom
issue-112-sa14-import-fix
Jun 2, 2026
Merged

Fix dialect import on SQLAlchemy 1.4#113
vgvoleg merged 1 commit into
mainfrom
issue-112-sa14-import-fix

Conversation

@vgvoleg
Copy link
Copy Markdown
Member

@vgvoleg vgvoleg commented Jun 2, 2026

Problem

import ydb_sqlalchemy raises NameError: name 'Any' is not defined under SQLAlchemy 1.4.x, so the dialect cannot be imported at all there (closes #112).

Root cause (verified)

On SQLAlchemy 1.4, @reflection.cache doesn't wrap the method with a closure — it regenerates the method from a source string (util.langhelpers.decorateformat_argspec_plus + exec) and exec's it in a namespace containing only the target/fn0 symbols. The generated source looks like:

def fn(self, connection, schema=None, **kw: Any):
    return target(fn0, self, connection, schema=schema, **kw)

The **kw: Any annotation is copied verbatim and evaluated at definition time, but Any is not in that exec namespace → NameError, raised on class definition (i.e. import).

get_view_names / get_view_definition were 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 : Any annotation from the kwargs of the two @reflection.cache view methods to match the other reflection methods. No behavior change on 2.0.

Verification

  • Reproduced the NameError on a clean SQLAlchemy 1.4.54 env with a minimal @reflection.cache + **kw: Any example, and inspected the generated wrapper source to confirm the mechanism.
  • After the fix, import ydb_sqlalchemy succeeds on SQLAlchemy 1.4.54.

`@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
@vgvoleg vgvoleg merged commit 3ece8ed into main Jun 2, 2026
6 checks passed
@vgvoleg vgvoleg deleted the issue-112-sa14-import-fix branch June 2, 2026 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dialect import fails on SQLAlchemy 1.4 (NameError: name 'Any' is not defined)

1 participant