question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: 'function' object has no attribute '_yield_per' when upgrading to SQLAlchemy 1.4

See original GitHub issue
Traceback (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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
srprashcommented, Apr 19, 2021

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 the sqlalchemy.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 member load_options which is of type Class and this load_options member sneaks past this check in xray patching because both functions and classes are callables in Python. Now the load_options is patched as a function whereas this line expects it to be the original class, and therefore the AttributeError occurs.

To test this I modified the check to if name.startswith("_") or name == "load_options": to skip patching the load_options and was able to run the aplication fine. I think the fix for this would be to have a better checking mechanism than callable to differentiate a function from a class.

0reactions
srprashcommented, Jun 11, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found