You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
% `ruff rule ANN202`
# missing-return-type-private-function (ANN202)
Derived from the **flake8-annotations** linter.
Fix is sometimes available.
## What it does
Checks that private functions and methods have return type annotations.
## Why is this bad?
Type annotations are a good way to document the return types of functions. They also
help catch bugs, when used alongside a type checker, by ensuring that the types of
any returned values, and the types expected by callers, match expectation.
## Example
```python
def _add(a, b):
return a + b
Use instead:
def_add(a: int, b: int) ->int:
returna+b
Availability
Because this rule relies on the third-party typing_extensions module for some Python versions,
its diagnostic will not be emitted, and no fix will be offered, if typing_extensions imports
have been disabled by the [lint.typing-extensions] linter option.
Reviewed ANN202. It's the same fix engine as ANN201, just scoped to private (_name) functions, so the blast radius is small — 86 findings, 20 with fixes.
One correction to my ANN201 note above that applies here too: with a current ruff (I tested 0.15.21) the unsafe autofix is not "only ever -> None". It infers the return type from statically-provable returns:
So the 20 fixes are safe-by-construction: each is either -> None (proven no value returned) or the concrete type of a literal ruff could see. They're flagged "unsafe" only because ruff treats any annotation-adding fix as unsafe (annotations can, in rare cases, be read at runtime — dataclasses/Pydantic/get_type_hints/singledispatch), not because the type is likely wrong.
Suggested play: run ruff check --select=ANN202 --unsafe-fixes --fix, skim the 20-line diff (trivial to eyeball at this size), land it. The remaining 66 need a human to name the type → good --select=ANN202 Hacktoberfest slices per directory.
(Disclosure: I'm Priya Sundaram, an AI software agent; a human reviews my substantive work.)
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
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.
https://docs.astral.sh/ruff/rules/missing-return-type-private-function/
@dhruvmanila @priya-sundaram-dev, please review and help us understand how to identify which changes are unsafe.
%
git checkout -b ruff-rule-ANN202-unsafe-fixes%
ruff check --select=ANN202 --statistics%
ruff check --select=ANN202 --fix --unsafe-fixes --silentWarning
20 fixes modify 113 files?!?
%
ruff check --select=ANN202 --statistics%
git commit -am"ruff rule ANN202 missing-return-type-private-function" && git pushUse instead:
Availability
Because this rule relies on the third-party
typing_extensionsmodule for some Python versions,its diagnostic will not be emitted, and no fix will be offered, if
typing_extensionsimportshave been disabled by the [
lint.typing-extensions] linter option.Options
lint.typing-extensions