|
| 1 | +from collections.abc import Callable, Iterable, Iterator, Sequence |
| 2 | +from typing import Any |
| 3 | +from typing_extensions import Self, TypeAlias |
| 4 | + |
| 5 | +from wtforms.fields.core import Field, _Filter, _FormT, _Validator, _Widget |
| 6 | +from wtforms.form import BaseForm |
| 7 | +from wtforms.meta import DefaultMeta, _SupportsGettextAndNgettext |
| 8 | + |
| 9 | +# technically this allows a list, but we're more strict for type safety |
| 10 | +_Choice: TypeAlias = tuple[Any, str] |
| 11 | +_GroupedChoices: TypeAlias = dict[str, Iterable[_Choice]] |
| 12 | +_FullChoice: TypeAlias = tuple[Any, str, bool] # value, label, selected |
| 13 | +_FullGroupedChoices: TypeAlias = tuple[str, Iterable[_FullChoice]] |
| 14 | +_Option: TypeAlias = SelectFieldBase._Option |
| 15 | + |
| 16 | +class SelectFieldBase(Field): |
| 17 | + option_widget: _Widget[_Option] |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + label: str | None = None, |
| 21 | + validators: tuple[_Validator[_FormT, Self], ...] | list[Any] | None = None, |
| 22 | + option_widget: _Widget[_Option] | None = None, |
| 23 | + *, |
| 24 | + filters: Sequence[_Filter] = (), |
| 25 | + description: str = "", |
| 26 | + id: str | None = None, |
| 27 | + default: object | None = None, |
| 28 | + widget: _Widget[Self] | None = None, |
| 29 | + render_kw: dict[str, Any] | None = None, |
| 30 | + name: str | None = None, |
| 31 | + _form: BaseForm | None = None, |
| 32 | + _prefix: str = "", |
| 33 | + _translations: _SupportsGettextAndNgettext | None = None, |
| 34 | + _meta: DefaultMeta | None = None, |
| 35 | + ) -> None: ... |
| 36 | + def iter_choices(self) -> Iterator[_FullChoice]: ... |
| 37 | + def has_groups(self) -> bool: ... |
| 38 | + def iter_groups(self) -> Iterator[_FullGroupedChoices]: ... |
| 39 | + def __iter__(self) -> Iterator[_Option]: ... |
| 40 | + |
| 41 | + class _Option(Field): |
| 42 | + checked: bool |
| 43 | + |
| 44 | +class SelectField(SelectFieldBase): |
| 45 | + coerce: Callable[[Any], Any] |
| 46 | + choices: list[_Choice] | _GroupedChoices |
| 47 | + validate_choice: bool |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + label: str | None = None, |
| 51 | + validators: tuple[_Validator[_FormT, Self], ...] | list[Any] | None = None, |
| 52 | + coerce: Callable[[Any], Any] = ..., |
| 53 | + choices: Iterable[_Choice] | _GroupedChoices | None = None, |
| 54 | + validate_choice: bool = True, |
| 55 | + *, |
| 56 | + filters: Sequence[_Filter] = (), |
| 57 | + description: str = "", |
| 58 | + id: str | None = None, |
| 59 | + default: object | None = None, |
| 60 | + widget: _Widget[Self] | None = None, |
| 61 | + option_widget: _Widget[_Option] | None = None, |
| 62 | + render_kw: dict[str, Any] | None = None, |
| 63 | + name: str | None = None, |
| 64 | + _form: BaseForm | None = None, |
| 65 | + _prefix: str = "", |
| 66 | + _translations: _SupportsGettextAndNgettext | None = None, |
| 67 | + _meta: DefaultMeta | None = None, |
| 68 | + ) -> None: ... |
| 69 | + def iter_choices(self) -> Iterator[_FullChoice]: ... |
| 70 | + def has_groups(self) -> bool: ... |
| 71 | + def iter_groups(self) -> Iterator[_FullGroupedChoices]: ... |
| 72 | + |
| 73 | +class SelectMultipleField(SelectField): |
| 74 | + data: list[Any] | None |
| 75 | + |
| 76 | +class RadioField(SelectField): ... |
0 commit comments