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

Omit no-member error if guarded behind if stmt #4764

Merged
merged 5 commits into from Jul 28, 2021

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Jul 28, 2021

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

Omit no-member errors if attribute / method access is guarded behind if statement.

This change doesn't implement control-flow logic. Instead I chose to check for an IF / IFExp node in the node parents and evaluate it. That doesn't solve all issue, especially not those with assert #4693, but I would never the less consider it an improvement.

As this is a larger change, I would like to target 2.10 for it.

Example

from datetime import datetime

value = "Hello World"
value.isoformat()  # [no-member]

if isinstance(value, datetime):
    value.isoformat()  # This is now fixed and doesn't emit 'no-member' anymore

--
Ref #1162 (not closed, but issue would be addressed)
Closes #1990
Closes #4168

@cdce8p cdce8p added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jul 28, 2021
@cdce8p cdce8p added this to the 2.10.0 milestone Jul 28, 2021
@coveralls
Copy link

coveralls commented Jul 28, 2021

Coverage Status

Coverage increased (+0.005%) to 92.225% when pulling b4cd3fd on cdce8p:fix-no-member into 24d03e9 on PyCQA:main.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

I'm surprised that this does not break more functional tests. I think this is a nice way to remove some false positive using astroid inference.

# Don't emit no-member if guarded behind `IF` or `IFExp`
# * Walk up recursively until if statement is found.
# * Check if condition can be inferred as `Const`,
# would evaluate as `False`,
Copy link
Member

Choose a reason for hiding this comment

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

That's pretty clever.

@Pierre-Sassoulas Pierre-Sassoulas merged commit 107f59e into pylint-dev:main Jul 28, 2021
@cdce8p cdce8p deleted the fix-no-member branch July 28, 2021 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

false positive no-member after isinstance Add isinstance in if condition check as a special case
3 participants