Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible types in assignment for document fields #602

Open
ericbuehl opened this issue Nov 17, 2023 · 6 comments
Open

Incompatible types in assignment for document fields #602

ericbuehl opened this issue Nov 17, 2023 · 6 comments
Labels
bug Something isn't working

Comments

@ericbuehl
Copy link

ericbuehl commented Nov 17, 2023

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 ericbuehl added bug Something isn't working untriaged Need triage labels Nov 17, 2023
@harshavamsi
Copy link
Collaborator

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

@ericbuehl
Copy link
Author

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

@dblock
Copy link
Member

dblock commented Nov 17, 2023

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

@dblock
Copy link
Member

dblock commented Nov 17, 2023

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 #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?

@VachaShah
Copy link
Collaborator

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

@dblock
Copy link
Member

dblock commented Nov 17, 2023

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants