modal-labs / modal-client

Python client library for Modal
https://modal.com/docs
Apache License 2.0
271 stars 35 forks source link

Experimental: Class "Strict" parameter mode #1968

Closed freider closed 3 months ago

freider commented 3 months ago

In "strict" parameter mode, class parameterization change behavior in the following ways:

Reasoning behind class_parameters:

We could (and are basically doing that, at this stage) just use cbor2 directly on the parameter kwargs, similar to how we use pickle today and never store any schema, and things would generally work well, with the added benefit of supporting all cbor2 native types as parameter values.

However, the stored class_parameters schema is useful/necessary for three main reasons:

Feature flag

This new feature is currently gated behind the MODAL_STRICT_PARAMETERS=1 env var and uses the existing class constructor logic but with signature inspection.

When we actually release this to users (when the backend support for web parameterization is live) an idea is to instead activate strict parameters when users use a new kind of dataclass-inspired schema (which has other benefits!), and keep the unstrict behavior of custom constructors, to not break any existing user code.

Note that this is not part of this PR, just an example of how we'd introduce this in a non-breaking way:

@app.cls()
class Foo:
    a: int [= parameter(default=123)]  # 
    b: int = field(init=False)  # non-parameter annotation, stuff that's set in @enter methods etc.

The above syntax would let us use typing.dataclass_transform() to get good editor support for our classes even without explicit constructors 🤓