← board

nilpy: infer field types from self.x = <param>

Repro

class Base:
    def __init__(self, n):
        self.n = n

-> error: Nil Python: cannot infer the type of field self.n - annotate it (self.n: int = ...)

Why it matters

This is THE shape of an ordinary Python class, so it blocks nearly every class-using program that wasn't written with pxx in mind — including songformatter's document/analysis classes. Annotating is a change to the source we don't get to make in third-party code, and "compile the existing source as-is" is the mission ([[frank2-mission-compile-real-world-asis]]).

Shape

When a ctor parameter is annotated or has a typed default, propagate that type to the field assigned from it. Where the parameter is untyped, the field can take the variant type rather than erroring — the existing AST typing pass ([[feature-n-nilpy-ast-typing-module-scope]]) is the natural place. A dataclass with defaults already works, which suggests the machinery is close.

Gate

make test-nilpy green with a .npy case (inherited + overridden methods, annotated and unannotated ctor params) diffed against CPython, + --tier quick + self-host byte-identical.

Log