Skip to content

collections

Classes:

LiteralDict

LiteralDict(
    elements: dict[Literal, Literal],
    loc: SourceLocation = NO_SOURCE_LOCATION,
)

Bases: Literal

Methods:

Source code in packages/astx/src/astx/literals/collections.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def __init__(
    self,
    elements: dict[Literal, Literal],
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """
    title: Initialize LiteralDict.
    parameters:
      elements:
        type: dict[Literal, Literal]
      loc:
        type: SourceLocation
    """
    super().__init__(loc)
    self.elements = dict(elements)
    key_types = {type(k.type_) for k in elements.keys()}
    value_types = {type(v.type_) for v in elements.values()}
    self.type_ = DictType(
        key_types.pop()() if len(key_types) == 1 else Int32(),
        value_types.pop()() if len(value_types) == 1 else Int32(),
    )
    self.loc = loc

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/literals/base.py
58
59
60
61
62
63
64
65
66
67
68
69
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST representation for the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"Literal[{self.type_}]: {self.value}"
    value = self.value
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

LiteralList

LiteralList(
    elements: list[Literal],
    loc: SourceLocation = NO_SOURCE_LOCATION,
)

Bases: Literal

Methods:

Source code in packages/astx/src/astx/literals/collections.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def __init__(
    self, elements: list[Literal], loc: SourceLocation = NO_SOURCE_LOCATION
) -> None:
    """
    title: Initialize LiteralList.
    parameters:
      elements:
        type: list[Literal]
      loc:
        type: SourceLocation
    """
    super().__init__(loc)
    self.elements = list(elements)  # Ensure correct type
    unique_types = {type(elem.type_) for elem in elements}
    self.type_ = ListType([t() for t in unique_types])
    self.loc = loc

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/literals/base.py
58
59
60
61
62
63
64
65
66
67
68
69
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST representation for the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"Literal[{self.type_}]: {self.value}"
    value = self.value
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

LiteralSet

LiteralSet(
    elements: set[Literal],
    loc: SourceLocation = NO_SOURCE_LOCATION,
)

Bases: Literal

Methods:

Source code in packages/astx/src/astx/literals/collections.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def __init__(
    self, elements: set[Literal], loc: SourceLocation = NO_SOURCE_LOCATION
) -> None:
    """
    title: Initialize LiteralSet.
    parameters:
      elements:
        type: set[Literal]
      loc:
        type: SourceLocation
    """
    super().__init__(loc)
    self.elements = set(elements)
    unique_types = {type(elem.type_) for elem in elements}
    self.type_ = SetType(
        unique_types.pop()() if len(unique_types) == 1 else Int32()
    )
    self.loc = loc

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/literals/base.py
58
59
60
61
62
63
64
65
66
67
68
69
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST representation for the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"Literal[{self.type_}]: {self.value}"
    value = self.value
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

LiteralTuple

LiteralTuple(
    elements: tuple[Literal, ...],
    loc: SourceLocation = NO_SOURCE_LOCATION,
)

Bases: Literal

Methods:

Source code in packages/astx/src/astx/literals/collections.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def __init__(
    self,
    elements: tuple[Literal, ...],
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """
    title: Initialize LiteralTuple.
    parameters:
      elements:
        type: tuple[Literal, Ellipsis]
      loc:
        type: SourceLocation
    """
    super().__init__(loc)
    self.elements = elements
    self.type_ = TupleType([elem.type_ for elem in elements])
    self.loc = loc

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/literals/base.py
58
59
60
61
62
63
64
65
66
67
68
69
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST representation for the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"Literal[{self.type_}]: {self.value}"
    value = self.value
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )