MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / parse_user_argument_for_enum

Function parse_user_argument_for_enum

lib/sqlalchemy/util/langhelpers.py:1784–1815  ·  view source on GitHub ↗

Given a user parameter, parse the parameter into a chosen value from a list of choice objects, typically Enum values. The user argument can be a string name that matches the name of a symbol, or the symbol object itself, or any number of alternate choices such as True/False/ None et

(
    arg: Any,
    choices: Dict[_E, List[Any]],
    name: str,
    resolve_symbol_names: bool = False,
)

Source from the content-addressed store, hash-verified

1782
1783
1784def parse_user_argument_for_enum(
1785 arg: Any,
1786 choices: Dict[_E, List[Any]],
1787 name: str,
1788 resolve_symbol_names: bool = False,
1789) -> Optional[_E]:
1790 """Given a user parameter, parse the parameter into a chosen value
1791 from a list of choice objects, typically Enum values.
1792
1793 The user argument can be a string name that matches the name of a
1794 symbol, or the symbol object itself, or any number of alternate choices
1795 such as True/False/ None etc.
1796
1797 :param arg: the user argument.
1798 :param choices: dictionary of enum values to lists of possible
1799 entries for each.
1800 :param name: name of the argument. Used in an :class:`.ArgumentError`
1801 that is raised if the parameter doesn't match any available argument.
1802
1803 """
1804 for enum_value, choice in choices.items():
1805 if arg is enum_value:
1806 return enum_value
1807 elif resolve_symbol_names and arg == enum_value.name:
1808 return enum_value
1809 elif arg in choice:
1810 return enum_value
1811
1812 if arg is None:
1813 return None
1814
1815 raise exc.ArgumentError(f"Invalid value for '{name}': {arg!r}")
1816
1817
1818_creation_order = 1

Callers 1

__init__Method · 0.85

Calls 1

itemsMethod · 0.45

Tested by

no test coverage detected