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] SIM 108 and SIM 118 don't cover enough cases as Ruff #193

Open
Emilianissimo opened this issue Feb 16, 2024 · 0 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@Emilianissimo
Copy link

Desired change

  • Rule(s): SIM108, SIM118
  • Adjustment: Cover casese, which are ignored for now

Explanation

For SIM118 package cannot handle any NotIn type of expressions, only In, which is not covering one of cases. For SIM108 package ignores the case when line of new code is more than 79 characters, which should adjusted as warning of line length and shoudn't be ignored. Ruff, for example can determine them.

Example

This is an example where the mentioned rule(s) would currently be suboptimal:

# SIM118
if key in dict.keys():  # returns the rule error
    ...
if key not in dict.keys():  # ignores
    ...

# SIM108
if a:
    b = c
else:
   b = d
# New code will be less than 79 and error will not be ignored

if long_varialbe_name:
    long_one_second = something_if
else:
    long_one_second = something_else
# New code wil exceed 79 length and error will be igonred

Soltions

# Solution for ast_compare.py:get_sim118
...
RULE = "SIM118 Use '{el} in {dict}' instead of '{el} in {dict}.keys()'"
errors: List[Tuple[int, int, str]] = []
if not (
    len(node.ops) == 1
    and (
            isinstance(node.ops[0], ast.In) or
            isinstance(node.ops[0], ast.NotIn)  # here is the new case
    )
    and len(node.comparators) == 1
):
     return errors
...

# Solution for ast_if.py:get_sim108
...
new_code = RULE.format(assign=assign, body=body, cond=cond, orelse=orelse)
    if len(new_code) > 79:
        new_code += '. This line will exceed 79 characters, please, be sure, that you will control it.'  # now all cases will not be ignored and just add new warning to it
    errors.append((node.lineno, node.col_offset, new_code))
...
@Emilianissimo Emilianissimo added the enhancement New feature or request label Feb 16, 2024
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

2 participants