Skip to content

fix: 🐛 Fix ValueError when using Flag #2759

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

Merged
merged 7 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2739](https://github.com/Pycord-Development/pycord/pull/2739))
- Fixed missing `None` type hints in `Select.__init__`.
([#2746](https://github.com/Pycord-Development/pycord/pull/2746))
- Fixed `TypeError` when using `Flag` with Python 3.11+.
([#2759](https://github.com/Pycord-Development/pycord/pull/2759))
- Fixed `TypeError` when specifying `thread_name` in `Webhook.send`.
([#2761](https://github.com/Pycord-Development/pycord/pull/2761))
- Updated `valid_locales` to support `in` and `es-419`.
Expand Down
27 changes: 15 additions & 12 deletions discord/ext/commands/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Iterator, Literal, Pattern, TypeVar, Union

from discord.utils import MISSING, MissingField, maybe_coroutine, resolve_annotation

if sys.version_info >= (3, 11):
_MISSING = MissingField
else:
_MISSING = MISSING
from discord.utils import (
MISSING,
maybe_coroutine,
resolve_annotation,
)

from .converter import run_converters
from .errors import (
Expand All @@ -59,6 +58,10 @@
from .context import Context


def _missing_field_factory() -> field:
return field(default_factory=lambda: MISSING)


@dataclass
class Flag:
"""Represents a flag parameter for :class:`FlagConverter`.
Expand Down Expand Up @@ -86,13 +89,13 @@ class Flag:
Whether multiple given values overrides the previous value.
"""

name: str = _MISSING
name: str = _missing_field_factory()
aliases: list[str] = field(default_factory=list)
attribute: str = _MISSING
annotation: Any = _MISSING
default: Any = _MISSING
max_args: int = _MISSING
override: bool = _MISSING
attribute: str = _missing_field_factory()
annotation: Any = _missing_field_factory()
default: Any = _missing_field_factory()
max_args: int = _missing_field_factory()
override: bool = _missing_field_factory()
cast_to_dict: bool = False

@property
Expand Down
6 changes: 0 additions & 6 deletions discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import warnings
from base64 import b64encode
from bisect import bisect_left
from dataclasses import field
from inspect import isawaitable as _isawaitable
from inspect import signature as _signature
from operator import attrgetter
Expand Down Expand Up @@ -115,11 +114,6 @@ def __repr__(self) -> str:


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


class _cached_property:
Expand Down