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

Money class instantiation returns Any as type #674

Open
alex-way opened this issue Jun 9, 2022 · 5 comments
Open

Money class instantiation returns Any as type #674

alex-way opened this issue Jun 9, 2022 · 5 comments

Comments

@alex-way
Copy link

alex-way commented Jun 9, 2022

When instantianting the djmoney.money.Money class it hints the type as Any when it should be Money. This prevents my code editor from presenting autocomplete suggestions as the type can't be inferred.

from djmoney.money import Money, DefaultMoney

i_am_any = Money(0) # is `Any`
i_am_money = DefaultMoney(0) # is `Money` class

image
image

@alex-way
Copy link
Author

Digging into this further the issue is caused by the @deconstructable class decorator on the Money class in djmoney/money.py. This might point the issue more towards django, rather than this library. Will investigate further and report back, hopefully with a fix.

@antonagestam
Copy link
Contributor

@alex-way Note that django-money does not yet expose type hints. py-moneyed received support for type hints in v2.0 and support for that was only recently merged: #638

Because that is now merged, there should be no blockers to adding type hints and exposing them with the py.typed marker.

It sounds like an interesting find with the deconstructable decorator. Does django-stubs have proper typing for it? E.g. should be something like (C) -> C.

@antonagestam
Copy link
Contributor

Btw, unlocking the possibility to add type hints was the entire point of me pushing for the py-moneyed 2.0 upgrade here ;)

@alex-way
Copy link
Author

As a workaround I've done something which I don't advise as it can cause issues with type hinting for other objects. However, it seems to work for my use case.

I created a file in the root of my repo typings/django/utils/deconstruct.pyi with the following file contents:

from typing import Any, Optional, Type
from djmoney.money import Money


# override for typehint
def deconstructible(*args: Any, path: Optional[Any] = ...) -> Type[Money]: ...

Again I'm not advocating for this solution, but just putting it out there for others who need a quick fix.

@karolyi
Copy link
Contributor

karolyi commented Aug 20, 2022

I just opened a merge request for this exact problem: typeddjango/django-stubs#1116

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants