Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bellini666 opened this issue Mar 28, 2022 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@bellini666
Copy link

Desired change

  • Rule(s): SIM902
  • Adjustment: Should not be applied to getattr and other alike

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

@bellini666 bellini666 added the enhancement New feature or request label Mar 28, 2022
@DmytroLitvinov
Copy link

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

@MartinThoma
Copy link
Owner

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

@MartinThoma
Copy link
Owner

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants