flask-wtf icon indicating copy to clipboard operation
flask-wtf copied to clipboard

Type hints across main flask-wtf codebase

Open princerb opened this issue 6 months ago • 5 comments

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 | Y syntax
  • [ ] Update pyproject.toml adding mypy and pyright cfgs
  • [x] Add typing requirements
  • [ ] Edit tox env
  • [ ] Update CI env

princerb avatar Jun 09 '25 11:06 princerb

  • Use from __future__ import annotations at the top of every file that has annotations
  • Use import typing as t and import collections.abc as cabc rather than importing many names
  • Use | syntax such as int | None instead of Optional[int]
  • Add types to tests as well as src
  • Add the following mypy and pyright config to pyproject.toml. Add it after the tool.coverage section:
    [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 typing requirements lock, tox env, and CI env, see Flask-SQLAlchemy-Lite for reference: https://github.com/pallets-eco/flask-sqlalchemy-lite/tree/0b182c5e3488d0971a8798184b8be650c2d09330

davidism avatar Jun 09 '25 12:06 davidism

You need to set up pre-commit, or pull the fixes that pre-commit adds, instead of force pushing. pre-commit install --install-hooks

davidism avatar Jun 10 '25 20:06 davidism

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.

princerb avatar Jun 13 '25 12:06 princerb

@davidism Any ideas you can suggest?

princerb avatar Jun 18 '25 12:06 princerb

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.

davidism avatar Jun 18 '25 18:06 davidism