Literals and variables¶
Scalar
¶
Bases: Query
Abstract base class for PromQL scalar values.
to_string
abstractmethod
¶
to_string(indent: str | int | None = None, _indent_level: int = 0, *, style: Style = DEFAULT_STYLE) -> str
Render this expression as a PromQL string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent
|
str | int | None
|
Enables multi-line output: an |
None
|
_indent_level
|
int
|
Current nesting depth; internal, leave at |
0
|
style
|
Style
|
Formatting options, see |
DEFAULT_STYLE
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
subquery
¶
subquery(range: SCALAR_TYPE, resolution: SCALAR_TYPE | None = None, *, offset: SCALAR_TYPE | None = None, at: AT_TYPE | None = None) -> Subquery
Evaluate this expression as a subquery: <expr>[range:resolution].
See Subquery; slice syntax expr[range:resolution]
is a shorthand for this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
range
|
SCALAR_TYPE
|
Window length; raw numbers are seconds ( |
required |
resolution
|
SCALAR_TYPE | None
|
Step between evaluations; |
None
|
offset
|
SCALAR_TYPE | None
|
Time shift; raw numbers are seconds. |
None
|
at
|
AT_TYPE | None
|
Evaluation time: a Unix timestamp, |
None
|
from_value
abstractmethod
classmethod
¶
from_value(value: SCALAR_TYPE) -> Scalar | Variable
Create a Scalar instance from a float or int value.
A Variable is passed through unchanged,
since its value is only known once Grafana interpolates it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
SCALAR_TYPE
|
Number to wrap; a |
required |
Float
¶
Float(value: float)
Bases: Scalar
A numeric scalar literal (e.g. 3.14, -2.5, 5).
The value is rendered as given: an int stays an integer (Float(5) →
5) and a float keeps its decimal point (Float(5.0) → 5.0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
Number to render; an |
required |
Examples:
>>> print(Float(5))
5
>>> print(Float(5.0))
5.0
>>> print(Float(-2.5))
-2.5
Raw numbers are wrapped automatically where a scalar is expected:
>>> up = InstantVector("up", [])
>>> print(up < Float(1))
up{} < 1
>>> print(up < 1)
up{} < 1
subquery
¶
subquery(range: SCALAR_TYPE, resolution: SCALAR_TYPE | None = None, *, offset: SCALAR_TYPE | None = None, at: AT_TYPE | None = None) -> Subquery
Evaluate this expression as a subquery: <expr>[range:resolution].
See Subquery; slice syntax expr[range:resolution]
is a shorthand for this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
range
|
SCALAR_TYPE
|
Window length; raw numbers are seconds ( |
required |
resolution
|
SCALAR_TYPE | None
|
Step between evaluations; |
None
|
offset
|
SCALAR_TYPE | None
|
Time shift; raw numbers are seconds. |
None
|
at
|
AT_TYPE | None
|
Evaluation time: a Unix timestamp, |
None
|
from_value
classmethod
¶
from_value(value: SCALAR_TYPE) -> Scalar | Variable
Wrap a raw number, passing Scalar and Variable through unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
SCALAR_TYPE
|
Number to wrap; a |
required |
to_string
¶
to_string(indent: str | int | None = None, _indent_level: int = 0, *, style: Style = DEFAULT_STYLE) -> str
Render this expression as a PromQL string.
Hex
¶
Hex(value: int)
Bases: Scalar
A hexadecimal integer scalar literal (e.g. 0xff, 0x8f).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
Integer to render in hexadecimal. |
required |
Examples:
>>> print(Hex(255))
0xff
>>> print(Hex(16))
0x10
subquery
¶
subquery(range: SCALAR_TYPE, resolution: SCALAR_TYPE | None = None, *, offset: SCALAR_TYPE | None = None, at: AT_TYPE | None = None) -> Subquery
Evaluate this expression as a subquery: <expr>[range:resolution].
See Subquery; slice syntax expr[range:resolution]
is a shorthand for this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
range
|
SCALAR_TYPE
|
Window length; raw numbers are seconds ( |
required |
resolution
|
SCALAR_TYPE | None
|
Step between evaluations; |
None
|
offset
|
SCALAR_TYPE | None
|
Time shift; raw numbers are seconds. |
None
|
at
|
AT_TYPE | None
|
Evaluation time: a Unix timestamp, |
None
|
from_value
classmethod
¶
from_value(value: SCALAR_TYPE) -> Scalar | Variable
Wrap a raw number, passing Scalar and Variable through unchanged.
Whole floats are accepted (3.0 → 0x3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
SCALAR_TYPE
|
Whole number to wrap; a |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
to_string
¶
to_string(indent: str | int | None = None, _indent_level: int = 0, *, style: Style = DEFAULT_STYLE) -> str
Render this expression as a PromQL string.
DurationParams
¶
Bases: TypedDict
Keyword arguments accepted by Duration.
Every field defaults to 0/False.
Attributes:
| Name | Type | Description |
|---|---|---|
y |
int
|
Years (always 365 days). |
w |
int
|
Weeks (always 7 days). |
d |
int
|
Days (always 24 hours). |
h |
int
|
Hours. |
m |
int
|
Minutes. |
s |
int
|
Seconds. |
ms |
int
|
Milliseconds. |
neg |
bool
|
Negate the duration, e.g. for a negative |
Duration
¶
Duration(**kwargs: Unpack[DurationParams])
Bases: Scalar
A PromQL duration scalar composed of time-unit components.
Duration literals combine non-negative integers with unit suffixes:
ms (milliseconds), s (seconds), m (minutes), h (hours),
d (days, always 24 h), w (weeks, always 7 d), y (years,
always 365 d). Multiple units may be combined from longest to shortest
(e.g. 1h30m, 2d12h, 54s321ms). The optional neg flag
prepends a - sign for negative offsets. Fields are keyword-only, see
DurationParams.
Examples:
>>> print(Duration(h=1, m=30))
1h30m
>>> print(Duration(ms=500))
500ms
>>> print(Duration(d=1, neg=True))
-1d
>>> print(Duration())
0s
Raises:
| Type | Description |
|---|---|
TypeError
|
If an unknown keyword (e.g. |
ValueError
|
If any unit field is negative; use |
subquery
¶
subquery(range: SCALAR_TYPE, resolution: SCALAR_TYPE | None = None, *, offset: SCALAR_TYPE | None = None, at: AT_TYPE | None = None) -> Subquery
Evaluate this expression as a subquery: <expr>[range:resolution].
See Subquery; slice syntax expr[range:resolution]
is a shorthand for this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
range
|
SCALAR_TYPE
|
Window length; raw numbers are seconds ( |
required |
resolution
|
SCALAR_TYPE | None
|
Step between evaluations; |
None
|
offset
|
SCALAR_TYPE | None
|
Time shift; raw numbers are seconds. |
None
|
at
|
AT_TYPE | None
|
Evaluation time: a Unix timestamp, |
None
|
from_value
classmethod
¶
from_value(value: SCALAR_TYPE) -> Scalar | Variable
Wrap a raw number of seconds, passing Scalar and Variable through unchanged.
The number is split into units and rounded to whole milliseconds, so
from_value(300) renders as 5m and from_value(-90) as -1m30s.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
SCALAR_TYPE
|
Number of seconds; a |
required |
to_string
¶
to_string(indent: str | int | None = None, _indent_level: int = 0, *, style: Style = DEFAULT_STYLE) -> str
Render this expression as a PromQL string.
String
¶
String(value: str, quote: Literal['`', '"', "'"] = '"')
Bases: Query
A PromQL string literal with configurable quoting.
Supports single-quote ('), double-quote ("), and backtick
(`) delimiters. Double- and single-quoted strings process
Go-style escape sequences (\n, \t, \\, etc.).
Backtick strings are raw — no escape processing is performed and
newlines are preserved as-is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The unquoted string content. |
required |
quote
|
Literal['`', '"', "'"]
|
Delimiter: |
'"'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> print(String("prod"))
"prod"
>>> print(String("us-east", "'"))
'us-east'
>>> print(String("line\nnew", "`"))
`line
new`
subquery
¶
subquery(range: SCALAR_TYPE, resolution: SCALAR_TYPE | None = None, *, offset: SCALAR_TYPE | None = None, at: AT_TYPE | None = None) -> Subquery
Evaluate this expression as a subquery: <expr>[range:resolution].
See Subquery; slice syntax expr[range:resolution]
is a shorthand for this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
range
|
SCALAR_TYPE
|
Window length; raw numbers are seconds ( |
required |
resolution
|
SCALAR_TYPE | None
|
Step between evaluations; |
None
|
offset
|
SCALAR_TYPE | None
|
Time shift; raw numbers are seconds. |
None
|
at
|
AT_TYPE | None
|
Evaluation time: a Unix timestamp, |
None
|
from_value
classmethod
¶
from_value(value: STRING_TYPE) -> String | Variable
Wrap a raw str, passing String and Variable through unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
STRING_TYPE
|
required |
to_string
¶
to_string(indent: str | int | None = None, _indent_level: int = 0, *, style: Style = DEFAULT_STYLE) -> str
Render this expression as a PromQL string.
Variable
¶
Variable(name: str, *, braces: bool = False)
Bases: Query
A Grafana dashboard template variable.
Renders using Grafana's variable-interpolation syntax ($name or, when
braces are needed — e.g. immediately followed by another identifier
character — ${name}). Grafana substitutes the variable's selected
value(s) before the resulting text is sent to Prometheus, so a
Variable can stand in anywhere a Query is
expected: as a metric selector, a binary-operator operand, or an
aggregation/function argument.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Variable name, without the leading |
required |
braces
|
bool
|
Render |
False
|
Examples:
>>> print(Variable("job"))
$job
>>> print(Variable("job", braces=True))
${job}
subquery
¶
subquery(range: SCALAR_TYPE, resolution: SCALAR_TYPE | None = None, *, offset: SCALAR_TYPE | None = None, at: AT_TYPE | None = None) -> Subquery
Evaluate this expression as a subquery: <expr>[range:resolution].
See Subquery; slice syntax expr[range:resolution]
is a shorthand for this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
range
|
SCALAR_TYPE
|
Window length; raw numbers are seconds ( |
required |
resolution
|
SCALAR_TYPE | None
|
Step between evaluations; |
None
|
offset
|
SCALAR_TYPE | None
|
Time shift; raw numbers are seconds. |
None
|
at
|
AT_TYPE | None
|
Evaluation time: a Unix timestamp, |
None
|
to_string
¶
to_string(indent: str | int | None = None, _indent_level: int = 0, *, style: Style = DEFAULT_STYLE) -> str
Render this expression as a PromQL string.