Open kasium opened 3 years ago
Hi,
This is working as expected if you correctly map DummyTable. Sadly to get rid of Cannot assign to a method [assignment]
you need to change name of the setter function.
from sqlalchemy import Column, Date, Time
from sqlalchemy.ext.hybrid import hybrid_property
from datetime import datetime
from sqlalchemy.orm import declarative_base
Base = declarative_base()
class DummyTable(Base):
create_date = Column("create_date", Date)
create_time = Column("create_time", Time)
@hybrid_property
def create_timestamp(self) -> datetime:
"""create time property"""
return datetime.combine(self.create_date, self.create_time)
@create_timestamp.setter
def _(self, value: datetime) -> None:
"""create time setter"""
self.create_date = value.date()
self.create_time = value.time()
DummyTable().create_timestamp = datetime.now()
Thanks a lot!
So, I just tried to rename the functions, but this doesn't work. It seems like that the function names must be same resulting in mypy issues: https://docs.sqlalchemy.org/en/14/orm/extensions/hybrid.html#defining-expression-behavior-distinct-from-attribute-behavior
I've only checked if it fixed mypy error, I had not tried sqlalchemy.
I'm not sure if we can tell mypy to be happy about the name of the functions
Also, if I just ignore the duplicate names (# type:ignore[no-redef]), then mypy still complains about overloaded functions. I guess, mypy can't figure out which function to use in which case. Mabye the easiest way is, to allow SQLAlchemy to use any function name
Describe the bug Using hybrid_property leads to several errors
Expected behavior No errors are returned
To Reproduce
Error
Versions.