Repository navigation
Releases
Releases · typy-lang/typy
TyPy 0.5.0
Added
- An instance of a class of the program that never outlives the function
that makes it is made in the function's frame, with no allocator call:
one the function reads, assigns attributes of, calls methods on, or
passes to functions that do no more.--emit=tyirends the line of each
such instance within frame(MEM-03, ADR-0042). - The table
[allocator]intypy.tomlnames the C functions a program
allocates with, in place ofmalloc,reallocandfree, and the
library that defines them, such as mimalloc'smi_malloc,mi_realloc
andmi_freefrommimalloc. A wrong table is E0107 (MEM-08,
ADR-0043). typy build --lib staticand--lib sharedbuild a module as a C
library,libNAME.aorlibNAME.so, with the C headerNAME.h. The
functions__all__lists are exported under their own names, with
int,floatandboolasint64_t,doubleandbool. The module
runs when the library is loaded, and is named after its file. An
exception that would leave an export ends the process: aSystemExit
with its status, any other with its report and an abort. E0429 names a
module or an export a library cannot have;typy check --libreports it
too (CLI-09, FFI-05, EXC-05, ADR-0044).- A library's files carry the release of its version, from the module's
__version__or elseversionintypy.toml, as Linux names
libraries:libNAME.so.5.0with the sonamelibNAME.so.5and links
libNAME.so.5andlibNAME.so, andlibNAME.5.0.awith a link
libNAME.a(CLI-13, ADR-0045). - A library build also writes
NAME_binding.py, a module that loads the
library withctypesby its soname and defines a typed function for
each export, and its stubNAME_binding.pyi, so a Python program
imports the binding and calls the library (FFI-08, ADR-0046). ctypes: a library a module declares withctypes.CDLL("name")or
ctypes.cdll.LoadLibrary("name")is linked when the program is built,
and a C function whoseargtypesandrestypeare assigned at module
level is called directly, with every integer type fromc_int8to
c_uint64,c_float,c_double,c_bool,c_char_pandc_void_p
converted asctypesconverts them, alsoctypes.ArgumentErrorfor an
inttoo large for afloat. E0430 names a declaration TyPy cannot
compile (FFI-01 to FFI-03, ADR-0047).ctypesobjects, pointers and arrays:c_int(5)and every scalar type
ofctypesmake an object whosevaluea program reads and assigns and
a C function writes throughbyref(x)or aPOINTER(T)parameter;
pointer(x),POINTER(T)(),contentsand indexes read and write
through a pointer, which a C function may also return;(T * n)(...)
makes an array withlen, checked indexes, iteration andin; and
sizeofgives a class's size, each as in CPython.argtypesand
restypetakePOINTER(T)and names bound to a type (FFI-03,
ADR-0048).ctypes.Structuresubclasses with a literal_fields_list, laid out as
C lays out a struct: fields read and assign as in CPython, a structure,
array or pointer field reads as an object over its memory, structures
nest, fill arrays and sit behind pointers, and a C function takes and
returns a structure by value as the x86-64 System V calling convention
passes it, in registers or in memory (FFI-03, ADR-0049).library-pathsand[libraries."NAME"]withlink = "static"or
"dynamic"intypy.tomlsay where the linker looks for C libraries and
how each is linked (FFI-04).- A benchmark report,
docs/testing/benchmark-report.md, which compares
TyPy's programs with CPython 3.12 to 3.15, PyPy, mypyc, Cython, Nuitka,
Codon, C, C++, Rust, Go, Java and JavaScript on twelve workloads: time,
peak memory, startup, build time and size.make bench-peersinstalls the
peers at pinned versions andmake bench-compareruns it. - Assignment expressions,
name := value, with CPython's scoping in
comprehensions and its syntax errors; a test of(name := value) is not Nonenarrowsname(EXP-13). matchstatements with literal, value, capture, wildcard,as,|,
sequence, mapping and class patterns and guards, with CPython's syntax
errors for patterns. A class pattern reads__match_args__from the
class body or a base, and a class pattern that CPython would answer
withTypeErroris refused with E0401. A case narrows the subject as
the test it stands for does, so aftercase None:an optional subject
is no longerNone, andcase [x, *rest]narrows a
list[int] | Noneto the list (CST-06).- The warning W0401 for a
matchwhose cases leave members of the
subject's union, optional orbooltype unmatched, naming them. A
matchwhose cases cover every member is complete, so a function that
returns from each case needs noreturnafter it (CST-07, ADR-0033). @dataclasswrites__match_args__, the names of the fields, and
takesmatch_args=Falseto leave it out (CLS-05, CST-06).- The names
Optional,Union,List,Dict,Set,TupleandType
oftypingin annotations, as the types they spell (LIB-17). IterableandIteratoroftypingandcollections.abc, and
List,Dict,Set,Tuple,Type,Optional[...]and
Union[...]oftyping, as the class ofisinstanceand of a class
pattern, with CPython's answers (TYP-17).- A loop, a comprehension,
list,sorted,sumand the other builtins
that read an iterable go over a variable that holds a union of fixed
tuples, such astuple[str] | tuple[str, int](TYP-14, ADR-0034). argsof an exception andcodeof aSystemExitare values of
their own: stored, passed, indexed, unpacked and looped over. The type
ofargscomes from every creation of the class and of the classes
derived from it, in the program and in TyPy's own raises, such as
tuple[str, int] | tuple[str];codeisNone, the one argument, or
the tuple. The five items of a Unicode error join only in a program
that encodes, decodes or opens a file (EXC-15, ADR-0034).- A class may define the in-place methods, such as
__iadd__, which
x += ycalls, falling back to__add__; an augmented assignment that
calls an operator method takes an item or an attribute of any
expression as its target, evaluated once (CLS-20, CLS-32). - Assigning a class variable through a subclass that inherits it gives
the subclass a variable of its own, which it, its subclasses and their
instances read from then on, as CPython does; through a variable such
asclsit is planned for 1.0 (CLS-25, CLS-31, ADR-0037). - A class method may be called through an instance, which passes the
instance's class ascls, and through a subclass defined in another
module than the class that defines it, which gets a copy of its own.
Through an instance of a generic class it is planned for 1.0 (CLS-25,
CLS-30, ADR-0037). - A static method may be called through any expression that gives an
instance, such asmake().helper(), which is evaluated first, as
CPython does (CLS-25). super(Base, self)takes any class of the enclosing class's chain of
bases, and finds the method of the nearest base ofBase, as CPython
does; with another instance it is planned for 1.0 (CLS-26, CLS-29).- Dataclasses take
field(default_factory=f), called for each instance
that is not given the field, hash as the tuple of their fields when
frozen or givenunsafe_hash=True, and take the optionsinit,
repr,unsafe_hashandslots, as CPython'sdataclassesdoes.
Other options offieldand@dataclassare planned for 1.0
(CLS-15, CLS-16, CLS-17, CLS-28). math.fsum,dist,prodandsumprodtake items of a union of
number types, such as alist[int | float], andprodaboolstart,
with CPython's results.prodof items that are not numbers is planned
for 1.0 (LIB-15, LIB-21).- A function may import a module that imports its own module at module
level, the usual way to break an import cycle, as CPython allows; the
function is checked once that module is. Inside__init__it is
planned for 1.0 (MOD-13, MOD-15). OSErrortakes arguments of any type, more than five of them, and two
or more throughsuper().__init__or to a subclass with an__init__
of its own, as CPython does:OSError("a", "b")is[Errno a] b.
errno,strerror,filenameandfilename2take their types from
every creation of the class, and an error about a file opened from a
descriptor has the descriptor asfilename. A number as the third
argument ofOSErrororBlockingIOErroris planned for 1.0 (EXC-16,
EXC-18, ADR-0035).- A literal index into a variable that holds a union of fixed tuples
raisesIndexErrorwhen the tuple it holds is too short, as CPython
does, in place of E0307; a position outside every member is still E0307
(DAT-08). sys.version_infoas a value of its own: stored, put in containers,
imported withfrom sys import version_infoand printed as CPython
writes it, and read as the tuple of its items by indexing, slicing,
comparing,len, loops and unpacking (LIB-16).- Type aliases of another module, by name or through the module,
type
statements inside functions, classes and blocks, and bounds and
constraints on an alias's type parameters, and an alias read as a
value, atyping.TypeAliasTypewith__name__and__module__
(SMT-12). - One value assigned to several targets, as in
a = items[0] = value, or
unpacked into targets, is held by each target as its own type, a union
or an optional among them (TYP-16). - A
\uor\Uescape of a surrogate in astrliteral or an
f-string gives that code point, as CPython keeps it (LEX-10). NotImplemented. An operator or comparison method that returns it
gives way to the other operand's reflected method, and an in-place
method to the binary methods; wh...
TyPy 0.4.0
Added
- Single inheritance between classes of the program: a class may derive
from a class defined before it, named by its name or as
module.Class, and holds its base's attributes and methods. An
instance is accepted wherever its base is, as an argument, in a
variable, in alist[Base]and as aBase | None, andtype(),
str,reprandprintuse its own class. A subclass may declare an
inherited attribute again with the same type (CLS-04, ADR-0021). - Overriding methods: a subclass, including an exception class, may
define a method of its base again, and a call runs the method of the
instance's own class, also forfor,nextandwith. An override
is checked against the base method as type checkers check it, E0404
otherwise; one that changes a default, or takes or returns a type
held differently, such asint | Noneforint, is planned for 0.5
(CLS-04, CLS-13, ADR-0021). super()in any method:super().name(...)andsuper(Class, self)
call the nearest base's method, andsuper().__init__()in a class
whose bases have no__init__does nothing, asobject.__init__
does. Outside a method it is E0414 (CLS-14). It comes after a
subclass assigns its own attributes when the base's__init__uses
the instance, which could run an override that reads them, E0309
otherwise (CLS-04).isinstancewith a class of the program, a built-in type or an
exception class, or a tuple of them; a variable narrows to the tested
class where the test holds, also after anifthat returns and in
the right operand ofand. A class held in a variable and
issubclassare planned for 0.5 (CLS-09, TYP-07).- Class variables,
name = valueandname: ClassVar[T] = valuein a
class body, read through the class or an instance and assigned
through the class; and attribute defaults,name: T = value, which
each new instance starts with (CLS-11, ADR-0024). - Comparison methods
__eq__,__ne__,__lt__,__le__,__gt__
and__ge__, and__hash__: operators call them, with CPython's
reflected methods and the subclass's method first, and lists, sets,
dicts,in,sorted,minandmaxcall them through the class's
type object. A class with__eq__and no__hash__has no hash
(CLS-07, ADR-0026). @dataclassfromdataclasses, witheq,orderandfrozenand
field(default=...): it writes__init__,__repr__,__eq__and
the ordering methods as CPython does, and refuses what CPython
refuses, such as a mutable default. The hash of a frozen dataclass
anddefault_factoryare planned for 0.5 (CLS-05, LIB-06).- Unions, such as
int | str,str | list[int] | Noneor
Circle | Square: a union of objects is held as the object itself, and
any other with a tag that names its member, so anintstored in an
int | strallocates nothing. A union can be passed, returned, stored,
printed, written with
strandrepr, given totype()and tested for truth.isinstance
narrows it in both branches, and a union of classes that share a base
has that base's attributes and methods. Any other operation on a
variable that holds a union, such asvalue.upper(),len(value)or
value + value, is checked for each member and runs the one for the
object's member. Lists, sets and dicts of a union compare and hash
each item as its member, so1and1.0are one key (TYP-14, TYP-07,
ADR-0027). - Generic functions written with PEP 695 type parameter lists, such as
def first[T](items: list[T]) -> T, with bounds,[S: Shape], and
constraints,[N: (int, float)]. A call takes the types from its
arguments, and TyPy compiles one copy of the function for each set of
types, so a generic call costs what a written-out call does.TypeVar
andGenericare E0202; a function that would need copies without
end is E0428. Calling a generic function of another module is planned
for 0.5 (GEN-07), and*Ts,**Pand defaults for 1.0 (GEN-01 to
GEN-03, GEN-06, ADR-0023). - Generic classes,
class Box[T]:, made byBox(1), whose types come
from__init__'s arguments, or byBox[str]("a"), and named
Box[int]in annotations. Each copy holds its attributes in place, and
answers as the generic class:type(Box(1)) is Boxand
isinstance(Box(1), Box)hold. A generic class with a base, a class
derived from one, and its class variables and defaults are planned for
0.5 (GEN-01, GEN-03, GEN-08, ADR-0028). - Protocols:
class Shape(Protocol):fromtypinglists methods and
attributes, and any class that has them satisfies it. A parameter
typed with a protocol, also insidelist[Shape], compiles to one copy
for each class it is called with, and[S: Shape]bounds a type
parameter by one. A class that falls short is E0404, naming the
member; calling a protocol andisinstancewith one are E0401. A
protocol as any other type, as a base, or with properties, static and
class methods or class variables is planned for 0.5 (GEN-04, GEN-05,
GEN-10, ADR-0023). ...alone as a statement does nothing, aspassdoes.opentakes a file descriptor, giving a file whosenameis thatint,
andclosefd, with CPython's errors (LIB-14).opentakes a mode held in a variable, which opens a text file and is
checked with CPython's errors when the program runs; a binary mode in a
variable raisesTypeError(LIB-14, ADR-0031).opentakes binary modes, which give aBufferedReader,
BufferedWriterorBufferedRandom, or aFileIOwithbuffering=0,
reading and writingbyteswith CPython's results and errors (LIB-14,
ADR-0031).opentakes the modes"r+","w+","a+"and"x+", which read and
write one text file as CPython's do (LIB-14).close()ofsys.stdin,sys.stdoutorsys.stderrcloses it, as in
CPython: reading it, writing to it,input()andprint()then raise
ValueError: I/O operation on closed file.(LIB-14).opentakesbuffering,encoding(utf-8,asciiorlatin-1),
errors(strict,ignore,replace,backslashreplace,
xmlcharrefreplaceorsurrogateescape) andnewline, with CPython's
results and errors;buffering=1writes out each line (LIB-14).- Text files have
name,modeandclosed, and the methodsfileno,
isatty,readable,writable,seekable,seek,telland
truncate, with CPython's results and errors; a file opened with"a"
starts at its end (LIB-14). - Names a module binds by a lazy import can be used from another module,
m.x = vassigns through a lazily importedm, and a variable may be
bound by more than one lazy import; each runs the pending import first,
as CPython does (MOD-11). A function or a class bound by two lazy
imports is planned for 0.5 (MOD-12). format % valueswith a format that is not a literal, also through
%=, and with atuple[T, ...]of values: the runtime reads the format
and raises what CPython raises (LIB-11).OSErrorand its subclasses built with two to five arguments, read as
errno,strerror,filename,winerrorandfilename2, with the
attributeserrno,strerror,filenameandfilename2.OSError
itself becomes the subclass forerrno, as in CPython. Other argument
types are planned for 0.5 (EXC-14, EXC-16).argsof an exception andcodeof aSystemExit, where a program
prints, formats, compares with==or!=, or tests them, and
len(error.args). Other uses, such aserror.args[0], are planned for
0.5 (EXC-11, EXC-13, EXC-15, ADR-0030).sys.stdin, a text file thatinput()reads too, andsys.maxsize,
sys.maxunicode,sys.byteorder,sys.platform,
sys.getrecursionlimitandsys.setrecursionlimit. The recursion
limit is kept forgetrecursionlimit; recursion still stops at the
stack's limit (LIB-13, FUN-02).sys.version,sys.hexversionandsys.version_inforeport CPython
3.15.0, whose behaviour TyPy follows.sys.version_infois compared
with a tuple ofints, indexed and read by field; as a value of its
own it is planned for 0.5 (LIB-13, LIB-16, ADR-0029).math.fsum,math.dist,math.prod,math.sumprod,math.gamma
andmath.lgamma, with CPython's results to the last bit. The first
four take iterables ofint,boolorfloatitems;prodof
floats with the default start andsumprodwith afloatare
int | float, since CPython gives theintstart or 0 for no items.
Items of a union and aboolstart are planned for 0.5 (LIB-12,
LIB-15).- Methods with type parameters,
def pair[T](self, value: T), also
static methods, in a class that is not generic. A call takes the types
from the arguments afterselfand runs the copy for them. Type
parameters of a method of a generic class, of a special method, a
property or a class method, and of a method that overrides or is
overridden are planned for 0.5 (GEN-01, GEN-09). FinalandFinal[T]fromtyping: a module or function variable, a
class variable or an attribute that__init__declaresFinalis
assigned only by its declaration, and any other assignment is E0414. A
Finalwithout a value is E0408 (TYP-08).- The
typestatement at module level:type Name = annotationmakes
Namestand for the annotation in the module's annotations, and may
name a class defined below it, as CPython reads the value late. An
alias may take type parameters,type Pair[T] = tuple[T, T], and
Pair[int]stands for its value withintin place ofT. An alias
of another module, read as a value, defined inside a function, a class
or a block, or with a bound on a type parameter is planned for 0.5
(SMT-10, SMT-12). - Container methods:
len(x),x[k],x[k] = v,del x[k]and
v in xcall__len__,__getitem__,__setitem__,__delitem__
and__contains__, and truth tests,anyandallcall `__bool...
TyPy 0.3.0
Added
openfor text files in UTF-8, in the modesr,w,aandx:
read,readline,readlines,write,writelines,flushand
close, iteration over the lines, andwith. Reading translates
newlines and decodes as CPython does, files still open are written out
when the program ends, and errors are CPython'sOSErrorsubclasses
with its text, such as[Errno 2] No such file or directory: 'notes.txt'.OSErrorand all its subclasses can be caught and
raised. Binary and+modes, other encodings,seekand the file's
attributes are planned for 0.4 (LIB-04, LIB-14, EXC-14, ADR-0019).- The
sysmodule'sargv,exit,stdoutandstderr,printwith
fileandflush, andinput, which raisesEOFErrorat the end of
standard input. An uncaughtSystemExitends the program with its
code as the exit status and no traceback, as in CPython. Other names
ofsysare E0201 when planned for 0.4 (LIB-13) and E0202 otherwise;
the switches of lazy imports are E0202 (LIB-04, MOD-09). - The
mathmodule: its constants and its functions of numbers, with
CPython's results to the last bit and CPython 3.15's error messages;
a name newer than the program's Python floor is E0425.fsum,prod,
sumprod,dist,gammaandlgammaare planned for 0.4 (LIB-05,
LIB-12, ADR-0018). format % values, printf-style formatting of a string literal, with
every conversion, flag, width, precision and*, and%(key)swith a
dict, giving CPython's results. A format that does not fit its values
is E0427 with CPython's message; a format that is not a literal is
planned for 0.4 (LIB-10, LIB-11).typy explain CODEprints what a diagnostic means, the rule it
enforces, a program that gives it and the corrected program; the
troubleshooting page shows the same programs, and the documentation
test runs them (CLI-10).- Warnings, where CPython warns while compiling: W0101 for an invalid
escape sequence such as"\d", with the doubled backslash as its fix,
and W0301 forassert (test, "message"), which is always true. They
never fail a build unless--deny-warningsis given (DIA-07, DIA-08,
ADR-0017). --error-format=jsonwrites each diagnostic as one JSON object per
line, with the same keys every time, for editors and CI (DIA-05).typy.tomlbeside the main file setspython-floorandmodule-paths,
the directories where imports look for modules after the main file's
directory;--python-flooroncheck,buildandrunoverrides the
file. A setting that is not valid is E0107 at its line, with exit status
2 (CLI-07, CLI-12, MOD-01).- Lazy imports:
lazy import mandlazy from m import namewith the
Python floor 3.15, and a module-level__lazy_modules__at every floor.
The module runs at the first use of a name, an error it raises reaches
that use, and the next use runs it again. Using such a name from another
module is E0201, planned for 0.4 (MOD-06 to MOD-08, MOD-11). Iterator[T]andIterable[T]annotations fromtypingor
collections.abc, andTracebackTypeandModuleTypefromtypes. An
Iterable[T]takes any built-in iterable or iterator ofTand keeps
the object, so a list passed as one can be read twice (TYP-15, CST-10,
MOD-10).- A truth test,
notorbool()of a value whose truth TyPy cannot know
is E0304 everywhere, not only in a condition (EXP-10). make benchtimes the release-built compiler on generated 10,000-line
projects, in one file and in 200 modules, against the budgets of NFR-04,
NFR-05 and NFR-14, and checks that checking time grows linearly with the
number of modules; CI runs it.- Programs of several modules:
import m,import a.b as cand
from m import namefor functions, classes, module variables and
submodules, packages with__init__.py, and relative imports. Each
module runs once, at its first import, and runs again at the next import
when its code raised. A module object prints as
<module 'm' from 'm.py'>, with the path from the main file's directory
(SMT-07, MOD-01 to MOD-03, MOD-10). - E0206 for a module the program does not have, E0422 for a circular
import at module level with each import shown, and E0425 for a name a
module does not define; standard library modules TyPy provides later
are E0201 (MOD-05, LIB-08). globalin a function, and functions that read the variables of their
module. A read before the module binds the variable raises CPython's
NameError, and CPython'sSyntaxErrors forglobalare E0103 (SMT-08).NameErrorandImportErroras built-in exception classes (EXC-01).raisewith an exception or an exception class, andraise ... from causeandraise ... from None. An uncaught exception prints its cause
or its context first, with CPython's line between them (SMT-06, EXC-04,
STD-05).- The built-in exception classes as values, annotations and calls, with
CPython's bases, so aKeyErrormay be stored where aLookupErroris
declared;NotImplementedErrorandAttributeErrorare new (EXC-01,
EXC-02, EXC-10). str,repr,printand f-strings of an exception give CPython's text,
computed from its arguments;reprof aUnicodeDecodeErrorshows its
five arguments (EXC-08).- Exception classes of your own: a class derived from a built-in
exception class, or from such a class defined before it, with
attributes, methods and its own__init__,__str__and__repr__.
Calling it keeps the arguments passed by position as the exception's
arguments, andsuper().__init__(...)in__init__calls the base's.
exceptcatches it by any of its bases, andiscompares exceptions
(CLS-03, CLS-13, CLS-14, EXP-08). - Assigning a list to a slice of a list,
xs[a:b:c] = items, with
CPython's results: a simple slice grows or shrinks the list, and an
extended slice takes exactly as many items or raisesValueError
(DAT-13). delof list items, list slices with or without a step, and dict keys,
with CPython's errors; E0421 fordelof a variable, an attribute or
part of a tuple,strorbytes(SMT-05).isandis notbetweenboolandbool | Nonevalues, such as
done is Trueorflag is False.True,FalseandNoneare each a
single object, as in CPython, so the result is that of==(EXP-08).dict.keys()anddict.items()as sets:|,&,-and^with a
set, another view or a list give a new set in CPython's order, and==,
<and the other comparisons compare their items.dict.values()
compares by identity, as in CPython (DAT-16).dict(name=value, ...), with or without a source, and
dict.fromkeys(keys)anddict.fromkeys(keys, value), with CPython's
order of evaluation (DAT-16).reversedof a dict, ofdict.keys(),dict.values()and
dict.items(), from the last entry to the first; a dict that changes
size raisesRuntimeError, as in CPython (DAT-16).rangeas a value and an annotation:len, indexing, slicing,in,
==, use as a dict key or set item, and CPython's text.for,
reversed,iterandnextread it, and its bounds may be of any size
(DAT-17).with, with one or more context managers andastargets, calling
__exit__on every way out of the body; a true result of__exit__
suppresses the exception. The parameters of__exit__may be left
without annotations, and its traceback is an object that holds no
frames (CST-05, CST-10).- E0420 for
withon a value whose class has no__enter__and
__exit__(CST-05). forover an instance of a class that defines__iter__and
__next__, which ends when__next__raisesStopIteration(CST-08).iter()andnext(), with or without a default, for lists, tuples,
str,bytes, sets, dicts and views of dicts, and for classes with
__iter__and__next__, with CPython's iterator behaviour: a list
iterator sees appended items, and a set or dict that changes size raises
RuntimeError(CST-09).assert testandassert test, message, which raiseAssertionError
when the test is false in every build mode, evaluating the message only
then; what the test shows holds after it (SMT-04).finally, which runs on every way out of atry: falling through,
return,break,continueand an exception, which keeps going after
it. E0419 refuses areturn,breakorcontinuethat would leave a
finallyblock, as CPython 3.14 warns about it (CST-04, EXC-06).trywithexceptclauses andelse:except C,except (A, B),
except C as nameand a bareexcept. A clause catches its classes and
every class derived from them, the name afterasis deleted when the
clause ends, and a bareraiseinside a clause re-raises the exception.
An exception raised while another is handled keeps it as its context,
and an uncaught one prints it first (CST-04, EXC-01, EXC-04, EXC-07,
EXC-09).- E0418 for an
exceptclause that names something other than exception
classes (EXC-09). - E0415 for
raiseof a value that is not an exception, E0416 for a bare
raiseoutside anexceptclause, and E0417 for a call of
UnicodeDecodeErrororUnicodeEncodeError(EXC-07, EXC-12).
Changed
- The last use of a variable moves its value into an assignment, a
display,append,insert,addor a dict entry, and a new value such
as a call's result goes into a collection without being counted, so
building a collection in a loop does no reference counting of its items
(MEM-09). --releaseand--release-smallbuilds leave out debug information; a
static musl hello world is about 30 KB (CLI-05, NFR-01).
Fixed
- A format specification on an exception, such as
f"{error:>5}",
raisesTypeErrornaming the exception's own class, as CPython does;
it namedNoneType. - A deeply nested program no longer crashes
typy; it abo...
TyPy 0.2.0
Added
- Tuples: displays such as
(1, "a"), annotations such as
tuple[int, str]andtuple[()], items read at a literal position such
aspair[0]orpair[-1],+,len, comparisons item by item, truth
tests,strandrepr, and tuples as arguments and return values
(DAT-08, TYP-06). - Unpacking assignment into names and nested tuples of names, such as
a, b = b, aandx, (y, z) = t(SMT-02). - Lists of
int,float,bool,strand lists: displays such as
[1, 2], annotations such aslist[str], an empty[]where an
annotation, a return type or a parameter gives its type, items read and
assigned at positions counted from either end withIndexErroroutside,
append,len,inandnot in,==, truth tests,strandrepr,
andforover a list. Two names bound to one list share it (DAT-07,
DAT-11, TYP-05, EXP-07, CST-03). A list stores each item in eight bytes,
so alist[float]holds raw doubles (MEM-02), and a millionintitems
fit in 8.5 MB (NFR-13). - Slices of strings and lists, such as
text[1:4],xs[::-1]and
xs[-2:], with Python's defaults and clamping, andValueErrorfor a
zero step (DAT-12). - The list methods
pop,insert,extend,remove,index,count,
clear,copyandreverse, with CPython's errors (LIB-03). +and*on lists, ordering of lists with<,<=,>and>=,
xs += ysandxs *= nchanging the list in place, and augmented
assignment of a list item such ascounts[i] += 1(EXP-02, EXP-06,
SMT-01).- An empty
[]in a comparison takes the type of the other operand, so
xs == []needs no annotation, and one inside a display takes the type
of the other items, as in[[1], []](TYP-05). sorted, withreverse,list.sort,sum, with an exact result for
intitems and CPython's compensated result forfloatitems,any,
all, andminandmaxof a list, withdefault(LIB-02, LIB-03).
sumof alist[float]takes a start such as0.0, because CPython
returns theint0 for an empty list without one. Thekeyargument
arrives with function values in 1.0 (FUN-05).- Lists of tuples and of
None, such aslist[tuple[int, str]], with every
list operation. A tuple is stored inline in the list, so a million pairs
ofintvalues take 16 MB (DAT-07, MEM-02). list(),list(xs), andlistof astr,enumerate,zipor
reversed;sorted,min,max,sum,anyandalltake those
sources too (LIB-02).inon astr, a substring test, and on a tuple, and slices of a tuple
with literal bounds such aspair[1:](EXP-07, DAT-12).tuple[T, ...], a tuple of any length, withtuple(source), indexing,
slicing,in,len,+,*, comparisons,count,indexandfor.
A tuple such as(1, 2)is accepted where atuple[int, ...]is
expected, andforgoes over a tuple whose items have one type (DAT-14,
CST-03).bytes: literals such asb"a\x00",bytes(),bytes(n)and
bytes(values), items asintvalues, slices,len,+,*,
comparisons,in,for, andstrandrepras CPython writes them
(DAT-06).- The methods of
bytesthatstrhas too,findtojoin,strip,
replace, padding,upper,lower,swapcase,capitalize,title
and theistests, with CPython's ASCII meaning, andbytes.hex
(LIB-03). str.encode,bytes.decodeandbytes(text, encoding, errors)with the
codecsutf-8,asciiandlatin-1under all their CPython names, and
the error handlersstrict,ignore,replace,backslashreplace,
xmlcharrefreplaceandsurrogateescape, raisingUnicodeEncodeError
andUnicodeDecodeErrorwith CPython's messages (LIB-03, EXC-02).- The
strmethodsfind,rfind,index,rindex,count,
startswith,endswith,split,rsplit,splitlines,partition,
rpartition,join,strip,lstrip,rstrip,removeprefix,
removesuffix,replace,center,ljust,rjust,zfill,
expandtabsandisascii, with arguments by keyword where CPython allows
them (LIB-03). - The
strmethodsupper,lower,casefold,swapcase,capitalize
andtitle, andisalpha,isalnum,isdecimal,isdigit,
isnumeric,isspace,isupper,islower,istitle,isidentifier
andisprintable, from CPython 3.15's Unicode 17.0 data, with CPython's
final sigma and its title case of words such asthey're(LIB-03,
OI-15). round, with and withoutndigits, rounding half to even on the exact
value as CPython does, soround(2.675, 2)is2.67(LIB-02).forover the characters of astr, and overenumerate, with
start,zip, withstrict, andreversedof lists and strings,
nested as inenumerate(zip(a, b)), with CPython's errors (CST-03,
LIB-02).- Sets of numbers,
bool,None,str,bytesand tuples of them:
displays such as{1, 2}, annotations such asset[int],set()and
set(source),in,len, truth tests,strandrepr,for,
|,&,-and^and their in-place forms,==and the subset
comparisons, and the methodsadd,remove,discard,pop,
clear,copy,union,intersection,difference,
symmetric_difference,update, the_updatemethods,issubset,
issupersetandisdisjoint, which take any iterable of the items, and
several where Python takes several (DAT-10, DAT-15, LIB-03). A
set hashes its items as CPython does and iterates and prints them in
CPython's order (ADR-0010). A list or a set as an item of a set is
rejected, since it has no hash.removeof a missing item andpopof
an empty set raiseKeyError, and a set that changes size while a loop
reads it raisesRuntimeError. set()in a comparison takes the type of the other operand, as[]
does, sos == set()needs no annotation (TYP-05).- Dicts: displays such as
{"a": 1}, annotations such as
dict[str, int],{}anddict()where an annotation or the other
operand of a comparison gives the type,dict(source)from a dict or
from(key, value)pairs,d[k]withKeyErrorfor a missing key,
d[k] = vandd[k] += v,in,len, truth tests,strandrepr,
forover a dict,|,|=,==and!=, the viewskeys(),
values()anditems(), which follow the dict as it changes, and the
methodsgetandsetdefaultwith a default,pop,popitem,
update,clearandcopy(DAT-09, LIB-03). A dict keeps insertion
order, leaves holes where entries were removed until it is resized, and
raises CPython'sRuntimeErrorwhen it changes while a loop reads it.
Sets take a dict wherever they take an iterable, with CPython's steps
for a dict (DAT-15). - List, set and dict comprehensions, with several
forclauses andif
conditions, each in its own scope, so a comprehension variable does not
leak and the first source is read in the enclosing scope, as in CPython
(EXP-12). - Optional values: annotations such as
int | NoneandNone | str,
Noneor a value where one is expected, in variables, parameters,
defaults, returns, lists, sets, dicts and tuples,is Noneand
is not None, truth tests,strandrepr, anddict.get(key)without
a default (TYP-06). A variable narrows to its value where an
is not Nonetest, anis Nonetest that failed, a truth test, or an
assignment shows it is notNone, and using it unnarrowed is rejected
with a hint (TYP-13).Nonecosts no memory: it is held in a value the
type never takes (ADR-0011). - A display with
Noneamong its items, and a conditional expression with a
Nonebranch, have the optional type, such aslist[int | None]. range(...)andreversed(range(...))wherever an iterable is read: in
comprehensions,list,set,dict,sum,sorted,enumerate,
zipand the set methods, as well as infor(CST-03).- Classes:
classstatements with__init__, methods, attributes declared
in the class body or assigned in__init__, and instances in variables,
parameters, returns, lists, sets, dicts and tuples (CLS-01). Assigning or
calling a name the class does not have is rejected (CLS-02), and so is
any use ofselfin__init__before every attribute is assigned.
__str__and__repr__write instances forprint,str,reprand
containers, and a class without them is written as CPython writes it
(CLS-08). Instances are equal only to themselves, hashed by identity and
compared withis. A signature names a class defined below it, or its
own class, in quotes, such as"Node | None". - Every type has a type object whose bases end at
object, the base of
each class, ready for inheritance andisinstancein 0.4 (CLS-10,
ADR-0012). type(x)gives the type object of a value, and class names and the
built-in type names, such asintanddict, are type objects as values.
A type object prints as<class 'int'>or<class '__main__.Point'>,
has__name__, compares withisand==, and can be a dict key; the
annotationstypeandtype[int]hold one (CLS-12).- f-strings, with
!s,!rand!a,{x = }, nested fields in a format
specification, and the whole format specification mini-language for
int,bool,floatandstr: fill, alignment, sign,z,#,0,
width,,and_grouping, precision and the typesb,c,d,o,
x,X,n,e,E,f,F,g,G,%ands. Every result
and error message matches CPython 3.12 to 3.15 on a table of 4,628
cases, andfloatdigits are rounded exactly (LEX-07). - Passing a variable or a constant as an argument no longer counts a
reference, andreturn namemoves the variable's value to the caller
(MEM-04). An argument read from a container is kept alive for the call,
so a callee that empties the container still sees it, as in CPython. - User documentation in `d...
TyPy 0.1.0
The first release. TyPy 0.1 compiles programs made of module-level
functions and statements over int, float, str, bool and None to
native executables for x86_64 Linux, with glibc or statically with musl.
Each program it accepts prints what CPython 3.12 to 3.15 prints. The
compiler needs glibc 2.39 or later and a C toolchain.
Added
- Engineering documents: research, requirements specification, use case
and domain models, architecture, decision records ADR-0001 to ADR-0008,
detailed design for 0.1, test plan and development guide. - A development container with Rust 1.99.1, LLVM 23, musl, CPython 3.12 to
3.15 for conformance testing, and the documentation tools. - The
typy-diagnosticscrate: source spans, diagnostic codes, the code
registry and text rendering, with unit and property tests. - The
typy-sourcecrate: reading source files with the 16 MiB and UTF-8
checks, line and column positions, parsing with the program's Python
floor, and TyPy's own syntax tree with its text dump. - The
typy-checkcrate: the 0.1 feature table, name resolution with
Python's scoping, type inference within a function, definite assignment,
and the typing rules for the operators and builtins of 0.1. Every
rejection names its rule. - The
typy-tyircrate: TyIR's types, a function builder, a verifier that
checks definitions by dominance and the types of every operation, and the
text form for--emit=tyir. - The
typy-lowercrate: lowers a checked program to TyIR with reference
counting, error returns throughinvoke, and the semantics of CPython for
loops,and,or, comparison chains and the builtins of 0.1. - The
typy-codegencrate: turns TyIR into LLVM 23 IR, assembly and object
code, with static immortal constants and traceback entries on raising
calls;--release-smallmarks functionsminsize, as LLVM 23 has noOz. - The
typy-runtimecrate, without the standard library: object headers,
reference counting,strobjects and their operations, exceptions with
tracebacks and the uncaught-exception report, buffered output, the stack
check andmain.make runtimebuilds the archive programs link. - Unlimited-size
intin the runtime: arithmetic,//,%and**with
Python's floor rules, bitwise operators and shifts, comparisons, and
conversion to and fromstrwithint()'s parsing rules and CPython's
4300-digit limit. Error messages match CPython's. floatin the runtime:/,//,%,**andabswith CPython's
special cases;int / int,float(int)andfloat(str)rounded once to
the nearest double; exactint(float)and exact comparison ofintwith
float; andreprwith CPython's shortest digits and layout.- Every division by zero raises
ZeroDivisionError: division by zero, the
wording of CPython 3.14 and later (OI-14). reprof a string escapes exactly the code points CPython 3.15 escapes,
from Unicode 17.0 tables generated from CPython's own data (OI-15).chrandordin the runtime, with CPython's messages.int()andfloat()read decimal digits of every script, such as
int("\u0661\u0662"), which is12.- The
typy-buildcrate: build requests, modes and--emitstages, and
the link of an object file with the runtime archive throughccor
musl-gcc. The executable appears whole or not at all; a missing
toolchain program is E0701, a failed link E0702, and a missing runtime
archive, or one from another TyPy version, E0703. - Diagnostics without a source location, such as a failed link.
- The
typycommand:typy check FILE,typy build FILEwith--emit,
-o,--releaseand--release-small, andtypy run FILE -- ARGS,
which keeps each build in a cache underTYPY_HOMEand runs it again
without compiling. Exit status 0 on success, 1 for program errors, 2 for
wrong usage and 101 for an internal error, which a panic also becomes. - The
typy-conformancecrate and a conformance suite of 89 programs in
tests/conformance/: each accepted program runs under CPython 3.12 to
3.15 and under TyPy in debug and release mode, with the same output,
exit status and last line of standard error; each rejected program gets
exactly its diagnostic code at its line and column.make testruns it. - Every accepted conformance program also runs linked with AddressSanitizer
and LeakSanitizer and must show no report. A program that ends with an
uncaught exception frees the exception before it exits, so it leaks
nothing. - The sanitizer run uses a runtime built with AddressSanitizer, so every
memory access the runtime makes is checked.make miriruns the
runtime's unit tests under Miri, andmake fuzzfuzzes the source,
checking and lowering stages. - Continuous integration: the quality gate and the release archive on
every pull request, Miri and 30 minutes of fuzzing on demand before a
release, code scanning, dependency review, and monthly grouped Dependabot
updates. A tagvX.Y.Zreleases the
version after a maintainer approves it. make distbuilds a reproducible release archive of the compiler and its
runtime archives, and runs a program with it unpacked.make checkmeasures branch coverage as well as line coverage and fails
below 95 percent of either (NFR-11);make coverage-branchesruns that
measurement alone, andmake test-int,make test-e2eand
make test-allrun one test level each.- Golden tests of every text stage of
--emitand of rendered
diagnostics intests/golden/;make blessrewrites them. - End-to-end tests for a missing or failing C toolchain, an empty program,
the 16 MiB limit, a file that is not UTF-8, two builds at once, argument
forwarding bytypy run, and environment variables that must not change
the output. - The stack check reads the stack pointer register, so
RecursionError
is raised in time also when locals do not live on the machine stack. - The runtime archive is built for musl as well as glibc, and
make check
checks its size: 40.3 KB, and 29.8 KB without large integers (NFR-02). A
hello world built with--release-smalland linked statically with musl
is 75 KB (NFR-01). - E0106 when an output file cannot be written; the linker reports a
directory it cannot write with E0106 instead of E0702. - The
typy-drivercrate: runs loading, checking, lowering, code
generation and linking in order, and stops after the stage--emit
names.--emit=typed-astwrites every node with its position and type,
and--emit=tyirleaves out theincanddecthat--emit=tyir-mem
shows. - A quality gate,
make check, that checks formatting, runs Clippy and the
tests with at least 95 percent line coverage, lints the documents, checks for
non-ASCII text, checks that every requirement has a row in the test plan,
and checks that every diagram is rendered from its source.