flask-sqlalchemy icon indicating copy to clipboard operation
flask-sqlalchemy copied to clipboard

Metaclasses do not accept additional keyword arguments

Open indietyp opened this issue 4 years ago • 0 comments

Currently, the metaclass mixins provided by flask-sqlalchemy do not accept additional keyword arguments. Due to that, it breaks compatibility to the __init_subclass_ hook.

The bug was present in SQLAlchemy, and was fixed in #5357, refer to documentation about the canonical inclusion of kwargs.

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, Integer

db = SQLAlchemy()


class Mixin:
    def __init_subclass__(cls, *, default=None, **kwargs):
        super().__init_subclass__(**kwargs)

        cls.default = default


class Model(db.Model, Mixin, default=2):
    id = Column(Integer, primary_key=True)

The execution of the snippet will result in:

Traceback (most recent call last):
  File "[omitted]", line 14, in <module>
    class Model(db.Model, Mixin, default=2):
TypeError: __init__() got an unexpected keyword argument 'default'

The instantiation of the class should not fail, and default should be propagated to the __init_subclass__ hook.

Environment:

  • Python version: 3.9.7
  • Flask-SQLAlchemy version: 2.5.1
  • SQLAlchemy version: 1.4.23

indietyp avatar Sep 23 '21 09:09 indietyp