pwoolvett / flake8_boolean_trap

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

Optionally allow booleans in single-arg functions #7

Open dosisod opened 1 year ago

dosisod commented 1 year ago

IMO, the following should not emit an error:

class BooleanExpression:
    def __init__(self, value: bool) -> None:
        self.value = value

BooleanExpression(True)

Assuming that the function/class is properly named, using value=True in this case shouldn't be necessary. That is just my opinion though, so having the option to turn it off in this particular case would be nice.

pwoolvett commented 1 year ago

what would "properly named" even mean?

FBT would raise once for the __init__, and once for the instantiation.

Are you talking about both?

dosisod commented 1 year ago

I agree that "properly named" is a bit vague, which is why it is subjective. And yes I'm suggesting that both instances should not emit an error.

With that in mind, if you truly don't want your users to call your function with keyword arguments, you should probably be using a positional-only argument anyways:

class BooleanExpression:
    def __init__(self, value: bool, /) -> None:  # notice the `/`
        self.value = value

BooleanExpression(True)

In this case, the intent that positional args are indented is explict, and an error shouldn't be emitted (in both cases). In fact, changing True to value=True will result in an exception:

Traceback (most recent call last):
  File "file.py", line 5, in <module>
    BooleanExpression(value=True)
TypeError: BooleanExpression.__init__() got some positional-only arguments passed as keyword arguments: 'value'

So I guess forget what I said about single arg functions, I would much rather use positional-only arguments in this instance. FBT shouldn't be telling to use a keyword argument for a positional only arg in functions though.