Skip to content

Examples for bad-exception-context#6026

Merged
DanielNoord merged 2 commits intopylint-dev:mainfrom
DudeNr33:doc-bad-exception-context
Mar 29, 2022
Merged

Examples for bad-exception-context#6026
DanielNoord merged 2 commits intopylint-dev:mainfrom
DudeNr33:doc-bad-exception-context

Conversation

@DudeNr33
Copy link
Copy Markdown
Collaborator

Co-authored-by: Vladyslav Krylasov vladyslav.krylasov@gmail.com

  • Write a good description on what the PR does.
  • If you used multiple emails or multiple names when contributing, add your mails
    and preferred name in script/.contributors_aliases.json

Type of Changes

Type
🐛 Bug fix
✨ New feature
🔨 Refactoring
📜 Docs

Description

Examples and related links for bad-exception-context.

Ref: #5953

Co-authored-by: Vladyslav Krylasov <vladyslav.krylasov@gmail.com>
@DudeNr33 DudeNr33 added this to the 2.14.0 milestone Mar 29, 2022
Copy link
Copy Markdown
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.

Nitpick to not use an Exception in the re-raise feel free to ignore.

Comment thread doc/data/messages/b/bad-exception-context/bad.py Outdated
Comment thread doc/data/messages/b/bad-exception-context/good.py Outdated
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
@DanielNoord DanielNoord merged commit e41b2f0 into pylint-dev:main Mar 29, 2022
Comment on lines +1 to +7
def divide(x, y):
result = 0
try:
result = x / y
except ZeroDivisionError:
raise ValueError(f"Division by zero when dividing {x} by {y} !") from result # [bad-exception-context]
return result
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
def divide(x, y):
result = 0
try:
result = x / y
except ZeroDivisionError:
raise ValueError(f"Division by zero when dividing {x} by {y} !") from result # [bad-exception-context]
return result
class YParameterIsZero(ZeroDivisionError):
...
def divide(x, y):
result = 0
try:
result = x / y
except ZeroDivisionError:
raise YParameterIsZero(f"Division by zero when dividing {x} by {y} !") from result # [bad-exception-context]
return result

Comment on lines +1 to +7
def divide(x, y):
result = 0
try:
result = x / y
except ZeroDivisionError as exc:
raise ValueError(f"Division by zero when dividing {x} by {y} !") from exc
return result
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
def divide(x, y):
result = 0
try:
result = x / y
except ZeroDivisionError as exc:
raise ValueError(f"Division by zero when dividing {x} by {y} !") from exc
return result
class YParameterIsZero(ZeroDivisionError):
...
def divide(x, y):
result = 0
try:
result = x / y
except ZeroDivisionError as exc:
raise YParameterIsZero(f"Division by zero when dividing {x} by {y} !") from exc
return result

@Pierre-Sassoulas
Copy link
Copy Markdown
Member

Nevermind the suggestion it's not that important :)

@DanielNoord
Copy link
Copy Markdown
Collaborator

Oops, sorry guys! Little too trigger happy 😅

@DudeNr33
Copy link
Copy Markdown
Collaborator Author

😀 No problem, while I think Pierre's last suggestion would have been the best solution, I guess it should be already good enough for the sake of documentation.

@Pierre-Sassoulas Pierre-Sassoulas removed this from the 2.14.0 milestone May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants