Skip to content

Commit ad3d2a8

Browse files
fix: 🐛 Fix ValueError when using Flag (#2759)
* 🐛 Fix `dataclasses.field` can't be reused * 📝 CHANGELOG.md * Update CHANGELOG.md Signed-off-by: Dorukyum <[email protected]> * ♻️ Move `_missing_field_factory` to flags.py and remove incorrect comment --------- Signed-off-by: Paillat <[email protected]> Signed-off-by: Dorukyum <[email protected]> Co-authored-by: Dorukyum <[email protected]>
1 parent 5eaebfb commit ad3d2a8

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ These changes are available on the `master` branch, but have not yet been releas
107107
([#2739](https://github.com/Pycord-Development/pycord/pull/2739))
108108
- Fixed missing `None` type hints in `Select.__init__`.
109109
([#2746](https://github.com/Pycord-Development/pycord/pull/2746))
110+
- Fixed `TypeError` when using `Flag` with Python 3.11+.
111+
([#2759](https://github.com/Pycord-Development/pycord/pull/2759))
110112
- Fixed `TypeError` when specifying `thread_name` in `Webhook.send`.
111113
([#2761](https://github.com/Pycord-Development/pycord/pull/2761))
112114
- Updated `valid_locales` to support `in` and `es-419`.

discord/ext/commands/flags.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@
3131
from dataclasses import dataclass, field
3232
from typing import TYPE_CHECKING, Any, Iterator, Literal, Pattern, TypeVar, Union
3333

34-
from discord.utils import MISSING, MissingField, maybe_coroutine, resolve_annotation
35-
36-
if sys.version_info >= (3, 11):
37-
_MISSING = MissingField
38-
else:
39-
_MISSING = MISSING
34+
from discord.utils import (
35+
MISSING,
36+
maybe_coroutine,
37+
resolve_annotation,
38+
)
4039

4140
from .converter import run_converters
4241
from .errors import (
@@ -59,6 +58,10 @@
5958
from .context import Context
6059

6160

61+
def _missing_field_factory() -> field:
62+
return field(default_factory=lambda: MISSING)
63+
64+
6265
@dataclass
6366
class Flag:
6467
"""Represents a flag parameter for :class:`FlagConverter`.
@@ -86,13 +89,13 @@ class Flag:
8689
Whether multiple given values overrides the previous value.
8790
"""
8891

89-
name: str = _MISSING
92+
name: str = _missing_field_factory()
9093
aliases: list[str] = field(default_factory=list)
91-
attribute: str = _MISSING
92-
annotation: Any = _MISSING
93-
default: Any = _MISSING
94-
max_args: int = _MISSING
95-
override: bool = _MISSING
94+
attribute: str = _missing_field_factory()
95+
annotation: Any = _missing_field_factory()
96+
default: Any = _missing_field_factory()
97+
max_args: int = _missing_field_factory()
98+
override: bool = _missing_field_factory()
9699
cast_to_dict: bool = False
97100

98101
@property

discord/utils.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import warnings
4040
from base64 import b64encode
4141
from bisect import bisect_left
42-
from dataclasses import field
4342
from inspect import isawaitable as _isawaitable
4443
from inspect import signature as _signature
4544
from operator import attrgetter
@@ -115,11 +114,6 @@ def __repr__(self) -> str:
115114

116115

117116
MISSING: Any = _MissingSentinel()
118-
# As of 3.11, directly setting a dataclass field to MISSING causes a ValueError. Using
119-
# field(default=MISSING) produces the same error, but passing a lambda to
120-
# default_factory produces the same behavior as default=MISSING and does not raise an
121-
# error.
122-
MissingField = field(default_factory=lambda: MISSING)
123117

124118

125119
class _cached_property:

0 commit comments

Comments
 (0)