Skip to content

chore: add type hints for bugbear - #611

Merged
cooperlees merged 1 commit into
PyCQA:mainfrom
phalberg:chore/mypy_on_bugbear
Oct 31, 2026
Merged

cooperlees merged 1 commit into
PyCQA:mainfrom
phalberg:chore/mypy_on_bugbear

Conversation

@phalberg

@phalberg phalberg commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adding some type hints for bugbear.py!

Issue

Part of #478.

Desc

While running mypy on bugbear.py (not strict, we will get to that one hopefully 🚀) I got the following errors:

bugbear.py:112: error: Argument 1 to "extend" of "list" has incompatible type "Iterator[AST]"; expected "Iterable[stmt]"  [arg-type]
bugbear.py:179: error: Argument 1 to "_only_comment_after" of "BugBearChecker" has incompatible type "Expr"; expected "expr"  [arg-type]
bugbear.py:1825: error: Argument 1 to "append" of "list" has incompatible type "FunctionDef"; expected "Lambda"  [arg-type]
bugbear.py:2780: error: "expr" has no attribute "id"  [attr-defined]
bugbear.py:3176: error: Need type annotation for "outer_names" (hint: "outer_names: set[<type>] = ...")  [var-annotated]
bugbear.py:3220: error: Incompatible types in assignment (expression has type "expr | None", variable has type "expr")  [assignment]
Found 6 errors in 1 file (checked 1 source file)

It seems happy after this change:

Success: no issues found in 1 source file

Note: Please do double check if all of these types make sense though, I must admit that i have not checked thoroughly neither am i completely used to the codebase yet...

Comment thread bugbear.py Outdated

underscore_indexes = [
index for index, target in enumerate(targets) if target.id == "_"
index for index, target in enumerate(targets) if isinstance(target, ast.Name) and target.id == "_"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems somewhat useless to check, since we have L2776-L2777 but I am not sure if there are simpler ways to satisfy mypy -- open for suggestions :)

@phalberg
phalberg force-pushed the chore/mypy_on_bugbear branch from c242921 to f2e6a06 Compare October 29, 2026 19:52
@phalberg
phalberg marked this pull request as ready for review October 29, 2026 19:54
@cooperlees
cooperlees requested a balanced review from Copilot October 29, 2026 20:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@phalberg
phalberg requested a balanced review from Copilot October 30, 2026 07:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@cooperlees cooperlees left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review on behalf of @cooperlees — I checked out the PR branch, verified each hunk, ran mypy and the test suite.

Verdict: Approve — all 6 changes are behavior-preserving typing fixes:

  1. stack: list[ast.AST] + extend() — same as list(body), fixes Iterator[AST] vs Iterable[stmt] error. Good.
  2. _only_comment_after(node.value) instead of (node) — matches the ast.expr signature. Expr and its Constant value share the same span so observable behavior is identical. Correct fix.
  3. safe_functions: list[AsyncFunctionDef | Lambda | FunctionDef] — pure annotation matching FUNCTION_NODES. Good.
  4. B913 isinstance(target, ast.Name) guard — redundant given the early-return above it, but harmless and satisfies expr has no attribute id. No behavior change. Good.
  5. outer_names: set[str] — pure annotation. Good.
  6. B046 kw_value is None: return — previously value=None would just fall through the isinstance checks with no error; early return is equivalent and narrows expr | None to expr. Correct and clearer.

Verification: mypy bugbear.py is clean (only unrelated missing flake8.exceptions stubs), pytest tests/ -q passes (142 passed, 2 skipped).

Note: This review was written by AI on behalf of cooperlees.

@cooperlees
cooperlees merged commit a7048ce into PyCQA:main Oct 31, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants