flask-sqlalchemy: TypeError: can't apply this __setattr__ to DefaultMeta object

I don’t know where this comes from. I have a small project where all versions are pinned and the Flask app runs in a Docker container. I just made an unrelated changes with the same versions and get this error. Does this look familiar?

Expected Behavior

Just running without crashes

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

Actual Behavior

Crashes

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 716, in __init__
    self.Model = self.make_declarative_base(model_class, metadata)
  File "/usr/local/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 798, in make_declarative_base
    model.query_class = self.Query
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/ext/declarative/api.py", line 79, in __setattr__
    _add_attribute(cls, key, value)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/ext/declarative/base.py", line 802, in _add_attribute
    type.__setattr__(cls, key, value)
TypeError: can't apply this __setattr__ to DefaultMeta object

Environment

  • Python version: Python 3.8.4 (Docker python:3.8-slim-buster)
  • Flask-SQLAlchemy version: flask-sqlalchemy==2.4.1
  • SQLAlchemy version: sqlalchemy==1.3.13

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 17
  • Comments: 18 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Wow, you guys are amazing! Thank you for resolving the issue that quickly!

I’ve just met similar problem. It happens only on Docker image python:3.8-slim-buster which now resolves to python:3.8.4-slim-buster (due to yesterday’s release). Changing Docker image to python:3.8.3-slim-buster solved problem for now. But I want to upgrade to 3.8.4 so it will be great if this issue is solved.

I hope we rescued some developers a few hours of stopped deployments 😉

Oh nice, thanks for reporting and checking the development version. If someone backports the fix to the 2.x branch, I can make a quick bugfix release.

Applying flake8 and flake8-bugbear is what caused us to start subclassing type. Code linting to the rescue!

I reported it in Python bug tracker: https://bugs.python.org/issue41295

But look carefully into report, and then check the flask-sqlalchemy code: the problem is caused, when a class inheritance from meta and non-metaclasses. And now, 2 months ago the base class for mixins included in DefaultMeta was changed from object to type. Conclusion: code from master works in Python 3.8.4, but the last released version is older.

You can check this by installing from master (pip install git+git://github.com/pallets/flask-sqlalchemy.git) and then run problematic code - should work also in 3.8.4.

Please release new version and problem (here) will be gone.

cc @zzzeek in case you see reports of this

I just confirmed @piotrgredowski hint: python:3.8.3-slim-buster works

This issue is caused by this PR from Python 3.8.4: https://github.com/python/cpython/pull/21288/files

To be more specific, the problem happens here: https://github.com/python/cpython/pull/21288/files#diff-c3cf251f16d5a03a9e7d4639f2d6f998R5952