[python] Introduce Mypy Typechecking#47
Open
hayat01sh1da wants to merge 20 commits intomasterfrom
Open
Conversation
66e3216 to
277885f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
1. Why (Purpose)
1-1. Reliability
disallow_untyped_defs = trueensures every function signature is explicitly typed, eliminating ambiguitywarn_return_any = trueprevents unintentionally returning loosely-typed values, reducing runtime surprises1-2. Maintainability
2. What (Procedures)
2-1. Target Repositories (13 repos)
2-2. Mypy Configuration (
pyproject.toml)A
pyproject.tomlwas created (or updated if it already existed) with the following[tool.mypy]section, consistent across all 13 repositories:python_version— Targets Python 3-14warn_return_any— Warns when a function returns a value typed asAnywarn_unused_configs— Warns about unused[tool.mypy]config entriesdisallow_untyped_defs— Rejects function definitions without type annotations (the strictest baseline)2-3. Type Annotations Added to Source and Test Files
def __init__(self, scheme: str, host: str, bot_id: str, ...) -> None:)-> str,-> None,-> list[dict[str, Any]])from typing import: UsedAny,TextIO, etc. from thetypingmodule where Python built-in types were insufficient (mainly in botpress-accuracy-checkers, deep-learnings, json-data-sorters, natural-language-processing, web-scrapers)from __future__ import annotations: Used in coding-tests and deep-learnings for forward-compatible union type syntax (e.g.,int | str)setUp(),tearDown(), andtest_*()methods annotated with-> None2-4. GitHub Actions Workflow Updates
The CI pipeline was updated to include a mypy typechecking step in the existing GHA workflow files:
Dependency installation — Added
mypytopip installalongsideflake8andpytest:pip install flake8 mypy pytestTypecheck step — Inserted a new
Typecheck with mypystep between flake8 linting and unit test execution:python/subdirectory (most repos):working-directory: ./pythonwas specifiedmypy .runs from the repo rootvol1.yml,vol2.yml,vol3.yml), each updated with the mypy step3. How (Operation and Maintenance)
3-1. Automatic Enforcement via CI
3-2. Day-to-Day Development
disallow_untyped_defs = true)mypy .locally before pushing to catch issues early3-3. Updating Configuration
pyproject.tomlat the Python project rootpython_versioninpyproject.toml4. Pros & Cons
4-1. Pros
[tool.mypy]block inpyproject.tomlcovers the entire project4-2. Cons
typingmodule constructs (Any,TextIO, union types, etc.)type: ignorecomments or stub packages