Skip to content

Use qualified name when checking for overgeneral exceptions #7497

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

Merged
merged 26 commits into from
Nov 13, 2022
Merged

Use qualified name when checking for overgeneral exceptions #7497

merged 26 commits into from
Nov 13, 2022

Conversation

Jackenmen
Copy link
Contributor

Type of Changes

Type
βœ“ πŸ› Bug fix
βœ“ πŸ”¨ Refactoring

Description

Allow specifying non-builtin exceptions in overgeneral-exception option using exception's qualified name.

Additionally, tries to deprecate specifying built-in exception names without dots. Small problem - I don't know how to tell pylint to warn about deprecation as warnings.warn(...) does not seem to be a valid way to do that.

Closes #7495

@coveralls
Copy link

coveralls commented Sep 19, 2022

Pull Request Test Coverage Report for Build 3455059518

  • 14 of 15 (93.33%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.002%) to 95.38%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pylint/checkers/exceptions.py 14 15 93.33%
Files with Coverage Reduction New Missed Lines %
pylint/checkers/classes/class_checker.py 2 94.8%
Totals Coverage Status
Change from base Build 3452506349: -0.002%
Covered Lines: 17301
Relevant Lines: 18139

πŸ’› - Coveralls

@github-actions

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Sep 20, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.16.0 milestone Sep 20, 2022
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.

Look good thank you ! I think this is a breaking change that we need to put in 3.0 though

@Jackenmen
Copy link
Contributor Author

Jackenmen commented Sep 20, 2022

It keeps compatibility with the old way (names without dots are handled as they've been before and names with dots weren't supported in any way before) so I think it should be fine? The long-term goal is to remove the old way in 3.0 though.

The problem I've run into is that I don't really know how I should make pylint actually show the deprecation warning to the user (warnings.warn that's currently in here doesn't seem to work) which is why this PR is a draft.

@Pierre-Sassoulas Pierre-Sassoulas added Needs review πŸ” Needs to be reviewed by one or multiple more persons Needs specification πŸ” Accepted as a potential improvement, and needs to specify edge cases, message names, etc. labels Sep 20, 2022
@DanielNoord DanielNoord removed the Needs review πŸ” Needs to be reviewed by one or multiple more persons label Sep 20, 2022
@github-actions

This comment has been minimized.

@Pierre-Sassoulas
Copy link
Member

Is this still a draft @jack1142 ?

@Jackenmen
Copy link
Contributor Author

Is this still a draft @jack1142 ?

I was waiting for a resolution of the warning discussion before committing any changes. Should be ready for review now.

@Jackenmen Jackenmen marked this pull request as ready for review September 24, 2022 16:57
@Pierre-Sassoulas Pierre-Sassoulas removed the Needs specification πŸ” Accepted as a potential improvement, and needs to specify edge cases, message names, etc. label Sep 24, 2022
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.

Looks pretty refined already, good job !

"broad-except",
args=exception.name,
node=handler.type,
confidence=INFERENCE,
Copy link
Member

Choose a reason for hiding this comment

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

πŸ‘

@github-actions

This comment has been minimized.

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.

LGTM πŸ‘

@DanielNoord
Copy link
Collaborator

Shit.. This needs a rebase due to the conflicts. I'm sorry about that @jack1142.

I've been very busy. I promise that if you rebase this PR and tag me I'll make sure to merge it immediately after and not let this slip πŸ˜…

@Jackenmen
Copy link
Contributor Author

Damn, conflict with 4 files πŸ˜„ I will do it tomorrow as I'm a bit busy tonight, thanks for letting me know!

@DanielNoord
Copy link
Collaborator

Yeah this is our bad probably. I have recently started a new job and have had less time for OS. This has messed a bit with out review/merge flow I think.

Like I said, make sure to ping my directly as I'll be more likely to see and follow up on it πŸ˜„

@Pierre-Sassoulas
Copy link
Member

Probably due to #7709, you're going to have to rename old broad-except and consider the new check too, sorry for letting this go stale.

@Jackenmen
Copy link
Contributor Author

Jackenmen commented Nov 10, 2022

broad-exception-caught doesn't get triggered if the except block raises so these test additions don't seem to make sense

We could add few more try except blocks which ensure that message isn't reported I suppose.

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.

You're right, sorry for the noise !

@Pierre-Sassoulas Pierre-Sassoulas enabled auto-merge (squash) November 10, 2022 12:57
auto-merge was automatically disabled November 12, 2022 20:52

Head branch was pushed to by a user without write access

@github-actions

This comment has been minimized.

@jacobtylerwalls jacobtylerwalls self-requested a review November 12, 2022 22:16
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.

I'm concerned about the changes shown in the primer. Most of the patterns there look benign, where a broad exception is caught, and then some logging/cleanup is done, and then the original error is re-raised. I didn't think we had consensus on changing that (or at least, I don't see any discussion in the issue.) I think the status quo should be preserved where directly raising MyBroadException emits a message, but catching and reraising it doesn't.

@Jackenmen
Copy link
Contributor Author

I'm concerned about the changes shown in the primer. Most of the patterns there look benign, where a broad exception is caught, and then some logging/cleanup is done, and then the original error is re-raised. I didn't think we had consensus on changing that (or at least, I don't see any discussion in the issue.)

Yeah, there was definitely no consensus on this change. The primer workflow didn't manage to run successfully on this PR 3 days ago when I added inference to broad-exception-raised so I wasn't really aware of these cases.

I think the status quo should be preserved where directly raising MyBroadException emits a message, but catching and reraising it doesn't.

I agree, I've done some changes that should hopefully address this.

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 the quick updates! (And sorry the primer was down.)

@github-actions

This comment has been minimized.

@Jackenmen
Copy link
Contributor Author

All good, I'm happy you/primer caught it before this PR was merged :)

DanielNoord
DanielNoord previously approved these changes Nov 13, 2022
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 a lot @jack1142, especially for revisiting and fixing the conflicts after we forgot to merge this.

Much appreciated!

@DanielNoord DanielNoord dismissed stale reviews from jacobtylerwalls and themself via b480242 November 13, 2022 11:26
@DanielNoord DanielNoord enabled auto-merge (squash) November 13, 2022 11:27
@DanielNoord DanielNoord merged commit ac65bdc into pylint-dev:main Nov 13, 2022
@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 b480242

@Jackenmen Jackenmen deleted the issue_7495 branch November 13, 2022 12:26
@Pierre-Sassoulas
Copy link
Member

@DanielNoord please cleanup the commit message when squashing, it does not have to be super fancy and a perefect synthesis but this one was just a mess πŸ˜„

Use qualified name when checking for overgeneral exceptions (https://github.com/PyCQA/pylint/pull/7497)

* Update tests

* Use qualified name when checking for overgeneral exceptions

* WIP: Add deprecation warning

* Add changelog fragment

* Use qualified name in test case

* spell check fix

* Update changelog fragment with suggested fixes

Co-authored-by: DaniΓ«l van Noord <13665637+DanielNoord@users.noreply.github.com>

* Move OVERGENERAL_EXCEPTIONS directly to the default value in dict

* Mark as TODO for pylint 3.0

* Properly warn on each occurrence of name without dots

* Update the warning per the review

* Rephrase the warning to mention pylint 3.0

* Remove unnecessary nesting of the if condition

* Quote the exception name in deprecation warning

* Use config value for overgeneral exceptions in broad-exception-raised

* Infer qualified name of the exception in broad-exception-raised

* e.g. -> maybe?

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>

* Suppress missing class docstrings

* Add few more tests for broad-exception-raised

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>

* Fix unexpected missing-raise-from

* Revert "Fix unexpected missing-raise-from"

This reverts commit https://github.com/PyCQA/pylint/commit/d796e72035b7f7578b9e6bb1e45a30935e80b009.

* Revert "Add few more tests for broad-exception-raised"

This reverts commit https://github.com/PyCQA/pylint/commit/e5a193ee136f8566d43450fbb9fbf28cc717d307.

* Change confidence of broad-exception-raised from HIGH to INFERENCE

* Only trigger broad-exception-raised for raise with new exc instance

* Update overgeneral-exceptions definition in example pylintrc file

* Update pylint/checkers/exceptions.py

Co-authored-by: DaniΓ«l van Noord <13665637+DanielNoord@users.noreply.github.com>
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>

@DanielNoord
Copy link
Collaborator

Shit, I thought I did. Sorry about that!

Comment on lines +302 to +311
warnings.warn_explicit(
"Specifying exception names in the overgeneral-exceptions option"
" without module name is deprecated and support for it"
" will be removed in pylint 3.0."
f" Use fully qualified name (maybe 'builtins.{exc_name}' ?) instead.",
category=UserWarning,
filename="pylint: Command line or configuration file",
lineno=1,
module="pylint",
)
Copy link
Member

@cdce8p cdce8p Nov 16, 2022

Choose a reason for hiding this comment

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

With --jobs 2, the open() call is executed for each opened file. E.g. for HomeAssistant I got over 16.000 warnings just for 3 old config values. That should be fixed before we release 2.16.
https://github.com/cdce8p/ha-core/actions/runs/3470026809/jobs/5797737991#step:8:25

pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.BaseException' ?) instead.
pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.Exception' ?) instead.
pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.HomeAssistantError' ?) instead.
pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.BaseException' ?) instead.
pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.Exception' ?) instead.
pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.HomeAssistantError' ?) instead.
pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.BaseException' ?) instead.
pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.Exception' ?) instead.
pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.HomeAssistantError' ?) instead.
pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.BaseException' ?) instead.
...

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

Successfully merging this pull request may close these issues.

overgeneral-exceptions doesn't work for exceptions outside builtins
6 participants