Skip to content

Comments

fix(converter): allow Greedy[...] to accept union types#1482

Open
devbutlazy wants to merge 2 commits intoDisnakeDev:masterfrom
devbutlazy:master
Open

fix(converter): allow Greedy[...] to accept union types#1482
devbutlazy wants to merge 2 commits intoDisnakeDev:masterfrom
devbutlazy:master

Conversation

@devbutlazy
Copy link

Summary

Fix #1421 => commands.Greedy[...] to correctly support Python 3.10+ union types (PEP 604 A | B) and typing.Union.
Previously, passing a union type like commands.Greedy[disnake.Member | disnake.Object] would raise:

TypeError: Greedy[...] expects a type or a Converter instance

This PR updates Greedy.__class_getitem__ to:

  • Detect union types using typing.get_origin and typing.get_args
  • Allow union types as valid converters
  • Preserve existing restrictions: Greedy[str] and Greedy[None]; Greedy[Optional[T]]; Greedy[Greedy[T]]
  • Use getattr(converter, "__name__", repr(converter)) for safe error messages

Now, Greedy[disnake.Member | disnake.Object] works as expected, allowing commands to greedily consume multiple valid converter types.

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, ...)

@shiftinv shiftinv added this to the disnake v2.12 milestone Nov 23, 2025
Copy link
Member

@shiftinv shiftinv left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! Just two small comments; for the most part this looks good to me, and seems to work as expected.


if origin is Union and type(None) in args:
msg = f"Greedy[{converter!r}] is invalid."
if origin in (Union, types.UnionType) and any(arg is type(None) for arg in args):
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason to change the latter part of this from type(None) in args to any(...)?

@@ -0,0 +1 @@
Fix Greedy[...] to accept union types (e.g., Member | Object) in commands.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Fix Greedy[...] to accept union types (e.g., Member | Object) in commands.
Fix ``Greedy[...]`` to accept union types (e.g. ``Member | Object``) in commands.

just a minor nit

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.10 uniontype is not supported within commands.Greedy

2 participants