python-3.x Ruff不捕获函数的未定义参数

pwuypxnk  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(135)

我不确定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

My 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
]
9rygscc1

9rygscc11#

Ruff目前不支持此功能。引用Ruff FAQ:
Pylint实现了许多Ruff没有实现的规则,反之亦然。例如,Pylint比Ruff做更多的类型推断(例如,Pylint可以验证函数调用中的参数数量)。因此,Ruff不是Pylint的“纯粹”替代品(反之亦然),因为它们执行不同的规则集。
尽管存在这些差异,许多用户已经成功地从Pylint切换到Ruff,特别是那些使用Ruff和类型检查器的用户,这可以覆盖Pylint提供的一些功能。

相关问题