Ruff 无法捕获函数的未定义参数

问题描述 投票:0回答:1

我不确定即使 ruff 的工作是捕获函数的未定义参数,但是有没有一个工具可以做到这一点?或者是否有我缺少添加的 ruff 配置,以便 ruff 可以做到这一点?

from typing import Optional


def my_func(bar: int, baz: int, foo: Optional[int] = None) -> int:
    if foo:
        return foo + bar + baz
    else:
        return bar + baz


my_func(foo=1, bar=2, baz=3)
my_func(bar=2, baz=3)
my_func(f=1, bar=2, baz=3) # this should have been caught by ruff, but isn't and I'm 

我的ruff.toml

select = [
    "F",   # https://beta.ruff.rs/docs/rules/#pyflakes-f
    "W",   # https://beta.ruff.rs/docs/rules/#warning-w
    "E",   # https://beta.ruff.rs/docs/rules/#error-e
    "I",   # https://beta.ruff.rs/docs/rules/#isort-i
    "N",   # https://beta.ruff.rs/docs/rules/#pep8-naming-n
    "ANN", # https://beta.ruff.rs/docs/rules/#flake8-annotations-ann
    "B",   # https://beta.ruff.rs/docs/rules/#flake8-bugbear-b
    "RUF", # https://beta.ruff.rs/docs/rules/#ruff-specific-rules-ruf
    "PT",  # https://beta.ruff.rs/docs/rules/#flake8-pytest-style-pt
    "D",
]
include = ["*.py"]
force-exclude = true
fixable = ["ALL"]
pydocstyle.convention = "numpy"

exclude = [".mypy_cache", ".ruff_cache", ".venv", "__pypackages__"]
ignore = [
    "ANN101", # https://beta.ruff.rs/docs/rules/#flake8-annotations-ann -- missing self type annotation
    "E501",   # Line-length is handled by black
    "N812",   # https://beta.ruff.rs/docs/rules/lowercase-imported-as-non-lowercase/
    "ANN401", # Ignore typing Any
    "D1",     # Don't complain about missing docstrings
]
python python-3.x lint ruff
1个回答
4
投票

Ruff 目前不支持此功能。引用Ruff常见问题解答

Pylint 实现了许多 Ruff 没有的规则,反之亦然。例如,Pylint 比 Ruff 进行更多类型推断(例如,Pylint 可以验证函数调用中参数的数量)。因此,Ruff 并不是 Pylint 的“纯粹”替代品(反之亦然),因为它们执行不同的规则集。

尽管存在这些差异,许多用户已成功从 Pylint 切换到 Ruff,尤其是那些将 Ruff 与 类型检查器一起使用的用户,它可以涵盖 Pylint 提供的一些功能。

© www.soinside.com 2019 - 2024. All rights reserved.