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

Infer type for Form.fields #1514

Open
andraantariksa opened this issue May 27, 2023 · 3 comments
Open

Infer type for Form.fields #1514

andraantariksa opened this issue May 27, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@andraantariksa
Copy link

andraantariksa commented May 27, 2023

https://stackoverflow.com/questions/73600700/best-practice-for-python-typing-out-of-dictionary-self-fields-type-in-django-mo

I'm looking to infer dictionary values type for this snippet

class MerchantBrandsForm(forms.Form):
    brands = forms.ModelChoiceField(required=True, queryset=None)

    def __init__(self: merchant: Merchant, *args: Any, **kwargs: Any):
        super().__init__(*args, **kwargs)
        self.fields['brands'].queryset = merchant.brands.all()
           # ^^^^^^^^^^
           # This would be a type error since forms.ModelChoiceField is generalized to forms.Field

self.fields type above should be inferred to

class Fields(TypedDict):
    brands: forms.ModelChoiceField
@andraantariksa andraantariksa added the bug Something isn't working label May 27, 2023
@sobolevn
Copy link
Member

This needs a plugin similar to one we have for models. If you want to work on this, I can provide all the help you need :)

@andraantariksa
Copy link
Author

This needs a plugin similar to one we have for models. If you want to work on this, I can provide all the help you need :)

Sure, I can work on this. Please give me some pointers

@asottile-sentry
Copy link

I'm going to close my duplicate of this since it's probably better tacked here: #1208

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

3 participants