sqlalchemy2-stubs
sqlalchemy2-stubs copied to clipboard
Defining a GenericFunction subclass errors on `type`
Describe the bug The type attribute of ColumnElement can get a class (at least in 1.4), but also an instance. This fails in mypy. I'm not quite sure how to fix this in the stubs, therefore first an issue
Expected behavior No error
To Reproduce
from sqlalchemy import Date
from sqlalchemy.sql.functions import FunctionElement
class dummy(FunctionElement):
type = Date()
Error
x.py:6:12: error: Incompatible types in assignment (expression has type "Date", base class "ColumnElement" defined the type as "Callable[[ColumnElement[Any]], Any]")
[assignment]
type = Date()
Versions.
- OS: Linux
- Python: 3.7.1
- SQLAlchemy: 1.4.22
- mypy: 0.910
- SQLAlchemy2-stubs: 0.0.2a6
Hi,
You should use GenericFunction as the base of your function, like indicated in the documentation https://docs.sqlalchemy.org/en/14/core/functions.html?highlight=functionelement#sqlalchemy.sql.functions.GenericFunction
That said this does not work with GenericFunction. Currently it will fail both when assigning the type or the instance.
A solution could be would be to type GenericFunction as type: Union[_TE, Type[_TE]] = ... but that's not really the correct typing when then using the function, but I guess it's better than nothing. cc @bryanforbes what do you thing about this?