Static typing of Field subclass kwargs
Today, the subclasses of Field have their constructor kwargs untyped, which means that static typecheckers like mypy/pyright treat them as Unknown. This can lead to bugs or typos like passing fields.Integer(validates=...) instead of fields.Integer(validate=...). In addition, editors like VSCode or neovim can't autocomplete or show you the types of these kwargs:
In https://github.com/microsoft/pyright/issues/6344#issuecomment-2226519311, it was recommended to use the TypedDict and Unpack features to statically type kwargs. For example:
class BaseFieldKwargs(TypedDict, total=False):
load_default: typing.Any
missing: typing.Any
dump_default: typing.Any
default: typing.Any
data_key: str | None
attribute: str | None
validate: (
None
| typing.Callable[[typing.Any], typing.Any]
| typing.Iterable[typing.Callable[[typing.Any], typing.Any]]
)
...
class Integer(Number):
...
def __init__(self, *, strict: bool = False, **kwargs: Unpack[BaseFieldKwargs]):
...
(The presence of a catch-all additional_metadata makes it a little tricky. We might need to union it with a generic Dict[str, typing.Any] until that parameter is deprecated.)
Thoughts on adding this to improve static typechecking?