TYPED CONVERSIONS
Use small, fixed local containers and simple records. Their contents and structure must fit the forms below.
Mode: typed conversion. Select Transpile → Typed, then click Transpile, or use the default CLI/API request. For runtime strings, shared containers, classes, and closures, see Runtime value conversions.
Assign a container once, directly in a function body, with 1–64 elements. Do not resize it, reassign it, alias it through another variable, pass it to a function, or return it.
def pick() -> int:
values = [10, 20, 30]
return values[-1]
pick() returns 30.
Use elements of one type: bool, int, float, or str. An index must be a direct integer literal within bounds; negative indices are supported. You can also iterate over the elements in order.
def is_priority(value: int) -> bool:
priorities = {1, 4, 9}
return value in priorities
is_priority(4) returns True; is_priority(2) returns False.
Use unique literal int, finite float, or bool elements, all of one type. Check membership with in or not in, using the local set name. The searched value must have that same type.
Empty sets, strings in sets, computed elements, duplicate elements, set methods, set indexing, and set iteration are unsupported. Use a list or tuple when you need iteration or indexing.
def lookup() -> int:
values = {'low': 1, 'high': 2}
return values['high']
lookup() returns 2.
Keys must be unique literal integers or strings of one type. Values must also use one supported scalar type. Lookup needs a literal key present in the dictionary. Iteration yields keys in insertion order.
Check for empty, oversized, mixed-type or nested contents; computed indices; element changes; methods; or a container used outside its local function. Comprehensions and generator expressions are unsupported in typed conversion. Use runtime list comprehensions when building a list from supported iterables. Flat scalar assignment such as a, b = 1, 2 is supported separately; unpacking an arbitrary container variable is not.
class Point:
x: int
y: int
def __init__(self, x: int, y: int) -> None:
self.x = x
self.y = y
def sum_point(left: int, right: int) -> int:
point = Point(left, right)
return point.x + point.y
sum_point(3, 4) returns 7.
Define 1–64 annotated int, float, or bool fields, followed by one initializer. Each initializer parameter must match a field and be assigned directly to it. Create a fresh local instance with positional arguments, then read its fields.
Annotate a parameter or result with the record's class name. This complete example constructs a point in one function, returns it to the caller, and passes it to another function.
class Point:
x: int
y: int
def __init__(self, x: int, y: int) -> None:
self.x = x
self.y = y
def make_point(x: int, y: int) -> Point:
point = Point(x, y)
return point
def sum_point(point: Point) -> int:
return point.x + point.y
def keep_point(point: Point) -> Point:
return point
def total(x: int, y: int) -> int:
point = make_point(x, y)
return sum_point(keep_point(point))
total(3, 4) returns 7; total(-3, 3) returns 0. A function can also return another record-returning call, such as return keep_point(make_point(x, y)).
| Operation | Supported form |
|---|---|
| Declare a type | Define the class before the module's functions. Use its unquoted name: point: Point and -> Point. |
| Create or receive a value | Assign a constructor or factory call to one fresh local directly in the function body. Construct with point = Point(x, y) before return point. |
| Pass a value | Use exact positional arguments. Pass a record local, parameter, or record-returning call to a parameter annotated with that same class. |
| Return a value | Return a local, parameter, or record-returning call. Every reachable exit must return the declared type; branches may select among existing values. |
| Read a field | Use a local or parameter, such as point.x. First assign a factory result to a local before reading its fields. |
Each record becomes a C struct. Parameters and returns copy its scalar fields by value, so no pointer to a completed function's local storage escapes and no allocation or free is needed. Typed source cannot mutate records or inspect object identity. Within that accepted subset, value copies preserve the observable field values of the Python program.
Record classes have distinct types even when their fields match. A Point cannot be passed where another class is declared. The same declarations are available to C callers through separate C module exports.
Do not change fields, rebind a record parameter/local, or create a second local with other = point. Identity checks, truth tests, equality on whole records, dynamic attributes, nested captures, records in containers, and record fields containing strings, containers, or other records are unsupported.
Record-boundary functions use positional calls without defaults, variadic arguments, or keyword-only parameters. Inline constructor expressions such as return Point(x, y) and sum_point(Point(x, y)) are not yet supported: construct a fresh local first. Quoted/forward record annotations, inheritance, decorators, and methods beyond the exact initializer are also unsupported.
Python module scope: the record class and every function that receives or returns it must belong to the same supplied Python module. Class imports and record values crossing Python modules remain unsupported. Other modules may call a scalar wrapper such as total above. Separate generated C callers can use the exported struct interface.