1733 add lambda instance assignment violation#1911
1733 add lambda instance assignment violation#1911AlexandrKhabarov wants to merge 3 commits intowemake-services:masterfrom
Conversation
|
@sobolevn |
sobolevn
left a comment
There was a problem hiding this comment.
Thanks a lot! Great work! So many awesome features!
I have several things I want to discuss:
- What do you think about
cls.attribute assignment? - What do you think about
class Some: a = lambda: ...?
| instance_lambda_assignment = """ | ||
| class Example(object): | ||
| def __init__(self): | ||
| self._attr = lambda: ... |
There was a problem hiding this comment.
We need to make this _attr thing here parametrized.
We need to check: attr, _attr, and __attr
We also need to parametrize self part.
We need to check: self, cls, mcs, other
| example.callback(1) will look like a method, but it is not a method. | ||
|
|
||
| Solution: | ||
| To forbid lambda assignment in instance attributes. |
There was a problem hiding this comment.
Do not assign
lambdafunctions to attributes, create real methods.
|
|
||
| Reasoning: | ||
| This will be very tricky to understand for users. | ||
| example.callback(1) will look like a method, but it is not a method. |
There was a problem hiding this comment.
example.callback(1)
but it is not really a method
|
Closes #1733 |
|
@AlexandrKhabarov please, don't forget to use |
11e1d7f to
f9c36c5
Compare
f9c36c5 to
9af8176
Compare
|
Yeah, I think, it is a good point to check assignments to class instance too. I'll add it. |
9bbf386 to
2de3cdf
Compare
2de3cdf to
816cb36
Compare
Codecov Report
@@ Coverage Diff @@
## master #1911 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 115 119 +4
Lines 6145 6258 +113
Branches 1375 1393 +18
==========================================
+ Hits 6145 6258 +113
Continue to review full report at Codecov.
|
|
@sobolevn |
sobolevn
left a comment
There was a problem hiding this comment.
Thanks a lot, @AlexandrKhabarov!
As always, great work! 👍
| return flatten_nodes | ||
|
|
||
|
|
||
| def get_assignments(node: ast.ClassDef) -> _AllAssignments: |
There was a problem hiding this comment.
This function is really similar to https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/logic/tree/classes.py#L76
Can you please explain, why can't we reuse it?
There was a problem hiding this comment.
I wrote another one function for finding assignments only.
This one finds assignments for classes and attributes for instances and I decided to unify returned values for lambda assignment violations.
I agree that both functions are similar and I need to think about how to decreases code duplication.
| AttributesAssignmentVisitor, | ||
| ) | ||
|
|
||
| instance_lambda_assignment = """ |
There was a problem hiding this comment.
We also need cls template:
"""
class Example(object):
@classmethod
def some(cls):
{0}
"""f534c8c to
924525d
Compare
db6c466 to
14b498c
Compare
14b498c to
8f6c965
Compare
|
@sobolevn |
sobolevn
left a comment
There was a problem hiding this comment.
Thanks a lot! The progress is looking great! 👍
Please, rebase your PR on master, since now it has some conflicts.
| isinstance(target, ast.Attribute) and | ||
| isinstance(target.ctx, ast.Store) and | ||
| isinstance(target.value, ast.Name) and | ||
| target.value.id in constants.SPECIAL_ARGUMENT_NAMES_WHITELIST |
There was a problem hiding this comment.
This is not correct, because constants.SPECIAL_ARGUMENT_NAMES_WHITELIST also has mcs and cls, so it can catch non-self assigns.
|
|
||
| _scope_predicates: Tuple[_ScopePredicate, ...] = ( | ||
| lambda node, names: predicates.is_property_setter(node), | ||
| lambda node, names: predicates.is_property_setter(node), # noqa: WPS467 |
There was a problem hiding this comment.
This should be ignored, because here we assign Tuple and not lambda.
We should test it explicitly.
I have made things!
Checklist
CHANGELOG.md