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

1733 add lambda instance assignment violation #1911

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

AlexandrKhabarov
Copy link
Contributor

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the CHANGELOG.md

@AlexandrKhabarov
Copy link
Contributor Author

@sobolevn
Hello, Nikita!
Can you look at my request?

Copy link
Member

@sobolevn sobolevn left a 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:

  1. What do you think about cls. attribute assignment?
  2. What do you think about class Some: a = lambda: ...?

instance_lambda_assignment = """
class Example(object):
def __init__(self):
self._attr = lambda: ...
Copy link
Member

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

Copy link
Contributor Author

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.
Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@sobolevn
Copy link
Member

Closes #1733

@sobolevn sobolevn linked an issue Feb 25, 2021 that may be closed by this pull request
@sobolevn
Copy link
Member

@AlexandrKhabarov please, don't forget to use closes #x in your PRs, so our automation will link them to original issues 🙂

@AlexandrKhabarov
Copy link
Contributor Author

AlexandrKhabarov commented Feb 26, 2021

Yeah, I think, it is a good point to check assignments to class instance too. I'll add it.

@AlexandrKhabarov AlexandrKhabarov force-pushed the issue-1733 branch 13 times, most recently from 9bbf386 to 2de3cdf Compare March 2, 2021 14:24
@codecov
Copy link

codecov bot commented Mar 2, 2021

Codecov Report

Merging #1911 (1e0f252) into master (8f72647) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
...ake_python_styleguide/logic/complexity/overuses.py 100.00% <ø> (ø)
wemake_python_styleguide/presets/types/tree.py 100.00% <ø> (ø)
wemake_python_styleguide/visitors/ast/blocks.py 100.00% <ø> (ø)
wemake_python_styleguide/logic/tree/annotations.py 100.00% <100.00%> (ø)
wemake_python_styleguide/logic/tree/assignments.py 100.00% <100.00%> (ø)
wemake_python_styleguide/logic/tree/decorators.py 100.00% <100.00%> (ø)
wemake_python_styleguide/logic/tree/functions.py 100.00% <100.00%> (ø)
...ake_python_styleguide/violations/best_practices.py 100.00% <100.00%> (ø)
wemake_python_styleguide/visitors/ast/classes.py 100.00% <100.00%> (ø)
wemake_python_styleguide/visitors/ast/compares.py 100.00% <100.00%> (ø)
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8f72647...8f6c965. Read the comment docs.

@AlexandrKhabarov
Copy link
Contributor Author

@sobolevn
Hello, Nikita?
Can you review my request?

Copy link
Member

@sobolevn sobolevn left a 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:
Copy link
Member

@sobolevn sobolevn Mar 2, 2021

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?

Copy link
Contributor Author

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 = """
Copy link
Member

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}
"""

@AlexandrKhabarov AlexandrKhabarov force-pushed the issue-1733 branch 2 times, most recently from db6c466 to 14b498c Compare March 8, 2021 14:45
@AlexandrKhabarov
Copy link
Contributor Author

@sobolevn
Hello Nikita!
I decreased code duplication and write one more tests for class lamda assignment.
Can you review my request?

Copy link
Member

@sobolevn sobolevn left a 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
Copy link
Member

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

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.

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

Successfully merging this pull request may close these issues.

Forbid assigning lambda to object properties
2 participants