MartinThoma / flake8-simplify

❄ A flake8 plugin that helps you to simplify code
MIT License
186 stars 19 forks source link

[Adjust Rule] SIM902 alert is wrong for getattr and other positional only functions #125

Closed bellini666 closed 2 years ago

bellini666 commented 2 years ago

Desired change

Explanation

getattr can't be used with keyword arguments and will throw TypeError: getattr() takes no keyword arguments if tried to be used as such.

The same applies to functions defined as as positional only (by using / in the arguments), which might make it impossible to use keyword arguments. This case is not easy to workaround though, but getattr and other official apis alike should probably be excluded from the rule.

Example

image

DmytroLitvinov commented 2 years ago

Get the same error with self.multiple = kwargs.pop('multiple', True) line.

MartinThoma commented 2 years ago

Thank you, I'll fix it immediately for those examples. However, positional-only arguments might be a reason to remove this rule completely :smiling_face_with_tear:

MartinThoma commented 2 years ago

Full minimal example:

from dataclasses import dataclass

@dataclass
class Point:
    x: int
    y: int

p = Point(1, 2)
print(getattr(p, "x", default="foo"))
MartinThoma commented 2 years ago

With the current way I receive the AST there is no practical way to detect positional-only arguments. I moved the rule to https://github.com/MartinThoma/flake8-scream as that project is allowed to have false-positives.