Static typing for Python-style code, with no type hints
Static typing checks every value's type before a program runs. This video shows Osprey's Python flavor doing that with no type hints, first on a five-line sample, then on a real port of a CPython program.
The full write-up, with the code and its limits, is Static typing for Python-style code, without writing type hints.
Osprey is actively evolving.
Transcript
- Static typing checks every value's type before a program runs. Dynamic typing checks as each line executes.
- Osprey's Python flavor keeps def, lambda and f-strings, infers every type, and checks it at compile time.
- This function has no annotations. The compiler works out that host is a string, because endsWith takes one, and that label returns a string.
- Main calls it with two strings.
- It runs, and prints Kitchen, kitchen.local.
- Pass the port number by mistake.
- The build stops, with the line and the two types that disagree. In CPython, that mistake surfaces only when the line runs, if it ever does.
- To test this on a real program, I ported a Chromecast sender to Python flavor.
- Where the original raised a KeyError for a missing TV, the port returns a result, and every caller must handle it.
- That's twelve hundred lines, and more than half its functions carry no annotation at all.
- The original's functions run under CPython and write the expected results. The port must match every one of them.
- Two hundred and fifty two checks, none failed.
- Python flavor is Osprey, not CPython. An arbitrary Python program does not run unchanged. To call real Python libraries, use Osprey's Python interop.
- Install Osprey, run the sample, then pass it a number. Osprey is actively evolving. Try it on your projects.