4Runtime value types stored in the constant pool.
7from __future__
import annotations
9from dataclasses
import dataclass
10from enum
import IntEnum
11from typing
import Union
15 """Type of a constant-pool :class:`Value`."""
22_Payload = Union[
None, bool, int, float, str]
28 A typed constant-pool entry.
30 Use the class-method constructors rather than setting ``type`` /
36 Value.from_float(3.14)
37 Value.from_string("hello")
41 _data: _Payload =
None
45 """Return a new Null-typed value."""
46 return cls(ValueType.Null,
None)
50 """Return a new Bool-typed value wrapping *v*."""
51 return cls(ValueType.Bool, bool(v))
55 """Return a new Int-typed value wrapping *v*."""
56 return cls(ValueType.Int, int(v))
60 """Return a new Float-typed value wrapping *v*."""
61 return cls(ValueType.Float, float(v))
65 """Return a new String-typed value wrapping *v*."""
66 return cls(ValueType.String, str(v))
69 """Return the payload as a Python ``bool``, raising ``TypeError`` if the type is not Bool."""
70 if self.
type != ValueType.Bool:
71 raise TypeError(f
"Value is {self.type.name}, not Bool")
72 return bool(self.
_data)
75 """Return the payload as a Python ``int``, raising ``TypeError`` if the type is not Int."""
76 if self.
type != ValueType.Int:
77 raise TypeError(f
"Value is {self.type.name}, not Int")
78 return int(self.
_data)
81 """Return the payload as a Python ``float``, raising ``TypeError`` if the type is not Float."""
82 if self.
type != ValueType.Float:
83 raise TypeError(f
"Value is {self.type.name}, not Float")
84 return float(self.
_data)
87 """Return the payload as a Python ``str``, raising ``TypeError`` if the type is not String."""
88 if self.
type != ValueType.String:
89 raise TypeError(f
"Value is {self.type.name}, not String")
90 return str(self.
_data)
93 """Return a concise debug representation showing the type and payload."""
94 if self.
type == ValueType.Null:
96 return f
"Value({self.type.name}, {self._data!r})"
98 def __eq__(self, other: object) -> bool:
99 """Return ``True`` if *other* is a :class:`Value` with identical type and payload."""
100 if not isinstance(other, Value):
101 return NotImplemented
102 return self.
type == other.type
and self.
_data == other._data
bool __eq__(self, object other)
"Value" from_int(cls, int v)
"Value" from_bool(cls, bool v)
"Value" from_string(cls, str v)
"Value" from_float(cls, float v)