@SemanticAnalyzerCore.visit.dispatch
def visit(self, node: astx.SubscriptExpr) -> None:
"""
title: Visit SubscriptExpr nodes.
parameters:
node:
type: astx.SubscriptExpr
"""
self.visit(node.value)
if not isinstance(node.index, astx.LiteralNone):
self.visit(node.index)
value_type = self._expr_type(node.value)
if isinstance(value_type, astx.ListType):
if isinstance(node.index, astx.LiteralNone):
self.context.diagnostics.add(
"list slicing is not supported",
node=node,
code=DiagnosticCodes.SEMANTIC_TYPE_MISMATCH,
)
self._set_type(node, None)
return
index_type = self._expr_type(node.index)
if not is_integer_type(index_type):
self.context.diagnostics.add(
"list indexing requires an integer index",
node=node.index,
code=DiagnosticCodes.SEMANTIC_TYPE_MISMATCH,
)
if not list_has_concrete_element_type(value_type):
self.context.diagnostics.add(
"list indexing requires a single concrete list element "
"type",
node=node.value,
code=DiagnosticCodes.SEMANTIC_TYPE_MISMATCH,
)
self._set_type(node, list_element_type(value_type))
return
if isinstance(node.value, astx.LiteralDict):
if not node.value.elements:
self.context.diagnostics.add(
"SubscriptExpr: key lookup on empty dict",
node=node,
)
elif not isinstance(
node.index,
(
astx.LiteralInt8,
astx.LiteralInt16,
astx.LiteralInt32,
astx.LiteralInt64,
astx.LiteralUInt8,
astx.LiteralUInt16,
astx.LiteralUInt32,
astx.LiteralUInt64,
astx.LiteralFloat32,
astx.LiteralFloat64,
astx.Identifier,
),
):
self.context.diagnostics.add(
"SubscriptExpr: only integer and floating-point "
"dict keys are supported",
node=node,
)
self._set_type(
node,
cast(
astx.DataType | None,
getattr(value_type, "value_type", None),
),
)