Skip to content

literals

Handle literal expressions, list operations, and generic subscript expressions during semantic analysis.

Classes:

ExpressionLiteralVisitorMixin

Bases: SemanticVisitorMixinBase

Methods:

visit

visit(node: SubscriptExpr) -> None
Source code in packages/irx/src/irx/analysis/handlers/_expressions/literals.py
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
@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),
        ),
    )