Skip to content

Conversation

@Pierre-Sassoulas
Copy link
Member

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

Closes #8067

@Pierre-Sassoulas Pierre-Sassoulas added the Crash πŸ’₯ A bug that makes pylint crash label Jan 17, 2023
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.16.0 milestone Jan 17, 2023
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.

I don't really like using hasattr as it is not exactly type safe. Can't we use isinstance?

@codecov
Copy link

codecov bot commented Jan 17, 2023

Codecov Report

Merging #8070 (e1a478d) into main (4a5b4e6) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8070      +/-   ##
==========================================
+ Coverage   95.43%   95.44%   +0.01%     
==========================================
  Files         176      176              
  Lines       18545    18545              
==========================================
+ Hits        17698    17700       +2     
+ Misses        847      845       -2     
Impacted Files Coverage Ξ”
pylint/checkers/method_args.py 100.00% <100.00%> (ΓΈ)
pylint/checkers/refactoring/refactoring_checker.py 98.33% <100.00%> (+0.20%) ⬆️

@github-actions

This comment has been minimized.

@Pierre-Sassoulas
Copy link
Member Author

Maybe we could check that it's a base.Instance ? I've seen that somewhere. Did you have something in mind ?

@DanielNoord
Copy link
Collaborator

Maybe we could check that it's a base.Instance ? I've seen that somewhere. Did you have something in mind ?

Purely based on the next line (self.linter.config.timeout_methods) I guess we're looking for FunctionDefs here?

@Pierre-Sassoulas
Copy link
Member Author

Given our current test, it can be

<class 'astroid.nodes.scoped_nodes.scoped_nodes.FunctionDef'>
<class 'astroid.nodes.scoped_nodes.scoped_nodes.ClassDef'>
<class 'astroid.bases.BoundMethod'>

(and create false negative according to our tests if we don't handle those cases). What's wrong with hasattr exactly ?

@DanielNoord
Copy link
Collaborator

Given our current test, it can be

<class 'astroid.nodes.scoped_nodes.scoped_nodes.FunctionDef'>
<class 'astroid.nodes.scoped_nodes.scoped_nodes.ClassDef'>
<class 'astroid.bases.BoundMethod'>

(and create false negative according to our tests if we don't handle those cases). What's wrong with hasattr exactly ?

class A:
    def qname(self):
        return 1

πŸ˜„

There is nothing that says that qname returns a string like we expect it to. I'm also not opposed to adding it to all nodes which seems much simpler than tackling these one by one.

@jacobtylerwalls
Copy link
Member

I'm also not opposed to adding it to all nodes which seems much simpler than tackling these one by one.

Marc was against that here, but it also comes up regularly, see pylint-dev/astroid#1373.

@Pierre-Sassoulas
Copy link
Member Author

Hmm yeah, right thank you I understand. The problematic version would be this:

class A:
    def qname(self):
        return "threading.lock.acquire"

Because then it's a false positive for consider-using-with. Are you proposing to add qname to all node in astroid directly ? I was going to propose that because clearly Slice has in fact a qname, and it's builtin.Slice, right ?

@Pierre-Sassoulas
Copy link
Member Author

Being able to use qname on all nodes would obviously be great in order to not crash when we get unexpected nodes. (Could assignAttr qname be the name of the variable with full namespace ?) But it's a big decisions with a required astroid upgrade, maybe hasattr is a decent temporary fix.

@Pierre-Sassoulas Pierre-Sassoulas added the qname Issue related to qname label Jan 17, 2023
@DanielNoord
Copy link
Collaborator

Yeah I'm not sure. hasattr is just asking for trouble further down the line. When we add py.typed to astroid all of our incorrect calls to qname() for nodes that don't have it will be flagged anyway.
Here it does feel like we could get away with doing a check for ClassDef, FunctionDef and the "lowest type" of either UnboundMethod or BoundMethod (I always forget which inherits which).

@Pierre-Sassoulas
Copy link
Member Author

Let's the how the primers react then

@Pierre-Sassoulas
Copy link
Member Author

There's a whole lot of other call to hasattr accross the codebase (qname, attrname, name). Probably something to do in another refactor merge request or after the astroid qname discussion.

@github-actions

This comment has been minimized.

@tushar-deepsource
Copy link
Contributor

Hey! This does fix the first crash in the issue, but it doesn't fix the second crash in RefactoringChecker, mentioned here.

Dropping the code snippet here as well:

class Foo:
    def __init__(self, foo=slice(42)):
        self.foo = foo


def bar():
    i = Foo()
    i.foo()
    return 100

and node.returns.name == "NoReturn"
)
try:
return node.qname() in self._never_returning_functions
Copy link
Collaborator

Choose a reason for hiding this comment

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

Clearly node is typed incorrectly, but oh well πŸ˜„

@Pierre-Sassoulas Pierre-Sassoulas merged commit ee55ec8 into pylint-dev:main Jan 18, 2023
@Pierre-Sassoulas Pierre-Sassoulas deleted the fix-8067 branch January 18, 2023 12:12
@github-actions
Copy link
Contributor

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit e1a478d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Crash πŸ’₯ A bug that makes pylint crash qname Issue related to qname

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inference crash: Slice has no attribute 'qname'

4 participants