You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But he noted that this causes errors when using keyword arguments to the model classes. I spent a little while figuring this out, so I wanted to share my solution for that issue:
When you don't define an __init__ method on your class, it ends up with sqlalchemy.orm.decl_base._declarative_constructor as the __init__ method, which is then additionally wrapped by SQLAlchemy instrumentation functions. If you'd like to use keyword arguments, you should define a custom __init__ method which wraps this function:
With that, you can specify whatever mix of required and optional keywords you like. The signature for _declarative_constructor is: _declarative_constructor(self: Any, **kwargs: Any) -> None, so using kw-only args ensures that the interface is the same. Now Mypy will understand what kw-args are allowed for your class.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm opening this discussion for workaround to the previous discussed issues using mypy with flask-sqlalchemy:
#1327
#1186
In #1327, @fhd documented the workaround:
But he noted that this causes errors when using keyword arguments to the model classes. I spent a little while figuring this out, so I wanted to share my solution for that issue:
When you don't define an
__init__
method on your class, it ends up withsqlalchemy.orm.decl_base._declarative_constructor
as the__init__
method, which is then additionally wrapped by SQLAlchemy instrumentation functions. If you'd like to use keyword arguments, you should define a custom__init__
method which wraps this function:With that, you can specify whatever mix of required and optional keywords you like. The signature for
_declarative_constructor
is:_declarative_constructor(self: Any, **kwargs: Any) -> None
, so using kw-only args ensures that the interface is the same. Now Mypy will understand what kw-args are allowed for your class.Beta Was this translation helpful? Give feedback.
All reactions