opensearch-py icon indicating copy to clipboard operation
opensearch-py copied to clipboard

Incompatible types in assignment for document fields

Open ericbuehl opened this issue 1 year ago • 6 comments

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

ericbuehl avatar Nov 17 '23 17:11 ericbuehl

Hi @ericbuehl thanks for opening this. Could you tell us the client version you're using? Thanks!

harshavamsi avatar Nov 17 '23 17:11 harshavamsi

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

ericbuehl avatar Nov 17 '23 17:11 ericbuehl

Likely introduced via #536, @ericbuehl care to write some (failing) tests? Maybe a fix?

dblock avatar Nov 17 '23 19:11 dblock

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 avatar Nov 17 '23 21:11 dblock

@dblock Do we want to wait for the fix for this issue before releasing 2.4.2?

VachaShah avatar Nov 17 '23 22:11 VachaShah

@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

dblock avatar Nov 17 '23 22:11 dblock