Path: blob/main/python/pylang/test/annotations.py
1396 views
# Annotations are completely disabled by default, so that it is possible to use mypy.1# This turns them back on:2from __python__ import annotations34def add(a: int, b: float):5return a + b67assrt.ok(add.__annotations__)8assrt.equal(add.__annotations__['a'], int)9assrt.equal(add.__annotations__['b'], float)10assrt.equal(add.__annotations__['return'], undefined)1112def sum(ls: list) -> int:13pass1415assrt.ok(not (sum.__annotations__ == undefined))16assrt.deepEqual(sum.__annotations__, {17'ls': list,18'return': int19})2021def optional(a:int=10):22return a2324assrt.ok(not (optional.__annotations__ == undefined))25assrt.equal(optional.__annotations__.a, int)26assrt.equal(optional.__defaults__.a, 10)2728def otherexpr(a:3+4) -> [1, 2]:29pass3031assrt.ok(not (otherexpr.__annotations__ == undefined))32assrt.equal(otherexpr.__annotations__['a'], 7)33assrt.deepEqual(otherexpr.__annotations__['return'], [1, 2])3435def basic(x:float):36pass3738assrt.deepEqual(basic.__annotations__, {39'x': float40})4142def kwstarargs(*args:list, **kwargs:dict) -> int:43pass4445assrt.equal(kwstarargs.__annotations__['return'], int)4647def nothing():48pass4950assrt.ok(nothing.__annotations__ == undefined)51assrt.throws(def():52nothing.__annotations__['return']53)5455test = def(x: int):56pass5758assrt.deepEqual(test.__annotations__, {59'x': int60})6162anonreturn = def() -> 'test':63pass6465assrt.equal(anonreturn.__annotations__['return'], 'test')6667assrt.equal(def asexpr(a: int):68a69.__annotations__['a'], int)7071assrt.deepEqual(def(a: int) -> float:72a + 10.073.__annotations__, {74'a': int,75'return': float76})7778class A:7980def f(self, a : int, b: 'x') -> float:81pass8283assrt.deepEqual(A.prototype.f.__annotations__, {'a':int, 'b':'x', 'return': float})848586