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 string normalization #33

Merged
merged 1 commit into from Oct 4, 2021

Conversation

terencehonles
Copy link
Contributor

This change updates annotation comparison to first normalize the original string leaf nodes to their repr form which is what is generated from the ast (the pyi file).

fixes #24

@codecov-commenter
Copy link

codecov-commenter commented May 15, 2021

Codecov Report

Merging #33 (1731abf) into main (e759da7) will increase coverage by 0.17%.
The diff coverage is 92.85%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #33      +/-   ##
==========================================
+ Coverage   78.34%   78.52%   +0.17%     
==========================================
  Files           4        4              
  Lines         956      964       +8     
  Branches      219      223       +4     
==========================================
+ Hits          749      757       +8     
  Misses        167      167              
  Partials       40       40              
Flag Coverage Δ
tests 78.52% <92.85%> (+0.17%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/retype/__init__.py 86.20% <92.85%> (+0.13%) ⬆️

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 e759da7...1731abf. Read the comment docs.

This change updates annotation comparison to first normalize the
original string leaf nodes to their repr form which is what is generated
from the ast (the pyi file).
Copy link
Contributor Author

@terencehonles terencehonles left a comment

Choose a reason for hiding this comment

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

Adding some possible questions / context


node.children = [normalize_node(i) for i in node.children]
if isinstance(node, 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.

I probably should have done this in #30, but since it won't touch the children it doesn't have to touch


return node


def maybe_replace_any_if_equal(name, expected, actual, flags):
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 a little unsure of this function... if the strings are equal shouldn't the original be used rather than the one we generated? I believe the return should probably be actual not expected (and I'm not sure why a fallback is given, but it could be given in reverse order).

I'm also a little confused about the Any normalization. From the comment it seems like people should be able to keep their original Any, but if you don't use --replace-any then it will error out instead of leaving things as is, but if you use --replace-any it doesn't check for nested Anys and was very naively stripping quotes. I'm guessing it probably was OK, but I updated this to look at the node type in order to strip the quotes and used the normalized string so that it will always be a single single quote (I'm not sure if """ or ''' are allowed for Leaf(STRING) values).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, I can't decide on this. This part was mostly written by Lukasz so don't have a good answer here.

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 in a rush on this, and I'm thinking we should probably be recursively checking for Any in case it's wrapped in something like Optional[Any] or Dict[str, Any] etc, but I'm not sure if --replace-any is targeting strictly Any that is not contained in anything else.

This code also seems like it's allowing a string form of Any to be normalized into a non string form of Any so checking nodes directly would not be sufficient (and why it's str(node) first) since Leaf(STRING, "'Any'") != Leaf(NAME, "Any") (and other Any forms that are captured) so if it was recursively allowing Any it would not be as trivial as looking for Leaf(String) and normalizing Anys to a single form "like" what I'm doing to normalize to the repr form.

@@ -53,30 +53,33 @@ def test_can_run_against_current_directory(tmp_path):
class RetypeTestCase(TestCase):
maxDiff = None

def assertReapply(
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 accidentally had too much whitespace in my expected output, but I was stepping through what the assertion was doing to figure out if I should be using a different one and I realized they were all duplicates some of which having slightly different orders so I figured I'd remove that duplication.

@gaborbernat gaborbernat merged commit b4b19dc into ambv:main Oct 4, 2021
@terencehonles terencehonles deleted the fix-string-normalization branch October 4, 2021 16:30
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.

single and double quotes in literals not handled correctly
3 participants