-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
1733 add lambda instance assignment violation #1911
base: master
Are you sure you want to change the base?
1733 add lambda instance assignment violation #1911
Conversation
@sobolevn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not assign
lambda
functions to attributes, create real methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example.callback(1)
but it is not really a method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not correct, because constants.SPECIAL_ARGUMENT_NAMES_WHITELIST
also has mcs
and cls
, so it can catch non-self assigns.
@@ -76,7 +76,7 @@ class BlockVariableVisitor(base.BaseNodeVisitor): | |||
) | |||
|
|||
_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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be ignored, because here we assign Tuple
and not lambda
.
We should test it explicitly.
I have made things!
Checklist
CHANGELOG.md