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

How to correctly override Field.to_representation #290

Open
XF-FW opened this issue Nov 15, 2022 · 0 comments
Open

How to correctly override Field.to_representation #290

XF-FW opened this issue Nov 15, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@XF-FW
Copy link
Contributor

XF-FW commented Nov 15, 2022

Question

I have a class that looks something like this:

class DecimalSerializerMethodField(s.DecimalField, s.SerializerMethodField):
    def to_representation(self, value: Any) -> Any:
        method_result = s.SerializerMethodField.to_representation(self, value)
        decimal_result = s.DecimalField.to_representation(self, method_result)

        return decimal_result

And when running mypy, I'm getting the following errors:

error: Argument 1 to "to_representation" of "Field" has incompatible type "DecimalSerializerMethodField"; expected "Field[_VT, _DT, _RP, _IN]"
error: Argument 1 to "to_representation" of "Field" has incompatible type "DecimalSerializerMethodField"; expected "Field[_VT, _DT, _RP, _IN]"
error: Argument 2 to "to_representation" of "Field" has incompatible type "_RP"; expected "_VT"

Now, I understand, that this is because I'm calling other_class.method(self, ...), but I'm not sure on how to workaround it.

My question, is how to correctly implement this class, while keeping typing intact?

Thanks

@XF-FW XF-FW added the bug Something isn't working label Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

1 participant