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

Adjust choices= type variance for model fields #1154

Merged
merged 1 commit into from
Sep 19, 2022

Conversation

flaeppe
Copy link
Member

@flaeppe flaeppe commented Sep 17, 2022

Avoid getting stuck in an invariance pit. I don't think it makes sense to mix two tuple with named group elements in the same choices sequence(?).

This also changes the outermost container type to Sequence as e.g. both tuple and list are supported.

Reason behind this is that I'm getting the following complaints from mypy:

error: Incompatible return value type (got "List[Tuple[Any, str]]", expected "List[Union[Tuple[Any, Any], Tuple[str, Iterable[Tuple[Any, Any]]]]]")  [return-value]
note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
note: Consider using "Sequence" instead, which is covariant

Docs: https://docs.djangoproject.com/en/4.1/ref/models/fields/#choices


I'm not sure if we should change _FieldChoices too? e.g.

_FieldChoices = Union[Iterable[_Choice], Iterable[_ChoiceNamedGroup]]

Avoid getting stuck in an invariance pit. I don't think it makes sense
to mix two tuple with named group elements in same choices sequence(?).

This also changes the outermost container type to `Sequence` as e.g.
both `tuple` and `list` are supported.
@sobolevn sobolevn merged commit a2a3543 into typeddjango:master Sep 19, 2022
@flaeppe flaeppe deleted the fix/field-choices-variance branch September 19, 2022 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants