Type hints across main flask-wtf codebase
Type hints across the main flask-wtf codebase to enhance type safety and maintainability. This is the part 1, if acceptable, I will add the rest that includes type hints in recaptcha codebase.
Checklist:
- [x] Add Type Hints to main codebase.
- [x] Add Type hints to recaptcha codebase
- [x] Add Type hints to
tests - [x] Apply modern
X | Ysyntax - [ ] Update
pyproject.tomladding mypy and pyright cfgs - [x] Add typing requirements
- [ ] Edit tox env
- [ ] Update CI env
- Use
from __future__ import annotationsat the top of every file that has annotations - Use
import typing as tandimport collections.abc as cabcrather than importing many names - Use
|syntax such asint | Noneinstead ofOptional[int] - Add types to
testsas well assrc - Add the following mypy and pyright config to
pyproject.toml. Add it after thetool.coveragesection:[tool.mypy] python_version = "3.9" files = ["src", "tests"] show_error_codes = true pretty = true strict = true [tool.pyright] pythonVersion = "3.9" include = ["src", "tests"] typeCheckingMode = "standard" - You also need to add the
typingrequirements lock, tox env, and CI env, see Flask-SQLAlchemy-Lite for reference: https://github.com/pallets-eco/flask-sqlalchemy-lite/tree/0b182c5e3488d0971a8798184b8be650c2d09330
You need to set up pre-commit, or pull the fixes that pre-commit adds, instead of force pushing. pre-commit install --install-hooks
After adding mypy and pyright, I'm seeing more type errors, primarily due to type compatibility issues with the main WTForms library. Should I try to resolve these type conflicts, or would it be better to adjust the type checking settings? I'm open to any suggestions on how to handle this.
@davidism Any ideas you can suggest?
Don't adjust the type checking settings. MyPy strict must pass. Pyright standard should pass, but can be lowered to basic if absolutely necessary. Pyright --verifytypes must pass. You can add specific ignore comments in if it's otherwise impossible to satisfy MyPy. If WTForms annotations need fixes, submit a fix there as well. But I can't say more without specific examples.