MCPcopy Index your code
hub / github.com/python/mypy / LiteralType

Class LiteralType

mypy/types.py:3189–3303  ·  view source on GitHub ↗

The type of a Literal instance. Literal[Value] A Literal always consists of: 1. A native Python object corresponding to the contained inner value 2. A fallback for this Literal. The fallback also corresponds to the parent type this Literal subtypes. For example, 'Literal[42

Source from the content-addressed store, hash-verified

3187
3188
3189class LiteralType(ProperType):
3190 """The type of a Literal instance. Literal[Value]
3191
3192 A Literal always consists of:
3193
3194 1. A native Python object corresponding to the contained inner value
3195 2. A fallback for this Literal. The fallback also corresponds to the
3196 parent type this Literal subtypes.
3197
3198 For example, 'Literal[42]' is represented as
3199 'LiteralType(value=42, fallback=instance_of_int)'
3200
3201 As another example, `Literal[Color.RED]` (where Color is an enum) is
3202 represented as `LiteralType(value="RED", fallback=instance_of_color)'.
3203 """
3204
3205 __slots__ = ("value", "fallback", "_hash")
3206
3207 def __init__(
3208 self, value: LiteralValue, fallback: Instance, line: int = -1, column: int = -1
3209 ) -> None:
3210 super().__init__(line, column)
3211 self.value = value
3212 self.fallback = fallback
3213
3214 # NOTE: Enum types are always truthy by default, but this can be changed
3215 # in subclasses, so we need to get the truthyness from the Enum
3216 # type rather than base it on the value (which is a non-empty
3217 # string for enums, so always truthy)
3218 # TODO: We should consider moving this branch to the `can_be_true`
3219 # `can_be_false` properties instead, so the truthyness only
3220 # needs to be determined once per set of Enum literals.
3221 # However, the same can be said for `TypeAliasType` in some
3222 # cases and we only set the default based on the type it is
3223 # aliasing. So if we decide to change this, we may want to
3224 # change that as well. perf_compare output was inconclusive
3225 # but slightly favored this version, probably because we have
3226 # almost no test cases where we would redundantly compute
3227 # `can_be_false`/`can_be_true`.
3228 def can_be_false_default(self) -> bool:
3229 if self.fallback.type.is_enum:
3230 return self.fallback.can_be_false
3231 return not self.value
3232
3233 def can_be_true_default(self) -> bool:
3234 if self.fallback.type.is_enum:
3235 return self.fallback.can_be_true
3236 return bool(self.value)
3237
3238 def accept(self, visitor: TypeVisitor[T]) -> T:
3239 return visitor.visit_literal_type(self)
3240
3241 def __hash__(self) -> int:
3242 # Intentionally a subset of __eq__ for perf
3243 return hash(self.value)
3244
3245 def __eq__(self, other: object) -> bool:
3246 if isinstance(other, LiteralType):

Callers 15

analyze_varFunction · 0.90
false_onlyFunction · 0.90
coerce_to_literalFunction · 0.90
analyze_literal_paramMethod · 0.90
visit_literal_typeMethod · 0.90

Calls

no outgoing calls

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…