TypeError: can't apply this __setattr__ to DefaultMeta object - Flask app - python

Viewed 1948

The initial problem, I am developing on the flask app framework and I was trying out blueprint template and it update my version of python I think to 3.8.4 and this new error appear. I was on python 3.8.3

flask run

outputted a complicated error a python type Error.

     File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1264.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1264.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 87, in _run_code     
    exec(code, run_globals)
  File "D:\Openvessel\vessel-app\vessel_env\Scripts\flask.exe\__main__.py", line 9, in <module>
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 966, in main
    cli.main(prog_name="python -m flask" if as_module else None)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\core.py", line 782, in main
    rv = self.invoke(ctx)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\click\core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 848, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 305, in __init__
    self._load_unlocked()
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask\cli.py", line 240, in locate_app
    __import__(module_name)
  File "D:\Openvessel\vessel-app\Back-end\app.py", line 1, in <module>
    from vessel_app import create_app
  File "D:\Openvessel\vessel-app\Back-end\vessel_app\__init__.py", line 21, in <module>
    db = SQLAlchemy()
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 716, in __init__
    self.Model = self.make_declarative_base(model_class, metadata)
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 798, in make_declarative_base
    model.query_class = self.Query
  File "d:\openvessel\vessel-app\vessel_env\lib\site-packages\sqlalchemy\ext\declarative\api.py", line 79, in __setattr__
    _add_attribute(cls, key, value)
  File "d:\openvessel\vessel-app\vessel_env\lib\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

My last guess is that its or its the python version 3.8.4 slim has this error.

File "D:\Openvessel\vessel-app\Back-end\vessel_app\__init__.py", line 21, in <module>
db = SQLAlchemy()
5 Answers

This looks like an issue that was introduced into the python language.

To fix this:

You can revert the version of python you are building with, or if you are using docker, fix the python image temporarily using python:3.8.3-slim or an equivalent image. You can also wait until the fix is out.

You can see the related pull request here: https://github.com/python/cpython/pull/21473

Introduced here: https://bugs.python.org/issue39960

Patch bug here: https://bugs.python.org/issue41295

Another issue in an unrelated package that is the same error: flask_sqlalchemy: error with `__setattr__` to `DefaultMeta`

Git lab CI Running nose tests with SqlAlchemy. ERROR: Failure: TypeError (can't apply this __setattr__ to DefaultMeta object)

Edit: A newer version with the fix introduced is out. Check out any image of python:3.8.5 +.

I also stumbled upon this issue since upgrading to 3.8.4 and this was indeed a bug introduced in this version.
At the time of the previous answers downgrading was the best option but this issue is already solved in python 3.8.5 that came out on July 20, as stated in the changelog:

bpo-41295: Resolve a regression in CPython 3.8.4 where defining ā€œsetattrā€ in a multi-inheritance setup and calling up the hierarchy chain could fail if builtins/extension types were involved in the base types.

So now you can upgrade to the new stable version instead of downgrading.

Yes, it seems to break on the newer python version. I downgraded my python version to 3.8.2 and it worked like a charm.

I faced the same issue of recent and got it fixed using pip install git+git://github.com/pallets/flask-sqlalchemy.git

full details about this answer can be got from comment by kam193

I ran into this issue as well with python 3.8.4 and Flask-SQLAlchemy 2.4.1. I updated Flask-SQLAlchemy to v3 using Tysonic's answer, but this just introduced new errors upon starting Flask.

I was able to resolve by using pip to uninstall Flask-SQLAlchemy and install it again, which left me with 2.4.4 and everything worked normally. My venv: python 3.8.4, Flask-SQLAlchemy 2.4.4, Flask 1.1.2, SQLAlchemy 1.3.17.

Related