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

Report method definition lineno when reporting property #200

Merged
merged 3 commits into from
Mar 29, 2020

Conversation

RJ722
Copy link
Contributor

@RJ722 RJ722 commented Mar 14, 2020

Description

Previously, we reported the lineno on which the @property annotation was made. It, however makes sense to report the line on which the method is actually defined.

This was also non-consistent behavior b/w Vulture running on Python < 3.8 and >= 3.8.

Related Issue

#195 (comment)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation in the README file.
  • I have updated the documentation accordingly.
  • I have added an entry in CHANGELOG.md.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@RJ722
Copy link
Contributor Author

RJ722 commented Mar 14, 2020

@jendrikseipp I took the liberty of adding some more simple, but more concrete tests to make sure that we do check for properties of individual unused items somewhere. Let me know if this doesn't right.

@coveralls
Copy link

coveralls commented Mar 14, 2020

Pull Request Test Coverage Report for Build 713

  • 8 of 8 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.5%) to 98.925%

Totals Coverage Status
Change from base Build 693: -0.5%
Covered Lines: 552
Relevant Lines: 558

💛 - Coveralls

@RJ722 RJ722 mentioned this pull request Mar 14, 2020
6 tasks
CHANGELOG.md Outdated Show resolved Hide resolved
var = v.unused_vars[0]
assert var.name == "v"
assert var.first_lineno == 1
assert var.last_lineno == 1
Copy link
Owner

Choose a reason for hiding this comment

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

Nice additional tests! Can you move them to a separate file, please? Maybe "test_items.py"?

vulture/core.py Outdated
# For python < 3.8, increment as many line numbers as there are
# decorators on the method.
# From Python3.8 onwards, lineno for property is the same as
# that of decorated method.
Copy link
Owner

Choose a reason for hiding this comment

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

Let's turn this into a docstring and rewrite it slightly:

For decorated methods, Python < 3.8 uses the first decorator to report the first line number. To obtain the line number of the actual method definition (as in Python >= 3.8), we add the number of decorators to the line number. Obviously, this is only exact if all decorators use only one line.

Copy link
Owner

Choose a reason for hiding this comment

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

Can we maybe make this an exact line number (even for multiline decorators)? For this, we would need to be able to retrieve the method's AST node from the property's AST node and call .lineno on it. Does the property's AST node have the relevant fields? You can inspect this on Python 2.7 with dir(node).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Obviously, this is only exact if all decorators use only one line.

Yeah, that is a problem. We need more test cases here.

For this, we would need to be able to retrieve the method's AST node from the property's AST node and call .lineno on it

I'm a little confused with this. Aren't AST nodes for property and method the same?

Copy link
Owner

Choose a reason for hiding this comment

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

What I meant was the FunctionDef AST node and a decorator AST node. To retrieve the method line number from the FunctionDef node node, we could use lines.get_last_line_number(node.decorator_list[-1]). However, this is also just an approximation. Thinking about this again, I belive that it would be best to simply report the first line number of the first decorator for unused properties for Python 3.8+. This preserves the behaviour of Vulture for newer Python versions and is also more accurate for counting the size of the unused code chunk (if the property is unused, we also don't need it's decorators). Most importantly, it's easy to compute the line number accurately (should work for all Python versions, but deserves a comment about why we don't use node.lineno): node.decorator_list[0].lineno.

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 tried lines.get_last_line_number(node.decorator_list[-1]) for a bunch of cases, and I agree about the inaccuracy, eg., it would fail here:

class TestFoo:
    @pytest.mark.parametrize(
        "line, codes",
        [
            ("# noqa", ["all"]),
            ("## noqa", ["all"]),
        ],
    )
    @property
    @pytest.mark.parametrize(
        "line, codes",
        [
            ("# noqa", ["all"]),
        ],
    )
    def test_noqa_regex_present(line, codes):
        pass

Also, I can't remember why this PR only targeted properties, and not all decorated functions? 😅

Anyways, I'll update this PR for all functions with the changes you mentioned above.

@RJ722
Copy link
Contributor Author

RJ722 commented Mar 28, 2020

Apologies for the delay @jendrikseipp. I've updated the PR according to the recent discussion.

@@ -1,6 +1,8 @@
# Unreleased

* Ignore unused import statements which occur in `__init__.py` (RJ722, #192).
* Report first decorator's line number for decorated objects on Python 3.8+.
(RJ722, #200)
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'm not sure -- do we need to create an entry in CHANGELOG for this?

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, I think it's good to reflect the change somewhere.

@@ -1,6 +1,8 @@
# Unreleased

* Ignore unused import statements which occur in `__init__.py` (RJ722, #192).
* Report first decorator's line number for decorated objects on Python 3.8+.
(RJ722, #200)
Copy link
Owner

Choose a reason for hiding this comment

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

Yes, I think it's good to reflect the change somewhere.

tests/test_report.py Show resolved Hide resolved
vulture/lines.py Outdated Show resolved Hide resolved
vulture/lines.py Outdated Show resolved Hide resolved
vulture/lines.py Outdated
# for newer Python versions, which is also more accurate for
# counting the size of the unused code chunk (if the property
# is unused, we also don't need it's decorators), we return the
# lineno of the first decorator, if there are any.
Copy link
Owner

Choose a reason for hiding this comment

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

Nice text!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hehe, I just copied over your comments.

vulture/lines.py Outdated Show resolved Hide resolved
@jendrikseipp jendrikseipp merged commit 672947b into jendrikseipp:master Mar 29, 2020
@jendrikseipp
Copy link
Owner

Thanks for your work and persistence!

@RJ722 RJ722 deleted the prop-lineno branch March 29, 2020 16:29
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.

3 participants