-
Notifications
You must be signed in to change notification settings - Fork 147
fix: warn_deprecated now respect warnings.filter ignores #1476
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
a645c93
cf77a2d
d9bcc21
d6ec2f8
1b41152
e936d99
f67ee9a
c415bd1
0ff68b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| warn_deprecated now respected warning.filter ignores if at least "disnake" is passed to module argument. | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -301,12 +301,27 @@ def warn_deprecated( | |||||||||||||||||||||||||||||||||||||||
| stacklevel = 1 # reset stacklevel, assume we just want the first frame outside library code | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| old_filters = warnings.filters[:] | ||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||
| warnings.simplefilter("always", DeprecationWarning) | ||||||||||||||||||||||||||||||||||||||||
| warnings.warn(*args, stacklevel=stacklevel + 1, category=DeprecationWarning, **kwargs) | ||||||||||||||||||||||||||||||||||||||||
| finally: | ||||||||||||||||||||||||||||||||||||||||
| assert isinstance(warnings.filters, list) | ||||||||||||||||||||||||||||||||||||||||
| warnings.filters[:] = old_filters | ||||||||||||||||||||||||||||||||||||||||
| warnings.filterwarnings(action="default", category=DeprecationWarning, module="cumbum") | ||||||||||||||||||||||||||||||||||||||||
hularuns marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||
| send_warning = True | ||||||||||||||||||||||||||||||||||||||||
| if len(old_filters) > 0: | ||||||||||||||||||||||||||||||||||||||||
| for action, _, category, module, _ in old_filters: | ||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||
| (category is DeprecationWarning) | ||||||||||||||||||||||||||||||||||||||||
| and ("disnake" in str(module)) | ||||||||||||||||||||||||||||||||||||||||
| and (action == "ignore") | ||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||
| send_warning = False | ||||||||||||||||||||||||||||||||||||||||
| break # break out after first disnake rule, it's good enough. | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if send_warning: | ||||||||||||||||||||||||||||||||||||||||
| # this still allows force bypassing of filters if the default DeprecationWarning ignore is set. | ||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||
| warnings.simplefilter(action="always", category=DeprecationWarning) | ||||||||||||||||||||||||||||||||||||||||
| warnings.warn(*args, stacklevel=stacklevel + 1, category=DeprecationWarning, **kwargs) | ||||||||||||||||||||||||||||||||||||||||
| finally: | ||||||||||||||||||||||||||||||||||||||||
| # NOTE: Is this assertion even necessary? warnings.filters is always at minimum an empty list? | ||||||||||||||||||||||||||||||||||||||||
| assert isinstance(warnings.filters, list) | ||||||||||||||||||||||||||||||||||||||||
| warnings.filters[:] = old_filters | ||||||||||||||||||||||||||||||||||||||||
hularuns marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||
| if send_warning: | |
| # this still allows force bypassing of filters if the default DeprecationWarning ignore is set. | |
| try: | |
| warnings.simplefilter(action="always", category=DeprecationWarning) | |
| warnings.warn(*args, stacklevel=stacklevel + 1, category=DeprecationWarning, **kwargs) | |
| finally: | |
| # NOTE: Is this assertion even necessary? warnings.filters is always at minimum an empty list? | |
| assert isinstance(warnings.filters, list) | |
| warnings.filters[:] = old_filters | |
| # allow force bypassing of filters if the default DeprecationWarning ignore is set. | |
| if not send_warning: | |
| return | |
| try: | |
| warnings.simplefilter(action="always", category=DeprecationWarning) | |
| warnings.warn(*args, stacklevel=stacklevel + 1, category=DeprecationWarning, **kwargs) | |
| finally: | |
| assert isinstance(warnings.filters, list) | |
| warnings.filters[:] = old_filters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree on the guard clause instead and committed your suggestion. Just thought, after committing we could instead return within the for loop of old_filters instead of assigning send_warning a value and breaking from the loop. Personally, I don't like that as it's not as explicit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bool variable can be avoided by using a for ... else clause, while still keeping it flat.
for ... in ...:
if condition:
break
else:
return
# here goes stuff that should run when condition is trueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of the early break, i've settled for doing an early return here instead
Uh oh!
There was an error while loading. Please reload this page.