Deprecate `Choices`?
mthuurne opened this issue · 1 comments
Problem
I'm adding type annotations to model_utils.choices.Choices
and mypy flags some errors which look like actual bugs in the code. It seems the handling of optgroups isn't implemented correctly; I'll put some examples below.
The documentation of Choices
contains this note:
Django 3.0 adds enumeration types. These provide most of the same features as Choices.
Maybe Choices
can be deprecated instead of fixing its implementation? I feel that would be a more efficient way of spending effort.
Environment
- Django Model Utils version: current
master
branch - Django version: 4.1.7
- Python version: 3.10.6
- Other libraries used, if any:
django-stubs
1.16.0
Code examples
Here the item added to triple_collector
is not actually a triple:
# option group
group_name = choice[0]
subchoices = choice[1]
tc = []
triple_collector.append((group_name, tc))
dc = []
double_collector.append((group_name, dc))
self._process(subchoices, tc, dc)
Option groups in self._doubles
are doubles (pairs) if they were passed to the Choices
constructor as doubles, but triples if they were passed as triples. This is a result of how the _process()
method handles them: triples are stored as-is whether they contain optgroups or not, while optgroups in doubles are added recursively.
Full code is here.