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.

Static typing for Python-style code, with no type hints (1:30). Captions are on; choose a chapter to jump to it.

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

  1. Static typing checks every value's type before a program runs. Dynamic typing checks as each line executes.
  2. Osprey's Python flavor keeps def, lambda and f-strings, infers every type, and checks it at compile time.
  3. This function has no annotations. The compiler works out that host is a string, because endsWith takes one, and that label returns a string.
  4. Main calls it with two strings.
  5. It runs, and prints Kitchen, kitchen.local.
  6. Pass the port number by mistake.
  7. 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.
  8. To test this on a real program, I ported a Chromecast sender to Python flavor.
  9. Where the original raised a KeyError for a missing TV, the port returns a result, and every caller must handle it.
  10. That's twelve hundred lines, and more than half its functions carry no annotation at all.
  11. The original's functions run under CPython and write the expected results. The port must match every one of them.
  12. Two hundred and fifty two checks, none failed.
  13. Python flavor is Osprey, not CPython. An arbitrary Python program does not run unchanged. To call real Python libraries, use Osprey's Python interop.
  14. Install Osprey, run the sample, then pass it a number. Osprey is actively evolving. Try it on your projects.