Skip to content

Arbitrary Python objects #32

@adriangb

Description

@adriangb

Maybe this is totally off, but would it even be possible to support arbitrary Python objects? For example, if I had a Python function like this:

class Input(TypedDict):
  type: Literal["http"]
  status_code: int
  method: Literal["GET", "PUT"]
  callback: Callable[[], None]

class Output(TypedDict):
  message: str
  callback: Callable[[], None]

def process(inp: Input) -> Output:
  assert inp["type"] == "http"
  status_code = input["status_code"]
  assert 99 < status_code < 600
  method = input["method"]
  assert method in ("GET", "PUT")
  msg = f'{method} {status_code}"
  return Output(message, inp["callback"])

This crate is super convenient to parse and validate type, status_code and method since you can make a declarative serve model for this:

#[derive(Serialize, Deserialize]
#[serde(rename_all = "UPPERCASE")]
enum Method {
  Get,
  Post,
}

#[derive(Serialize, Deserialize]
struct HTTPMessage {
  status_code: u32,
  method: Method,
}

#[derive(Serialize, Deserialize]
#[serde(tag = "type")]
enum Message {
  Http(HTTPMessage),
}

But as far as I can tell there is no way to say "make the callback key a Py<PyAny>. In other words, something like:

#[derive(Serialize, Deserialize]
struct HTTPMessage {
  status_code: u32,
  method: Method,
  callback: Py<PyAny>,
}

Is that right?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions