ruff: FBT003: False positive with positional-only parameters

FBT003 is triggered even if only positional arguments are allowed.

Toy example below - I stumbled upon it with a Pandas decorator:

with pd.option_context("mode.use_inf_as_na", True):
    ....

Example

def foo(*args):
    pass

def foo2(a = False, *args):
    pass


foo(False) # Should not warn
# foo2(True), # Warns as expected
foo2("a", False) # Should not warn

Errors

fbt003.py:8:5: FBT003 Boolean positional value in function call
fbt003.py:10:11: FBT003 Boolean positional value in function call

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 6
  • Comments: 16 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Just wanted to mention, there are cases in pytest fixtures where this comes up too. For example:

@pytest.mark.parametrize("throw_err", (True, False))
def test_something(throw_err: bool):
    assert ...

In this case, the FBT rule says this throw_err needs to be a kwarg. Which is a bit weird as tests are never really going to be called by anyone other than pytest ?

FWIW the same issue is for all the various set* methods you can find in PySide / PyQt, for example QTableView.setSortingEnabled or QHeaderView.setSortIndicatorShown (or many others).

If you try to call them setting the flag with a keyword, you get a runtime error, like

TypeError: QHeaderView.setVisible() takes no keyword arguments

Yeah I would accept that patch. But also fine if you just want to add a handful of methods to the allowlist in there. (We should include bool, and we might as well add option_context, set_enabled, etc. There’s little harm IMO.)

Okay. Let me see if I can even get the thing to build, first 😉

I guess the rule I’d like in the list is “instance method named set_* with single positional boolean argument” and “(non-class) function named set_* with two arguments, second of which is a positional-only boolean”.

We should add some of these to the allowlist. There isn’t much we can do right now apart from allowing these calls selectively.

@charliermarsh was that “… so I’ll take a look at this soon” or was that “… so, patches welcome”? 😃 I never wrote rust before, but no time like the present…

A related case is this one:

def foo(a: bool, /):
    ...

The error message is correct in this case, but I’d argue that the author is doing this intentionally.