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

SIM108 not raised for instance variables #16542

Open
cspoelstra opened this issue Mar 6, 2025 · 2 comments
Open

SIM108 not raised for instance variables #16542

cspoelstra opened this issue Mar 6, 2025 · 2 comments
Labels
rule Implementing or modifying a lint rule

Comments

@cspoelstra
Copy link

Summary

SIM108 is not being raised for instance variables.

class A:
    def func(self):
        if cond:
            self.x = a
        else:
            self.x = b

Expected behavior:
SIM108 raised to suggest

self.x = a if cond else b

Actual behavior:
SIM108 not raised.

Removing self. causes the rule to be raised.

@ntBre ntBre added rule Implementing or modifying a lint rule help wanted Contributions especially welcome labels Mar 6, 2025
@ntBre
Copy link
Contributor

ntBre commented Mar 6, 2025

Thanks for the report! It looks like the rule is currently restricted to simple variable name expressions like the upstream rule, but I think it makes sense to expand it to attribute accesses (as in this example) and probably subscripts too. I think that should cover valid left-hand sides of assignments.

let Expr::Name(ast::ExprName { id: body_id, .. }) = body_target else {
return;
};
let Expr::Name(ast::ExprName { id: else_id, .. }) = else_target else {
return;
};

@MichaReiser MichaReiser removed the help wanted Contributions especially welcome label Mar 7, 2025
@MichaReiser
Copy link
Member

The challenge with allowing more expressions is that we then get pushback that the resulting expression is now too complicated and isn't a simplification. That's why, for now, I'd prefer to keep the rule aligned with the upstream rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

3 participants