django-stubs
django-stubs copied to clipboard
Infer type for Form.fields
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
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 :)
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
I'm going to close my duplicate of this since it's probably better tacked here: #1208