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

Ensure --fail-on always returns a non-zero exit code when issue is matched #4713

Merged
merged 6 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -512,3 +512,5 @@ contributors:
* Daniel Dorani (doranid): contributor

* Will Shanks: contributor

* Mark Bell: contributor
2 changes: 2 additions & 0 deletions ChangeLog
Expand Up @@ -17,6 +17,8 @@ Release date: TBA
..
Put bug fixes that should not wait for a new minor version here

* Fix bug in which --fail-on can return a zero exit code even when the specified issue is present
MarkCBell marked this conversation as resolved.
Show resolved Hide resolved

* Fix hard failure when handling missing attribute in a class with duplicated bases

Closes #4687
Expand Down
4 changes: 3 additions & 1 deletion pylint/lint/pylinter.py
Expand Up @@ -707,7 +707,9 @@ def enable_fail_on_messages(self):
self.fail_on_symbols.append(msg.symbol)

def any_fail_on_issues(self):
return any(x in self.fail_on_symbols for x in self.stats["by_msg"])
return self.stats is not None and any(
x in self.fail_on_symbols for x in self.stats["by_msg"]
)

def disable_noerror_messages(self):
for msgcat, msgids in self.msgs_store._msgs_by_category.items():
Expand Down
13 changes: 6 additions & 7 deletions pylint/lint/run.py
Expand Up @@ -394,14 +394,13 @@ def __init__(
if exit:
if linter.config.exit_zero:
sys.exit(0)
elif linter.any_fail_on_issues():
# We need to make sure we return a failing exit code in this case.
# So we use self.linter.msg_status if that is non-zero, otherwise we just return 1.
sys.exit(self.linter.msg_status or 1)
elif score_value and score_value >= linter.config.fail_under:
sys.exit(0)
else:
if (
score_value
and score_value >= linter.config.fail_under
# detected messages flagged by --fail-on prevent non-zero exit code
and not linter.any_fail_on_issues()
):
sys.exit(0)
sys.exit(self.linter.msg_status)

def version_asked(self, _, __):
Expand Down