From 1c312a158a585208840b220e7123ae18be85bec9 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sat, 24 Apr 2021 08:53:36 -0400 Subject: [PATCH] [commands] Add FlagConverter.__iter__ --- discord/ext/commands/flags.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/discord/ext/commands/flags.py b/discord/ext/commands/flags.py index 14ebb774..68a02911 100644 --- a/discord/ext/commands/flags.py +++ b/discord/ext/commands/flags.py @@ -40,6 +40,7 @@ from discord.utils import maybe_coroutine from dataclasses import dataclass, field from typing import ( Dict, + Iterator, Optional, Pattern, Set, @@ -443,6 +444,14 @@ class FlagConverter(metaclass=FlagsMeta): how this converter works, check the appropriate :ref:`documentation `. + .. container:: operations + + .. describe:: iter(x) + + Returns an iterator of ``(flag_name, flag_value)`` pairs. This allows it + to be, for example, constructed as a dict or a list of pairs. + Note that aliases are not shown. + .. versionadded:: 2.0 Parameters @@ -468,6 +477,10 @@ class FlagConverter(metaclass=FlagsMeta): def _can_be_constructible(cls) -> bool: return all(not flag.required for flag in cls.__commands_flags__.values()) + def __iter__(self) -> Iterator[Tuple[str, Any]]: + for flag in self.__class__.__commands_flags__.values(): + yield (flag.name, getattr(self, flag.attribute)) + @classmethod async def _construct_default(cls: Type[F], ctx: Context) -> F: self: F = cls.__new__(cls)