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

Verify nodes' str() and repr() don't raise errors/warnings #2198

Merged
merged 1 commit into from
Jun 12, 2023

Conversation

crazybolillo
Copy link
Contributor

@crazybolillo crazybolillo commented Jun 6, 2023

Calling str() or repr() on certain nodes fails either with errors or warnings. This commit adds unittests to find and reproduce this behavior with the endgoal of fixing it.

Closes #1881

Type of Changes

Type
🐛 Bug fix

@crazybolillo
Copy link
Contributor Author

I changed the test flavor to pytest and fixed all str and repr issues found in the tests. After all I had to add some more failsafes in other places.

@crazybolillo
Copy link
Contributor Author

Also something weird is going on with the PR, since it is trying to reapply a commit already found in the main branch (same hash and everything), I guess it has to do with me not having some sort of permission to update the PR or execute a certain flow.

@Pierre-Sassoulas
Copy link
Member

Could you rebase or merge main in your branch ?

@crazybolillo
Copy link
Contributor Author

Could you rebase or merge main in your branch ?

It should be. My fork shows my branch being just one commit ahead and none behind. 😮

@crazybolillo crazybolillo force-pushed the 1881-crazybolillo branch 3 times, most recently from 5ac8540 to 5fa419b Compare June 7, 2023 17:59
@codecov
Copy link

codecov bot commented Jun 7, 2023

Codecov Report

Merging #2198 (f5028e2) into main (61ca2e8) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2198      +/-   ##
==========================================
+ Coverage   92.68%   92.71%   +0.02%     
==========================================
  Files          94       94              
  Lines       10830    10830              
==========================================
+ Hits        10038    10041       +3     
+ Misses        792      789       -3     
Flag Coverage Δ
linux 92.47% <100.00%> (+0.02%) ⬆️
pypy 87.97% <100.00%> (+0.33%) ⬆️
windows 92.30% <100.00%> (+0.02%) ⬆️

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

Impacted Files Coverage Δ
astroid/nodes/node_ng.py 93.67% <100.00%> (+0.90%) ⬆️

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Thanks for doing this! I really like the test you came up with!

if name == "self":
continue

if "int" in param_type.annotation:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like this trick, never thought of doing it like this!

@@ -1350,7 +1350,7 @@ def fromlineno(self) -> int:
# lineno is the line number of the first decorator, we want the def
# statement lineno. Similar to 'ClassDef.fromlineno'
lineno = self.lineno or 0
if self.decorators is not None:
if getattr(self, "decorators", None) is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer just setting decorators in the __init__. Since it can be None anyway this doesn't interfere with the typing of the attribute.

@@ -246,7 +246,7 @@ def accept(self, visitor):
def get_children(self) -> Iterator[NodeNG]:
"""Get the child nodes below this node."""
for field in self._astroid_fields:
attr = getattr(self, field)
attr = getattr(self, field, None)
Copy link
Collaborator

Choose a reason for hiding this comment

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

For which of the nodes is this necessary? get_children isn't only used in __str__ so I'm worried people might depend on this returning an AttributeError.

@@ -859,33 +859,33 @@ def find_argname(self, argname, rec=False):
return None, None

def get_children(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

See other comment. If we could find a way where we only impact __str__ and __repr__ I would be very much in favour of that.

@jacobtylerwalls jacobtylerwalls added this to the 3.0.0a5 milestone Jun 8, 2023
@crazybolillo crazybolillo force-pushed the 1881-crazybolillo branch 2 times, most recently from 9b6dd82 to f556237 Compare June 8, 2023 15:46
@crazybolillo
Copy link
Contributor Author

Ah I see, makes sense to only touch __str__ and __repr__. Apparently the only node that had issues after patching value = getattr(self, field) was MatchCase.

The error is the following:
AttributeError: 'MatchCase' object has no attribute 'pattern'

I solved inside __repr__ so nothing else is modified now.

Thanks for the review 🤝

Copy link
Member

@jacobtylerwalls jacobtylerwalls left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on! Will approve and merge once the comment is added that's discussed above.

Calling str() or repr() on certain nodes fails either with errors
or warnings. A unittest was added to verify this behaviour and find
the offending nodes. Code has been corrected, mainly by
accesing node's attributes safely and using placeholders if necessary.

Closes  pylint-dev#1881
@crazybolillo
Copy link
Contributor Author

Thanks to you guys for the insightful comments, I also prefer the list comprehension, I changed that and added the missing comment 🤝

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Thanks for picking this up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Some __str__ methods of nodes raise errors and warnings
4 participants