AttributeError: 'function' object has no attribute '_yield_per' when upgrading to SQLAlchemy 1.4
See original GitHub issueTraceback (most recent call last):
File "/Users/mvanbaak/dev/frontrow/public-api/src/fr_public_api/helpers/brandresolver.py", line 36, in resolve_brand_for_apikey
key = db.session.query(ApiKey).filter_by(key=api_key).one()
File "/Users/mvanbaak/.dotfiles/virtualenvs/fr_public_api/lib/python3.7/site-packages/aws_xray_sdk/ext/sqlalchemy/util/decorators.py", line 63, in wrapper
res = func(*args, **kw)
File "/Users/mvanbaak/.dotfiles/virtualenvs/fr_public_api/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 2730, in one
return self._iter().one()
File "/Users/mvanbaak/.dotfiles/virtualenvs/fr_public_api/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 2771, in _iter
execution_options={"_sa_orm_load_options": self.load_options},
File "/Users/mvanbaak/.dotfiles/virtualenvs/fr_public_api/lib/python3.7/site-packages/aws_xray_sdk/ext/sqlalchemy/util/decorators.py", line 63, in wrapper
res = func(*args, **kw)
File "/Users/mvanbaak/.dotfiles/virtualenvs/fr_public_api/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1616, in execute
_parent_execute_state is not None,
File "/Users/mvanbaak/.dotfiles/virtualenvs/fr_public_api/lib/python3.7/site-packages/sqlalchemy/orm/context.py", line 253, in orm_pre_session_exec
if "yield_per" in execution_options or load_options._yield_per:
AttributeError: 'function' object has no attribute '_yield_per'
Versions:
Flask==1.1.2
SQLAlchemy==1.4.2
Flask-SQLAlchemy==2.5.1
boto3==1.17.35
botocore==1.20.36
aws-xray-sdk==2.7.0
I get the same error with SQLAlchemy 1.4.0 and 1.4.1, things work fine with 1.3.23.
Confirmed it’s an xray issue with the following change to my code which made things work again:
-db = XRayFlaskSqlAlchemy(model_class=Base)
+db = SQLAlchemy(model_class=Base)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Developers - AttributeError: 'function' object has no attribute ...
AttributeError : 'function' object has no attribute '_yield_per' when upgrading to SQLAlchemy 1.4.
Read more >1.4 Changelog — SQLAlchemy 2.0 Documentation
In a related change, fixed issue where an object that contains a loaded ... causing in-place upgrades of SQLAlchemy to no longer be...
Read more >Asynchronous I/O (asyncio) - SQLAlchemy 1.4 Documentation
The program can freely switch between async/await code and contained functions that use sync code with virtually no performance penalty. There ...
Read more >Working with Engines and Connections — SQLAlchemy 2.0 ...
This section describes how to use transactions when working directly with Engine and Connection objects. When using the SQLAlchemy ORM, the public API...
Read more >Error Messages - SQLAlchemy 1.4 Documentation
The <some function> in SQLAlchemy 2.0 will no longer <something>; Object is ... used inline before it has been mapped, however this cprop...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Upon further investigation, i can say that the root cause of this issue is in the method
decorate_all_functions
which monkey-patches all the non-private functions in thesqlalchemy.orm.Query
class with a xray wrapper function.However, the problem started with v1.4.0 of sqlalchemy where the
sqlalchemy.orm.Query
class introduced a new non-private memberload_options
which is of typeClass
and thisload_options
member sneaks past this check in xray patching because both functions and classes arecallables
in Python. Now theload_options
is patched as a function whereas this line expects it to be the original class, and therefore theAttributeError
occurs.To test this I modified the check to
if name.startswith("_") or name == "load_options":
to skip patching theload_options
and was able to run the aplication fine. I think the fix for this would be to have a better checking mechanism thancallable
to differentiate a function from a class.Hey @peterdeme Yes. The fix was released in v2.8.0 of the SDK. Please see the changelog here:https://github.com/aws/aws-xray-sdk-python/blob/master/CHANGELOG.rst#280 Sorry we missed on updating the issue about the release. I’ll close this now. Feel free to open a new one if you run into any problems. Thanks