Skip to content

Conversation

@hularuns
Copy link

@hularuns hularuns commented Nov 14, 2025

Summary

Closes #1475

warn_deprecated now respect if a user wants to ignore deprecation warnings by checking if the module name has disnake in it.

test_deprecated_warn tests have been added.

Expected use will be:

import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning, module = "disnake")

Using simplefilter or not passing a module value will result in the existing behaviour now which forces through the warning, as such this change will not impact current users unless they use the above example, or similar.

warning filters are generally not that large, so there is expected to be a very negligible impact to performance.

Known potential issues with this approach

  • This only currently respects ignore action value for the warnings.
  • literally any module string which has disnake in it will be picked up to be ignored if the action is ignore

Checklist

  • If code changes were made, then they have been tested
    • I have updated the documentation to reflect the changes
    • I have formatted the code properly by running uv run nox -s lint
    • I have type-checked the code by running uv run nox -s pyright
  • This PR fixes an issue
  • This PR adds something new (e.g. new method or parameters)
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

@read-the-docs-community
Copy link

read-the-docs-community bot commented Nov 14, 2025

Documentation build overview

📚 disnake | 🛠️ Build #30332442 | 📁 Comparing 0ff68b3 against latest (b325cc4)


🔍 Preview build

Show files changed (48 files in total): 📝 48 modified | ➕ 0 added | ➖ 0 deleted
File Status
index.html 📝 modified
whats_new.html 📝 modified
api/abc.html 📝 modified
api/activities.html 📝 modified
api/app_commands.html 📝 modified
api/app_info.html 📝 modified
api/audit_logs.html 📝 modified
api/automod.html 📝 modified
api/channels.html 📝 modified
api/clients.html 📝 modified
api/components.html 📝 modified
api/emoji.html 📝 modified
api/entitlements.html 📝 modified
api/events.html 📝 modified
api/exceptions.html 📝 modified
api/guild_scheduled_events.html 📝 modified
api/guilds.html 📝 modified
api/integrations.html 📝 modified
api/interactions.html 📝 modified
api/invites.html 📝 modified
api/localization.html 📝 modified
api/members.html 📝 modified
api/messages.html 📝 modified
api/misc.html 📝 modified
api/permissions.html 📝 modified
api/roles.html 📝 modified
api/skus.html 📝 modified
api/soundboard.html 📝 modified
api/stage_instances.html 📝 modified
api/stickers.html 📝 modified
api/subscriptions.html 📝 modified
api/ui.html 📝 modified
api/users.html 📝 modified
api/utilities.html 📝 modified
api/voice.html 📝 modified
api/webhooks.html 📝 modified
api/widgets.html 📝 modified
ext/tasks/index.html 📝 modified
ext/commands/api/app_commands.html 📝 modified
ext/commands/api/bots.html 📝 modified
ext/commands/api/checks.html 📝 modified
ext/commands/api/cogs.html 📝 modified
ext/commands/api/context.html 📝 modified
ext/commands/api/converters.html 📝 modified
ext/commands/api/exceptions.html 📝 modified
ext/commands/api/help_commands.html 📝 modified
ext/commands/api/misc.html 📝 modified
ext/commands/api/prefix_commands.html 📝 modified

@hularuns hularuns changed the title warn_deprecated now respect if a user wants to ignore deprecation w… fix: warn_deprecated now respect warnings.filter ignores Nov 14, 2025
@hularuns
Copy link
Author

I'm not sure how the pyright check is failing

@Enegg
Copy link
Contributor

Enegg commented Nov 14, 2025

@onerandomusername pyright tests are borked

Copy link
Contributor

@Enegg Enegg 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 not sure we really need this hack to be in the lib in the first place, but since this is legacy behavior I'm not one to decide that

disnake/utils.py Outdated
Comment on lines 316 to 324
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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

Copy link
Author

@hularuns hularuns Nov 14, 2025

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

Copy link
Contributor

@Enegg Enegg Nov 15, 2025

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 true

Copy link
Author

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

hularuns and others added 5 commits November 14, 2025 23:14
Co-authored-by: Eneg <[email protected]>
Signed-off-by: Sam <[email protected]>
- updated tests to be more explanatory if they fail assertions
- also removed the cheeky change of old_filters >= and replaced with >
@shiftinv shiftinv added this to disnake Nov 17, 2025
@shiftinv shiftinv added this to the disnake v2.12 milestone Nov 17, 2025
@github-project-automation github-project-automation bot moved this to Todo in disnake Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Deprecation warning overriding user settings by forcing it on.

4 participants