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

Fix for issue 649. #678

Closed
wants to merge 8 commits into from
Closed

Conversation

NanduTej
Copy link

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

Related issues

@NanduTej
Copy link
Author

NanduTej commented Jul 18, 2019

Hey @sobolevn! I have made the changes and I think they work as they are supposed to. Can you check and let me know? This is code for #649

@sobolevn
Copy link
Member

sobolevn commented Jul 18, 2019

Looking good!
Can you please rebase your changes? And I will merge them into 0.12

@NanduTej
Copy link
Author

@sobolevn I did the rebase.

@sobolevn
Copy link
Member

@NanduTej please, fix style 🙂

You can run make test locally to get the same checks CI has.

@NanduTej
Copy link
Author

NanduTej commented Jul 18, 2019

@sobolevn I think my parts of the code got through. There are errors in the previous code.

@sobolevn
Copy link
Member

sobolevn commented Jul 18, 2019

There are no issues visible to CI in the projects right now: https://travis-ci.org/wemake-services/wemake-python-styleguide/builds

But, new rules - reveal new problems! That's why we build new rules 💪
There are several things to fix here in your PR.

First issue

    def _check_first_element(
        self,
        node: ast.AST,
        statement: ast.AST,
        extra_lines: int,
    ) -> Optional[bool]:
        if statement.lineno == node.lineno and not extra_lines:
            return False
        return None

This code should not raise any violations. It is correct. See the original issue (3rd example):
Снимок экрана 2019-07-18 в 15 28 10

This code from ./wemake_python_styleguide/logic/naming/name_nodes.py is also correct:

def get_assigned_name(node: ast.AST) -> Optional[str]:
    """
    Returns variable names for node that is just assigned.

    Returns ``None`` for nodes that are used in a different manner.
    """
    if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):
        return node.id

    if isinstance(node, ast.Attribute) and isinstance(node.ctx, ast.Store):
        return node.attr

    if isinstance(node, ast.ExceptHandler):
        return getattr(node, 'name', None)

    return None  # this is a meaningful value!

Please, make sure that all the examples from the original task work.
Add them in tests if they are missing right now.

Second issue

./wemake_python_styleguide/visitors/ast/keywords.py
  215:1    WPS214 Found too many methods: 8
  @final
  ^

It means that your new method makes the class too complex. Here's how to fix it:
Move some logic from the class. How to define that this is pure logic and not methods? Look at _get_return_node_variables and _get_name_nodes_variable and _get_assign_node_variables - they don't use self at all (we might actually build a new rule to check that). It means that they can be extracted to logic/returns.py file. Then you will have extra capacity to add your new method without this violation.

Thanks you for your work! I really appreciate it 👍
Let's make an awesome product together! 🎉

@NanduTej
Copy link
Author

NanduTej commented Jul 19, 2019

@sobolevn sorry for spamming so many commits, my flake8 does not give me the full messages as the build was giving me, so I pushed all the changes I could see and tested them. I am still confused as to what the issue is with the 2 'return None'. I have added all the tests mentioned in the issue and all of them are passing. Can you help me out?

@sobolevn
Copy link
Member

@NanduTej the easiest way to fix it is:

  1. create ex.py with the code that raises a violation
  2. run it with scripts/parse.py to see the ast, it might give you a hint on what is going on
  3. debug your visitor on why it is happening: use print(node, vars(node)) or pdb
  4. add this code sample to the tests, so we can be sure that the problem is fixed, more tests = better product

@sobolevn
Copy link
Member

If you setup does not match the one from CI, try the clean build:

deactivate
rm -rf dist pip-wheel-metadata wemake_python_styleguide.egg-info .venv
poetry install
poetry run make test

@NanduTej
Copy link
Author

If you setup does not match the one from CI, try the clean build:

deactivate
rm -rf dist pip-wheel-metadata wemake_python_styleguide.egg-info .venv
poetry install
poetry run make test

Thanks! This worked.

@sobolevn
Copy link
Member

@NanduTej how's it going? Do you need any help from my side? 🙂

@NanduTej
Copy link
Author

@sobolevn Sorry, didn't finish the work on this. I am out for my convocation ceremony😄. Will get back and work on it in 3 days.

@sobolevn
Copy link
Member

Had to google what "convocation ceremony" is 😆
Congratulations! 🎉

Feel free to get back to it when you have the time.

@NanduTej
Copy link
Author

Hey @sobolevn! I tried to debug but could not get anywhere. Can you help me out further? I did create the ex.py file and pasted the 2 pieces of code and ran the parse.py program. I did not find anything out of order in the output.

@sobolevn
Copy link
Member

@NanduTej what exactly is your problem?

@NanduTej
Copy link
Author

On running flake8 ., There is an error popping up.


./wemake_python_styleguide/logic/naming/name_nodes.py

29:5 WPS331 Found local variable that are only used in return statements
return None
^

./wemake_python_styleguide/visitors/ast/statements.py

219:9 WPS331 Found local variable that are only used in return statements
return None
^


I am unable to debug this.

returns, return_sub_nodes = self._get_return_node_variables(nodes)
assign = get_assign_node_variables(nodes)
names = get_name_nodes_variable(nodes)
returns, return_sub_nodes = get_return_node_variables(nodes)
Copy link
Member

Choose a reason for hiding this comment

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

You can insert breakpoint() (if you are using 3.7, otherwise see https://realpython.com/python-debugging-pdb/) and debug this function.

Copy link
Member

Choose a reason for hiding this comment

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

You can also use ipbd:

  1. pip install ipdb
  2. export PYTHONBREAKPOINT=ipdb.set_trace
  3. Set breakpoint() where you need it
  4. Run flake8 on file that raises an unwanted violation

@NanduTej
Copy link
Author

Hey @sobolevn! Can you explain this to me?

@sobolevn
Copy link
Member

sobolevn commented Jul 26, 2019

@NanduTej this is a integration test for several things:

  1. that violation is active and enabled
  2. that violation is raised for the bad code
  3. that line number where violation is raised is correct
  4. that noqa works
    Docs: https://github.com/wemake-services/wemake-python-styleguide/blob/master/CONTRIBUTING.md#before-submitting

@NanduTej
Copy link
Author

Hey @sobolevn! I am sorry I am taking so much time with this. Can you help me with understanding the flow of how flake8 works. Maybe that will give me a clue as to where to look for the error.

@sobolevn
Copy link
Member

@NanduTej can you please show me the piece of code that does not work for you? Maybe I can help you from there. Since, I am pretty sure that flake8 has nothing to do with this task.

(In case you still need this information, it is right in our docs: https://wemake-python-stylegui.de/en/latest/pages/api/index.html#overview and here's how all classes work: https://wemake-python-stylegui.de/en/latest/pages/api/index.html#api-reference)

@NanduTej
Copy link
Author

NanduTej commented Jul 31, 2019

On running flake 8, I run into this error.


./wemake_python_styleguide/logic/naming/name_nodes.py
29:5 WPS331 Found local variable that are only used in return statements
return None
^

./wemake_python_styleguide/visitors/ast/statements.py

219:9 WPS331 Found local variable that are only used in return statements
return None
^


The code is executing fine with all my tests. When I push my code, this error will make build fail which is coming when flake8 command is being run.

@sobolevn
Copy link
Member

So, then add these cases to your tests:

correct_example_N = """
def get_assigned_name(node: ast.AST) -> Optional[str]:
    if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):
        return node.id

    if isinstance(node, ast.Attribute) and isinstance(node.ctx, ast.Store):
        return node.attr

    if isinstance(node, ast.ExceptHandler):
        return getattr(node, 'name', None)

    return None
"""

And:

correct_example_N1 = """
class WrongAttributeVisitor(object):
     def _is_super_called(self, node: ast.Call) -> bool:
        if isinstance(node.func, ast.Name):
            if node.func.id == 'super':
                return True
        return False
"""

And then debug what is going on: I wrote a debugging section of documentation just for this case.
https://wemake-python-stylegui.de/en/latest/pages/api/debugging.html

P.S. Don't forget to rebase your code.

return_sub_nodes: Dict[str, ast.Return] = defaultdict()
for sub_node in node:
if isinstance(sub_node, ast.Return):
if isinstance(sub_node.value, ast.Name):
Copy link
Author

Choose a reason for hiding this comment

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

For the correct_example_N we are not collecting the previous returns where I expected them to be collected in return_sub_nodes and so we are running into issues here. The correct_example_N1 is passing the tests. How should we collect those returns here? Example the returns of type ast.Attribute, and should we be collecting them here?
@sobolevn

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I have missed your question. Can you try to use ast.walk(subnode) there? I guess that happens because they are not direct children of the give node, but are lower down the tree.

Copy link
Member

Choose a reason for hiding this comment

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

You can try some other task if you are stuck with one. And pass this one to me.

Copy link
Author

Choose a reason for hiding this comment

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

Sure

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.

2 participants