WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@princerb
Copy link

@princerb princerb commented Jun 9, 2025

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:

  • Add Type Hints to main codebase.
  • Add Type hints to recaptcha codebase
  • Add Type hints to tests
  • Apply modern X | Y syntax
  • Update pyproject.toml adding mypy and pyright cfgs
  • Add typing requirements
  • Edit tox env
  • Update CI env

@princerb princerb force-pushed the fix/improve-type-hints-p1 branch from e0c27a1 to 449af6d Compare June 9, 2025 12:00
@davidism
Copy link
Member

davidism commented Jun 9, 2025

  • 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

@princerb princerb force-pushed the fix/improve-type-hints-p1 branch from 54eb413 to 51ae407 Compare June 10, 2025 19:48
@princerb princerb force-pushed the fix/improve-type-hints-p1 branch from 073b6c7 to 784e402 Compare June 10, 2025 19:59
@davidism
Copy link
Member

davidism commented Jun 10, 2025

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

@princerb
Copy link
Author

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
Copy link
Author

@davidism Any ideas you can suggest?

@davidism
Copy link
Member

davidism commented Jun 18, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants