-
Notifications
You must be signed in to change notification settings - Fork 162
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
Conversation
@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. |
Pull Request Test Coverage Report for Build 713
💛 - Coveralls |
tests/test_scavenging.py
Outdated
var = v.unused_vars[0] | ||
assert var.name == "v" | ||
assert var.first_lineno == 1 | ||
assert var.last_lineno == 1 |
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.
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. |
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.
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.
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.
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)
.
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.
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?
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.
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
.
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 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.
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) |
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'm not sure -- do we need to create an entry in CHANGELOG for this?
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.
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) |
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.
Yes, I think it's good to reflect the change somewhere.
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. |
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.
Nice text!
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.
Hehe, I just copied over your comments.
Thanks for your work and persistence! |
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: