pwoolvett / flake8_boolean_trap

A flake8 plugin to detect boolean traps.
10 stars 1 forks source link

Allow boolean arguments as keyword-only arguments #8

Closed henhuy closed 1 year ago

henhuy commented 1 year ago

From my understanding, explicitly set boolean arguments should be allowed. As python supports keyword-only arguments since PEP3102 this could be allowed. Example (borrowed from https://ariya.io/2011/08/hall-of-api-shame-boolean-trap):

def repaint(*, immediate=True)
    pass

So that function must be called using keyword repaint(immediate=False). What do you think?

pwoolvett commented 1 year ago

hi @henhuy this is already supported (and suggested as a fix).

def repaint(*, immediate=True):
    pass

def repaint2(immediate=True):
    pass
$ flake8 --select FBT ./a.py 
./a.py:4:24: FBT002 do not set boolean defaults for positional args. Hint: in `def repaint2(...)`, define `immediate` as kw-only

Note the suggestion is for the second definition only, the first passes fine

Im closing this as already solved. Feel free to re-open if i didn't get your point.

henhuy commented 1 year ago

Oh, nice!

Got this error while using ruff. Maybe I didn't use latest ruff version. Thanks for your answer!

pwoolvett commented 1 year ago
$ ruff --select=FBT ./a.py
a.py:4:24: FBT002 Boolean default value in function definition
Found 1 error.

its a pity i cannot share same codebase - fbt for ruff is here

henhuy commented 1 year ago

Yes, I understand...

henhuy commented 1 year ago

So I should post issue there...

pwoolvett commented 1 year ago

So I should post issue there...

in the next issue, yes. for the current one, i already tried it and it works fine, see https://github.com/pwoolvett/flake8_boolean_trap/issues/8#issuecomment-1479516888 , so there's no need to create another issue...