Skip to content

WPS212 counts return-statements from inner definitions #3413

@phagenlocher

Description

@phagenlocher

What's wrong

def foobar() -> Any:
    def inner_func(x: Any) -> Any:
        return x

    class Foobar:
        def __init__(self: Self):
            self.x = 1

        def get_x(self: Self) -> int:
            return self.x

    return inner_func(Foobar())

In the above definition WPS212 for foobar is being triggered because 3 return statements are being counted. However, the function only has a single return. The statements of the inner function and class method definitions are being counted as well.

How it should be

WPS212 should ignore inner definitions of functions and classes when counting return statements. Selectively noqaing inner definitions with their own return statements is currently impossible if WPS212 has to be enforced.

Flake8 version and plugins

{
  "platform": {
    "python_implementation": "CPython",
    "python_version": "3.12.10",
    "system": "Linux"
  },
  "plugins": [
    {
      "plugin": "darglint",
      "version": "1.8.1"
    },
    {
      "plugin": "flake8-annotations",
      "version": "3.1.1"
    },
    {
      "plugin": "flake8-bugbear",
      "version": "24.12.12"
    },
    {
      "plugin": "flake8-builtins",
      "version": "2.5.0"
    },
    {
      "plugin": "flake8-class-attributes-order",
      "version": "0.3.0"
    },
    {
      "plugin": "flake8-comprehensions",
      "version": "3.16.0"
    },
    {
      "plugin": "flake8-docstrings",
      "version": "1.7.0"
    },
    {
      "plugin": "flake8-eradicate",
      "version": "1.5.0"
    },
    {
      "plugin": "flake8-executable",
      "version": "2.1.3"
    },
    {
      "plugin": "flake8-functions",
      "version": "0.0.8"
    },
    {
      "plugin": "flake8_cognitive_complexity",
      "version": "0.1.0"
    },
    {
      "plugin": "mccabe",
      "version": "0.7.0"
    },
    {
      "plugin": "pep8-naming",
      "version": "0.14.1"
    },
    {
      "plugin": "pycodestyle",
      "version": "2.13.0"
    },
    {
      "plugin": "pyflakes",
      "version": "3.3.2"
    },
    {
      "plugin": "wemake-python-styleguide",
      "version": "1.1.0"
    }
  ],
  "version": "7.2.0"
}

pip information

pip 25.1.1 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)
amqp==5.3.1
annotated-types==0.7.0
anyio==4.9.0
attrs==25.3.0
billiard==4.2.1
black==25.1.0
blinker==1.9.0
build==1.2.2.post1
celery==5.5.2
certifi==2025.1.31
chardet==5.2.0
charset-normalizer==3.4.1
Chunking==0.0.2
click==8.1.8
click-didyoumean==0.3.1
click-plugins==1.1.1
click-repl==0.3.0
cognitive_complexity==1.3.0
coloredlogs==15.0.1
darglint==1.8.1
eradicate==2.3.0
fastembed==0.5.1
filelock==3.18.0
flake8==7.2.0
flake8-annotations==3.1.1
flake8-bugbear==24.12.12
flake8-builtins==2.5.0
flake8-class-attributes-order==0.3.0
flake8-comprehensions==3.16.0
flake8-docstrings==1.7.0
flake8-eradicate==1.5.0
flake8-executable==2.1.3
flake8-functions==0.0.8
flake8_cognitive_complexity==0.1.0
Flask==3.1.0
flask-redis==0.4.0
flatbuffers==25.2.10
fsspec==2025.3.0
grpcio==1.71.0
grpcio-tools==1.71.0
h11==0.14.0
h2==4.2.0
hpack==4.1.0
httpcore==1.0.7
httpx==0.28.1
huggingface-hub==0.29.3
humanfriendly==10.0
hyperframe==6.1.0
idna==3.10
itsdangerous==2.2.0
Jinja2==3.1.6
kaleido==0.2.1
kombu==5.5.3
loguru==0.7.3
MarkupSafe==3.0.2
mccabe==0.7.0
mmh3==4.1.0
mpmath==1.3.0
mr-proper==0.0.7
mypy-extensions==1.0.0
narwhals==1.32.0
nodeenv==1.9.1
numpy==2.2.4
onnxruntime==1.21.0
packaging==24.2
pandas==2.2.3
pathlib==1.0.1
pathspec==0.12.1
pep8-naming==0.14.1
pillow==10.4.0
pip-tools==7.4.1
platformdirs==4.3.7
plotly==6.0.1
portalocker==2.10.1
prompt_toolkit==3.0.51
protobuf==5.29.4
py_rust_stemmers==0.1.5
pycodestyle==2.13.0
pydantic==2.10.6
pydantic_core==2.27.2
pydocstyle==6.3.0
pyflakes==3.3.2
Pygments==2.19.1
PyJWT==2.9.0
pyproject_hooks==1.2.0
pyright==1.1.398
python-dateutil==2.9.0.post0
pytz==2025.2
PyYAML==6.0.2
qdrant-client==1.13.3
redis==5.3.0
requests==2.32.3
requests-toolbelt==1.0.0
setuptools==80.4.0
six==1.17.0
sniffio==1.3.1
snowballstemmer==2.2.0
stdlib-list==0.11.1
sympy==1.13.3
tokenizers==0.21.1
tqdm==4.67.1
typing_extensions==4.13.0
tzdata==2025.2
urllib3==2.3.0
vine==5.1.0
wcwidth==0.2.13
wemake-python-styleguide==1.1.0
Werkzeug==3.1.3
wheel==0.45.1

OS information

Linux 089052fc5b28 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 GNU/Linux

Devcontainer under WSL based on python:3.12-slim docker image.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions