Nerodia Reference Manual

7. Python Typing🔗

Nerodia has a single baseline representation of Python objects, Py, which takes as a type parameter a Typing. For example, an instance of str in Python is represented as Py str through its abbreviation, PyStr. A Py value carries a proof of its typing. For instance, s : Py str implies s ⦂ str.

🔗def
Nerodia.Py (T : Typing) : Type
Nerodia.Py (T : Typing) : Type

A typed Python object.

🔗def

A typing predicate for Python objects.

This is the propositional equivalent of a Python type expression. However, it can express more complex types (e.g., intersections) than Python's base type system and is thus closer in power to the type system of Python's more advanced type checkers (e.g., ty).

🔗def

Holds if self has the Python typing T. Written as self ⦂ T.

🔗def
Nerodia.PyObject.attachType {T : Typing} {α : Type} [ViewPy T α] (self : PyObject) (h : self ⦂ T) : α
Nerodia.PyObject.attachType {T : Typing} {α : Type} [ViewPy T α] (self : PyObject) (h : self ⦂ T) : α

Types self as T using a proof of correctness.

For example, the following pattern in Python:

if isinstance(self, T):
  # Python type checkers would assume `self: T` in this block
  doSomethingWithT(self)
else:
  differentType()

can be implemented in Lean like so:

if h : self ⦂ T then let self := self.attachType h doSomethingWithT self else differentType

7.1. Weak Typing🔗

Typing in Python is mutable. Objects can have their type changed by reassigning their __class__ attribute, and types themselves can have their inheritance tree change by reassigning __bases__.

As such, most type relations in Python do not hold statically and therefore cannot be modelled correctly and safely by a pure Typing in Lean. Nonetheless, statically typing Python objects in Lean is still useful, so Nerodia provides a mechanism for weak typing. Objects are annotated with erased type hints to indicate the expected type and the Typing of weak types verifies the presence of its corresponding hint.

For example, it is possible to mutate objects between the many subtypes of BaseException (e.g., an object can be retyped Exception). As such, instances of these subtypes are weakly typed.

7.2. Strong Typing🔗

While the Python specification leaves the mutability of an object's type undefined, the CPython implementation places notable restrictions on this mutability. Notably, it prevents reassignment between many builtin types (e.g., str).

Nerodia leverages this to provide pure type checks. Their static types (e.g., PyStr) then hold a proof of this check through their Typing. Since many builtin types are also immutable, the data of such types can be safely accessed in a pure manner (e.g., PyStr.toString).

def testStrInstance (o : PyObject) : IO Unit := if h : o ⦂ str then IO.println s!"str: {o.attachType h |>.toString}" else IO.println "not a str" not a str #eval PyIO.toIO do testStrInstance (← mkPyInt 1) str: hi #eval PyIO.toIO do testStrInstance (← mkPyStr "hi")
not a str
str: hi

Still, there are caveats. Foremost, this is not strictly in accordance with the Python specification, which leaves the mutability of an object's type undefined. However, CPython's implementation strongly assumes confusion between builtin types cannot happen (e.g., retyping an int to/from a str would easily segfault when used). Weighing these considerations, Nerodia chooses to model builtin types functionally to make reasoning easier and more pure, accepting the cost of a potential future breakage in the event of an unlikely, massive CPython refactor.