Skip to content

ANN2 fixes can add return types that are shadowed #20610

@dscorbett

Description

@dscorbett

Summary

The fixes for missing-return-type-undocumented-public-function (ANN201), missing-return-type-private-function (ANN202), missing-return-type-special-method (ANN204), missing-return-type-static-method (ANN205), and missing-return-type-class-method (ANN206) add return types without checking for shadowed variables, which can cause type-checking errors. They should use get_or_import_builtin_symbol or similar. Example:

$ cat >ann2.py <<'# EOF'
from collections import UserString as str
from typing import override
def foo(): return "!"
def _foo(): return "!"
class C:
    @override
    def __str__(self): return "!"
    @staticmethod
    def foo(): return "!"
    @classmethod
    def bar(cls): return "!"
# EOF

$ ruff --isolated check --select ANN201,ANN202,ANN204,ANN205,ANN206 ann2.py --unsafe-fixes --fix
Found 5 errors (5 fixed, 0 remaining).

$ mypy ann2.py
ann2.py:3: error: Incompatible return value type (got "str", expected "UserString")  [return-value]
ann2.py:4: error: Incompatible return value type (got "str", expected "UserString")  [return-value]
ann2.py:7: error: Return type "UserString" of "__str__" incompatible with return type "str" in supertype "builtins.object"  [override]
ann2.py:7: error: Incompatible return value type (got "str", expected "UserString")  [return-value]
ann2.py:9: error: Incompatible return value type (got "str", expected "UserString")  [return-value]
ann2.py:11: error: Incompatible return value type (got "str", expected "UserString")  [return-value]
Found 6 errors in 1 file (checked 1 source file)

Version

ruff 0.13.2 (b0bdf03 2025-09-25)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions