-
Notifications
You must be signed in to change notification settings - Fork 133
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
fix: POSTCOMPILE expansion in SQLAlchemy 1.4.27+ #408
Changes from all commits
256e044
ed93927
9133c20
5724486
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -341,16 +341,19 @@ def group_by_clause(self, select, **kw): | |
|
||
__sqlalchemy_version_info = tuple(map(int, sqlalchemy.__version__.split("."))) | ||
|
||
__expandng_text = ( | ||
__expanding_text = ( | ||
"EXPANDING" if __sqlalchemy_version_info < (1, 4) else "POSTCOMPILE" | ||
) | ||
|
||
# https://github.com/sqlalchemy/sqlalchemy/commit/f79df12bd6d99b8f6f09d4bf07722638c4b4c159 | ||
__expanding_conflict = "" if __sqlalchemy_version_info < (1, 4, 27) else "__" | ||
|
||
__in_expanding_bind = _helpers.substitute_string_re_method( | ||
fr""" | ||
\sIN\s\( # ' IN (' | ||
( | ||
\[ # Expanding placeholder | ||
{__expandng_text} # e.g. [EXPANDING_foo_1] | ||
{__expanding_conflict}\[ # Expanding placeholder | ||
{__expanding_text} # e.g. [EXPANDING_foo_1] | ||
_[^\]]+ # | ||
\] | ||
(:[A-Z0-9]+)? # type marker (e.g. ':INT64' | ||
|
@@ -431,7 +434,9 @@ def visit_notendswith_op_binary(self, binary, operator, **kw): | |
|
||
__placeholder = re.compile(r"%\(([^\]:]+)(:[^\]:]+)?\)s$").match | ||
|
||
__expanded_param = re.compile(fr"\(\[" fr"{__expandng_text}" fr"_[^\]]+\]\)$").match | ||
__expanded_param = re.compile( | ||
fr"\({__expanding_conflict}\[" fr"{__expanding_text}" fr"_[^\]]+\]\)$" | ||
).match | ||
|
||
__remove_type_parameter = _helpers.substitute_string_re_method( | ||
r""" | ||
|
@@ -521,9 +526,10 @@ def visit_bindparam( | |
|
||
assert_(param != "%s", f"Unexpected param: {param}") | ||
|
||
if bindparam.expanding: | ||
if bindparam.expanding: # pragma: NO COVER | ||
assert_(self.__expanded_param(param), f"Unexpected param: {param}") | ||
param = param.replace(")", f":{bq_type})") | ||
if self.__sqlalchemy_version_info < (1, 4, 27): | ||
param = param.replace(")", f":{bq_type})") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll need to take a closer look at this. I believe this is required to pass in the type information about sometimes ambiguous query parameters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the failing test, there ends up with an extra trailing parentheses and the type information doesn't appear to have been extracted.
It does seem this change is necessary, but I wish I understood what had changed in 1.4.27 that requires it. |
||
|
||
else: | ||
m = self.__placeholder(param) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we're missing unit test coverage of entering the
if bindparam.expanding
if statement, but SQLAlchemy >= 1.4.27I would have thought that the update to
setup.py
would make sure the tests run, though. 🤔