Skip to content

ruff rule ANN202 missing-return-type-private-function - #15298

Merged
cclauss merged 2 commits into
TheAlgorithms:masterfrom
cclauss:ruff-rule-ANN202-unsafe-fixes
Sep 12, 2026
Merged

ruff rule ANN202 missing-return-type-private-function#15298
cclauss merged 2 commits into
TheAlgorithms:masterfrom
cclauss:ruff-rule-ANN202-unsafe-fixes

Conversation

@cclauss

@cclauss cclauss commented Sep 12, 2026

Copy link
Copy Markdown
Member

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

86	ANN202	missing-return-type-private-function
Found 86 errors.
No fixes available (20 hidden fixes can be enabled with the `--unsafe-fixes` option).

% ruff check --select=ANN202 --fix --unsafe-fixes --silent

Warning

20 fixes modify 113 files?!?

% ruff check --select=ANN202 --statistics

66	ANN202	missing-return-type-private-function
Found 66 errors.

% git commit -am"ruff rule ANN202 missing-return-type-private-function" && git push

% `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:
    return a + 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.

Options

  • lint.typing-extensions

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Sep 12, 2026
@cclauss cclauss added the require type hints https://docs.python.org/3/library/typing.html label Sep 12, 2026
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 12, 2026
@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

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:

def _noreturn(self):    ->  def _noreturn(self) -> None:      # no value-returning path
def _returns(self):     ->  def _returns(self) -> int:        # return 5   (literal)
    return 5
def _stub(self):        ->  (left alone)                      # raise NotImplementedError
    raise NotImplementedError
def _returns(self, x):  ->  (left alone)                      # return x   (type unknown)
    return x

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.)

@cclauss
cclauss merged commit 0525ef5 into TheAlgorithms:master Sep 12, 2026
8 checks passed
@cclauss
cclauss deleted the ruff-rule-ANN202-unsafe-fixes branch September 12, 2026 19:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This PR modified some existing files require type hints https://docs.python.org/3/library/typing.html

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants