Skip to content

Commit bb61cf7

Browse files
authored
fix: option and BridgeOption adjustments (Pycord-Development#2417)
1 parent 8cec636 commit bb61cf7

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ These changes are available on the `master` branch, but have not yet been releas
1616
([#2396](https://github.com/Pycord-Development/pycord/pull/2396))
1717
- Added `user` argument to `Paginator.edit`.
1818
([#2390](https://github.com/Pycord-Development/pycord/pull/2390))
19+
- Added `bridge_option` decorator. Required for `bridge.Bot` in 2.7.
20+
([#2417](https://github.com/Pycord-Development/pycord/pull/2417))
1921

2022
### Fixed
2123

@@ -34,13 +36,19 @@ These changes are available on the `master` branch, but have not yet been releas
3436
([#2407](https://github.com/Pycord-Development/pycord/pull/2407))
3537
- Fixed invalid data being passed to `Interaction._guild` in certain cases.
3638
([#2411](https://github.com/Pycord-Development/pycord/pull/2411))
39+
- Fixed option typehints being ignored when using `parameter_name`.
40+
([#2417](https://github.com/Pycord-Development/pycord/pull/2417))
3741

3842
### Changed
3943

4044
- Changed the type of `Guild.bitrate_limit` to `int`.
4145
([#2387](https://github.com/Pycord-Development/pycord/pull/2387))
4246
- HTTP requests that fail with a 503 status are now re-tried.
4347
([#2395](https://github.com/Pycord-Development/pycord/pull/2395))
48+
- `option` decorator now accepts `input_type`.
49+
([#2417](https://github.com/Pycord-Development/pycord/pull/2417))
50+
- `Option` may be used instead of `BridgeOption` until 2.7.
51+
([#2417](https://github.com/Pycord-Development/pycord/pull/2417))
4452

4553
## [2.5.0] - 2024-03-02
4654

discord/commands/options.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def to_dict(self) -> dict[str, str | int | float]:
395395
return as_dict
396396

397397

398-
def option(name, type=None, **kwargs):
398+
def option(name, input_type=None, **kwargs):
399399
"""A decorator that can be used instead of typehinting :class:`.Option`.
400400
401401
.. versionadded:: 2.0
@@ -408,12 +408,13 @@ def option(name, type=None, **kwargs):
408408
"""
409409

410410
def decorator(func):
411-
nonlocal type
412-
type = type or func.__annotations__.get(name, str)
413-
if parameter := kwargs.get("parameter_name"):
414-
func.__annotations__[parameter] = Option(type, name=name, **kwargs)
415-
else:
416-
func.__annotations__[name] = Option(type, **kwargs)
411+
resolved_name = kwargs.pop("parameter_name", None) or name
412+
itype = (
413+
kwargs.pop("type", None)
414+
or input_type
415+
or func.__annotations__.get(resolved_name, str)
416+
)
417+
func.__annotations__[resolved_name] = Option(itype, name=name, **kwargs)
417418
return func
418419

419420
return decorator

discord/ext/bridge/core.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
SlashCommandOptionType,
4141
)
4242

43-
from ...utils import MISSING, find, get
43+
from ...utils import MISSING, find, get, warn_deprecated
4444
from ..commands import BadArgument
4545
from ..commands import Bot as ExtBot
4646
from ..commands import (
@@ -63,6 +63,7 @@
6363
"BridgeCommandGroup",
6464
"bridge_command",
6565
"bridge_group",
66+
"bridge_option",
6667
"BridgeExtCommand",
6768
"BridgeSlashCommand",
6869
"BridgeExtGroup",
@@ -627,3 +628,38 @@ async def convert(self, ctx, argument: str) -> Any:
627628
return converted
628629
except ValueError as exc:
629630
raise BadArgument() from exc
631+
632+
633+
def bridge_option(name, input_type=None, **kwargs):
634+
"""A decorator that can be used instead of typehinting :class:`.BridgeOption`.
635+
636+
.. versionadded:: 2.6
637+
638+
Attributes
639+
----------
640+
parameter_name: :class:`str`
641+
The name of the target parameter this option is mapped to.
642+
This allows you to have a separate UI ``name`` and parameter name.
643+
"""
644+
645+
def decorator(func):
646+
resolved_name = kwargs.pop("parameter_name", None) or name
647+
itype = (
648+
kwargs.pop("type", None)
649+
or input_type
650+
or func.__annotations__.get(resolved_name, str)
651+
)
652+
func.__annotations__[resolved_name] = BridgeOption(itype, name=name, **kwargs)
653+
return func
654+
655+
return decorator
656+
657+
658+
discord.commands.options.Option = BridgeOption
659+
discord.Option = BridgeOption
660+
warn_deprecated(
661+
"Option",
662+
"BridgeOption",
663+
"2.5",
664+
reference="https://github.com/Pycord-Development/pycord/pull/2417",
665+
)

0 commit comments

Comments
 (0)