opensearch-py
opensearch-py copied to clipboard
Incompatible types in assignment for document fields
What is the bug?
Using v2.4.1, types on fields are not inferred correctly when setting from instance methods
How can one reproduce the bug?
a contrived example:
class MyDocument(Document):
myVal = Double()
def save(self, **kwargs):
# set default value for myVal
if not self.myVal:
self.myVal = 1
return super().save(**kwargs)
running mypy would yield:
error: Incompatible types in assignment (expression has type "int", variable has type "Double") [assignment]
What is the expected behavior?
no type error
Hi @ericbuehl thanks for opening this. Could you tell us the client version you're using? Thanks!
Hi @ericbuehl thanks for opening this. Could you tell us the client version you're using? Thanks!
Sure! I am using the latest, 2.4.1
Likely introduced via #536, @ericbuehl care to write some (failing) tests? Maybe a fix?
This one is interesting. The Double
field class can be assigned to a float
, but the type system can't guess that. One possible workaround is https://github.com/opensearch-project/opensearch-py/pull/604 in which we declare a_double
as either type.
class MyDocumentWithDouble(MyDoc):
a_double: Union[float, field.Double] = field.Double()
@ericbuehl WDYT? Can we do better?
@dblock Do we want to wait for the fix for this issue before releasing 2.4.2?
@VachaShah no, I don't think it's fixable, and the workaround above is perfectly acceptable IMO, and it's also not a runtime problem