Skip to content

validation

Functions:

validate_assignment

validate_assignment(
    diagnostics: DiagnosticBag,
    *,
    target_name: str,
    target_type: DataType | None,
    value_type: DataType | None,
    node: AST,
) -> None
Source code in src/irx/analysis/validation.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def validate_assignment(
    diagnostics: DiagnosticBag,
    *,
    target_name: str,
    target_type: astx.DataType | None,
    value_type: astx.DataType | None,
    node: astx.AST,
) -> None:
    """
    title: Validate an assignment.
    parameters:
      diagnostics:
        type: DiagnosticBag
      target_name:
        type: str
      target_type:
        type: astx.DataType | None
      value_type:
        type: astx.DataType | None
      node:
        type: astx.AST
    """
    if not is_assignable(target_type, value_type):
        diagnostics.add(
            f"Cannot assign value of type '{value_type}' to '{target_name}'",
            node=node,
        )

validate_calendar_date

validate_calendar_date(value: str) -> date
Source code in src/irx/analysis/validation.py
247
248
249
250
251
252
253
254
255
256
def validate_calendar_date(value: str) -> date:
    """
    title: Validate a date component.
    parameters:
      value:
        type: str
    returns:
      type: date
    """
    return date.fromisoformat(value)

validate_call

validate_call(
    diagnostics: DiagnosticBag,
    *,
    function: SemanticFunction,
    arg_types: list[DataType | None],
    node: FunctionCall,
) -> None
Source code in src/irx/analysis/validation.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def validate_call(
    diagnostics: DiagnosticBag,
    *,
    function: SemanticFunction,
    arg_types: list[astx.DataType | None],
    node: astx.FunctionCall,
) -> None:
    """
    title: Validate a function call.
    parameters:
      diagnostics:
        type: DiagnosticBag
      function:
        type: SemanticFunction
      arg_types:
        type: list[astx.DataType | None]
      node:
        type: astx.FunctionCall
    """
    if len(function.args) != len(list(node.args)):
        diagnostics.add("codegen: Incorrect # arguments passed.", node=node)
        return

    for idx, (param, arg_type) in enumerate(zip(function.args, arg_types)):
        if not is_assignable(param.type_, arg_type):
            diagnostics.add(
                f"Argument {idx} for '{function.name}' has incompatible type",
                node=node,
            )

validate_cast

validate_cast(
    diagnostics: DiagnosticBag,
    *,
    source_type: DataType | None,
    target_type: DataType | None,
    node: AST,
) -> None
Source code in src/irx/analysis/validation.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def validate_cast(
    diagnostics: DiagnosticBag,
    *,
    source_type: astx.DataType | None,
    target_type: astx.DataType | None,
    node: astx.AST,
) -> None:
    """
    title: Validate a cast expression.
    parameters:
      diagnostics:
        type: DiagnosticBag
      source_type:
        type: astx.DataType | None
      target_type:
        type: astx.DataType | None
      node:
        type: astx.AST
    """
    if source_type is None or target_type is None:
        return
    if is_assignable(target_type, source_type):
        return
    if _is_numeric_cast_type(source_type) and _is_numeric_cast_type(
        target_type
    ):
        return
    if isinstance(target_type, (astx.String, astx.UTF8String)):
        return
    diagnostics.add(
        f"Unsupported cast from {source_type} to {target_type}",
        node=node,
    )

validate_literal_datetime

validate_literal_datetime(value: str) -> datetime
Source code in src/irx/analysis/validation.py
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
def validate_literal_datetime(value: str) -> datetime:
    """
    title: Validate an astx datetime literal.
    parameters:
      value:
        type: str
    returns:
      type: datetime
    """
    stripped = value.strip()

    if "T" in stripped:
        date_part, time_part = stripped.split("T", 1)
    elif " " in stripped:
        date_part, time_part = stripped.split(" ", 1)
    else:
        raise ValueError("invalid datetime format")

    if "." in time_part:
        raise ValueError("fractional seconds are not supported")
    if time_part.endswith("Z") or "+" in time_part or "-" in time_part[2:]:
        raise ValueError("timezone offsets are not supported")

    try:
        year_str, month_str, day_str = date_part.split("-")
        year = int(year_str)
        month = int(month_str)
        day = int(day_str)
    except Exception as exc:
        raise ValueError("invalid date part") from exc

    if not (INT32_MIN <= year <= INT32_MAX):
        raise ValueError("year out of 32-bit range")

    try:
        time_parts = time_part.split(":")
        if len(time_parts) not in {
            TIME_PARTS_HOUR_MINUTE,
            TIME_PARTS_HOUR_MINUTE_SECOND,
        }:
            raise ValueError("invalid time part")
        hour = int(time_parts[0])
        minute = int(time_parts[1])
        second = (
            int(time_parts[2])
            if len(time_parts) == TIME_PARTS_HOUR_MINUTE_SECOND
            else 0
        )
    except Exception as exc:
        raise ValueError("invalid time part") from exc

    if not (0 <= hour <= MAX_HOUR):
        raise ValueError("hour out of range")
    if not (0 <= minute <= MAX_MINUTE_SECOND):
        raise ValueError("minute out of range")
    if not (0 <= second <= MAX_MINUTE_SECOND):
        raise ValueError("second out of range")

    try:
        return datetime(year, month, day, hour, minute, second)
    except ValueError as exc:
        raise ValueError("invalid calendar date/time") from exc

validate_literal_time

validate_literal_time(value: str) -> time
Source code in src/irx/analysis/validation.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def validate_literal_time(value: str) -> time:
    """
    title: Validate an astx time literal.
    parameters:
      value:
        type: str
    returns:
      type: time
    """
    if "." in value:
        raise ValueError("fractional seconds are not supported")
    parts = value.split(":")
    if len(parts) not in {
        TIME_PARTS_HOUR_MINUTE,
        TIME_PARTS_HOUR_MINUTE_SECOND,
    }:
        raise ValueError("invalid time format")
    try:
        parsed = [int(part) for part in parts]
    except ValueError as exc:
        raise ValueError("invalid time format") from exc

    hour, minute = parsed[0], parsed[1]
    second = parsed[2] if len(parsed) == TIME_PARTS_HOUR_MINUTE_SECOND else 0

    if not (0 <= hour <= MAX_HOUR):
        raise ValueError("hour out of range")
    if not (0 <= minute <= MAX_MINUTE_SECOND):
        raise ValueError("minute out of range")
    if not (0 <= second <= MAX_MINUTE_SECOND):
        raise ValueError("second out of range")

    return time(hour, minute, second)

validate_literal_timestamp

validate_literal_timestamp(value: str) -> datetime
Source code in src/irx/analysis/validation.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def validate_literal_timestamp(value: str) -> datetime:
    """
    title: Validate an astx timestamp literal.
    parameters:
      value:
        type: str
    returns:
      type: datetime
    """
    if "." in value:
        raise ValueError("fractional seconds are not supported")
    if "Z" in value or "+" in value[10:] or "-" in value[10:]:
        raise ValueError("timezone offsets are not supported")
    try:
        return datetime.fromisoformat(value)
    except ValueError as exc:
        raise ValueError(str(exc)) from exc