Giter Site home page Giter Site logo

Comments (4)

Icxolu avatar Icxolu commented on June 16, 2024 2

How about allowing #[pyo3(signature = ...)] on complex enum variants to specify the constructor signature? Like so:

#[pyclass]
enum Thing
  #[pyo3(signature = (x = None, y = 0, z = 1)]
  A {
    x: Option<i32>,
    y: i32,
    z: i32,
  }
}

This would be comparable to building the class hierarchi manuelly and gives the same control (default, pos/kw only args, ...) as a the constructor for a #[pyclass] struct would. We would also be able to reuse the existing attribute implementation from #[pyfunction]/#[pymethods]. I've experimented with that idea over here Icxolu@3e2c41b

from pyo3.

EricLBuehler avatar EricLBuehler commented on June 16, 2024

@Icxolu, I think that looks great! Do you plan to open a PR?

from pyo3.

Icxolu avatar Icxolu commented on June 16, 2024

Thanks! Glad you like it. Yes, if people think the syntax and functionality proposed above is workable I'll open a PR from that. Given that #[pyo3(signature = ...] is currently only supported on functions (and is most intuitively understand there), I think I'll wait for a few more opinions whether we should reuse it here, or if we perhaps should rename it in this position
(#[pyo3(constructor = ...)], or something like that could also be an option) to make clear what the signature refers to.

from pyo3.

wyfo avatar wyfo commented on June 16, 2024

I really like it too. And actually, I think it could also make sense to extend this feature to regular struct, to auto-generate constructors in a kind of dataclass-like fashion (but not necessarily in the same PR).

from pyo3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.