Skip to content
This repository was archived by the owner on Feb 19, 2023. It is now read-only.

No single letter variables #25

Merged
merged 2 commits into from
Jun 20, 2021

Conversation

klssmith
Copy link
Contributor

This allows _ to be used as a variable name, since it is conventionally
used for throwaway variables, but does not allow any other single letter
variables.

The target method of ast.Assigns will normally contain a list of nodes,
however for an unpacking assignment (such as 'a,b = c') it will contain
an ast.Tuple or an ast.List so we need to cover for all cases.

@klssmith
Copy link
Contributor Author

@MarcoGorelli This is giving an error with the mypy precommit hook (error: "expr" has no attribute "id” on line 22 of single_letter_variables.py) which I can’t figure out. It’s also more complicated than originally discussed to cover for a few edge cases, so I’m not sure if this is still along the right lines or not.

@codecov
Copy link

codecov bot commented Jun 19, 2021

Codecov Report

Merging #25 (1c6f213) into main (92aff77) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main       #25   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           47        49    +2     
  Lines          633       659   +26     
  Branches        80        84    +4     
=========================================
+ Hits           633       659   +26     
Impacted Files Coverage Δ
tests/string_to_concatenate_test.py 100.00% <ø> (ø)
...ev_flaker/_plugins_tree/single_letter_variables.py 100.00% <100.00%> (ø)
tests/single_letter_variables_test.py 100.00% <100.00%> (ø)

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 92aff77...1c6f213. Read the comment docs.

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Awesome work, I'm really impressed you figured this all out!

For the mypy issue, we just need to "help" mypy and tell it that the target elements are ast.Names, and hence that they have the id attribute. You probably want to write it like this:

    for item in assignment_names:
        if isinstance(item, ast.Name) and item.id != "_" and len(item.id) == 1:
            yield node.lineno, node.col_offset, MSG

cc @jbrockmendel in case you have comments - this'll raise an error all over the place in pandas, but it can be fixed incrementally

@jbrockmendel
Copy link
Member

This is pretty neat. _ makes sense to special-case. Maybe also e.g. for i in range(...)? (i know we do that a lot in the cython code)

@MarcoGorelli
Copy link
Member

Thanks for taking a look - for i in range wouldn't be covered by this, as that's ast.For rather than ast.Assign

@klssmith if you mark this as "ready for review" we can merge it

@klssmith klssmith force-pushed the no-single-letter-variables branch from 7fcbe1a to a67aecc Compare June 20, 2021 08:50
@klssmith
Copy link
Contributor Author

Thanks for the comments.

@MarcoGorelli I've made the change needed to pass the mypy precommit check. I've not changed this to ready to review just yet because I've noticed that the col_offset in the error message can be misleading in a few situations. A file with this line

abc, d = (5, 6)

gives the warning of

test.py:1:1: PDF023 found assignment to single-letter variable

Since the single letter variable is at col_offset 6, I assume the message should read test.py:1:6: PDF023 ... in this case? If so, I can take a look at how the col_offset in error message can be fixed.

@MarcoGorelli
Copy link
Member

when you're in the for loop, does item have .lineno and coloffset attributes? If so, perhaps use those in the message?

This allows `_` to be used as a variable name, since it is conventionally
used for throwaway variables, but does not allow any other single letter
variables.

The `target` method of `ast.Assigns` will normally contain a list of nodes,
however for an unpacking assignment (such as `'a,b = c'`) it will contain
an `ast.Tuple` or an `ast.List` so we need to cover for all cases.
@klssmith klssmith force-pushed the no-single-letter-variables branch from a67aecc to 1c6f213 Compare June 20, 2021 19:41
@klssmith
Copy link
Contributor Author

👍 I was able to replace yield node.lineno, node.col_offset, MSG with yield item.lineno, item.col_offset, MSG as you suggested to get the col_offset working.

@klssmith klssmith marked this pull request as ready for review June 20, 2021 19:50
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Awesome work! Thanks @klssmith !

If you enjoyed this kind of work (ast-parsing), I'd suggest you have a look at pyupgrade too - their maintainer is amazing, I've learned a ton from contributing there

@MarcoGorelli MarcoGorelli merged commit 8be2d08 into pandas-dev:main Jun 20, 2021
@klssmith
Copy link
Contributor Author

Thanks for the help @MarcoGorelli! I will check out pyupgrade

@klssmith klssmith deleted the no-single-letter-variables branch June 20, 2021 20:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants