pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Add a `#[pyo3]` macro

Open konstin opened this issue 7 years ago • 1 comments

It would be great to have a single #[pyo3] macro like the #[wasm-bindgen] macro in wasm-bindgen. This macro would have the following properties:

  • It can be attached to functions, classes, impl $struct and impl $protocol for $struct. The macro can decide where it's attached to by using syn::parseinto a syn::Item, matching and delegating to the respective code gen function.
  • It supports the parameters currently supported (mostly by reusing the existing code)
  • Method types (static, method, etc.) are automatically detected. The constructor might require a #[pyo3(constructor)] annotation
  • There is #[pyo3(module)] to replace #[pymodule]. It doesn't need to support #[pyfn] though since we can do freestanding functions now.
  • Tests for all features. This doesn't need to be as elaborate as wasm-bindgen, but maybe we can come up with something where we feed syn item to functions in pyo3-derive-backend. (Integration tests are also sufficient)

The best was to get started is probably adding a function in pyo3cls/src/lib.rs that dispatches to the existing macros and then expanding upon that. Looking into capybara*s pyo3 wrapper and wasm bindgen's macro frontend should give some additional pointers.

konstin avatar Aug 25 '18 19:08 konstin

I have been thinking about this and think it's a good idea. We also implemented attributes like #[name = "foo"] on #[pyclass], but in retrospect I think that might have been a mistake and should have been namespaced as #[pyo3(name = "foo)].

We might want to do a review of how much we can add in a non-breaking fashion already and then consider deprecating old names later.

davidhewitt avatar Aug 06 '20 13:08 davidhewitt