MCPcopy Create free account
hub / github.com/ipython/traitlets / Enum

Class Enum

traitlets/traitlets.py:3188–3274  ·  view source on GitHub ↗

An enum whose value must be in a given sequence.

Source from the content-addressed store, hash-verified

3186
3187
3188class Enum(TraitType[G, G]):
3189 """An enum whose value must be in a given sequence."""
3190
3191 if t.TYPE_CHECKING:
3192
3193 @t.overload
3194 def __init__(
3195 self: Enum[G],
3196 values: t.Sequence[G],
3197 default_value: G | Sentinel = ...,
3198 allow_none: Literal[False] = ...,
3199 read_only: bool | None = ...,
3200 help: str | None = ...,
3201 config: t.Any = ...,
3202 **kwargs: t.Any,
3203 ) -> None:
3204 ...
3205
3206 @t.overload
3207 def __init__(
3208 self: Enum[G | None],
3209 values: t.Sequence[G] | None,
3210 default_value: G | Sentinel | None = ...,
3211 allow_none: Literal[True] = ...,
3212 read_only: bool | None = ...,
3213 help: str | None = ...,
3214 config: t.Any = ...,
3215 **kwargs: t.Any,
3216 ) -> None:
3217 ...
3218
3219 def __init__(
3220 self: Enum[G],
3221 values: t.Sequence[G] | None,
3222 default_value: G | Sentinel | None = Undefined,
3223 allow_none: bool = False,
3224 read_only: bool | None = None,
3225 help: str | None = None,
3226 config: t.Any = None,
3227 **kwargs: t.Any,
3228 ) -> None:
3229 self.values = values
3230 if allow_none is True and default_value is Undefined:
3231 default_value = None
3232 kwargs["allow_none"] = allow_none
3233 kwargs["read_only"] = read_only
3234 kwargs["help"] = help
3235 kwargs["config"] = config
3236 super().__init__(default_value, **kwargs)
3237
3238 def validate(self, obj: t.Any, value: t.Any) -> G:
3239 if self.values and value in self.values:
3240 return value # type:ignore[no-any-return]
3241 self.error(obj, value)
3242
3243 def _choices_str(self, as_rst: bool = False) -> str:
3244 """Returns a description of the trait choices (not none)."""
3245 choices = self.values or []

Callers 11

ParityClass · 0.90
CClass · 0.90
TClass · 0.90
mypy_enum_typingFunction · 0.90
ExampleClass · 0.90
MyConfClass · 0.90
MyConf2Class · 0.90
ApplicationClass · 0.90
EnvironPrinterClass · 0.90
PrintHelloClass · 0.90
FooClass · 0.90

Calls

no outgoing calls

Tested by 1

mypy_enum_typingFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…