-
Notifications
You must be signed in to change notification settings - Fork 784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unify #[pyfunction]
and #[pyfn]
#694
Comments
But... are they so confusing? Or am I too accustomed? |
The guide is clear enough on the differences. It's only a little confusing - but mostly I think for new users of pyo3. Another downside to having two different attributes is that they might drift apart over time if we're not careful. For example, if #692 lands, then the way that each of these specify the python name is different:
vs
It seems like a good idea to me to simplify the codebase by unifying these attributes. |
@kngwyu I would not say they are confusing if you read the guide, but I agree with @davidhewitt - these two procedural macros apparently do the same thing but in two different contexts. Consider that even if the documentation clarifies this, it's definitely going to be hard to remember which one to use in which context unless you do it basically every day, so you'll have to look it up every time. Readers of the code who aren't necessarily reading the documentation will wonder why one or the other is used, etc. If it's possible to unify them into a single macro, IMO, it should definitely be done. |
I'd rather introduce #[pymodule]
mod mymod {
#[pyfunction]
fn myfunction(py: Python, ...) {}
} and make |
I do like this new syntax for
|
I think attributes should be #[pymodule]
mod my_mod {
#[pyclass]
struct MyClass { ... }
#[pyfunction]
fn myfunction(py: Python, ...) { ... }
#[pyattribute]
// can override name on all module members using #[name]
#[name = "MY_PYTHON_NAME"]
pub const MY_CONST: &str = "My str";
#[init]
fn my_init(py: Python, module: &PyModule) {
// custom init code; called after adding all other module members
// can add non-const attributes here using module.add()
}
// add in external #[pyclass], #[pyfunction]
#[pyclass]
#[name = "NameStillWorksHere"]
pub use path::to::external::MyClass;
#[pyfunction]
pub use path::to::external::my_function;
} |
I did consider I really like your idea of |
I'm sorry I noticed @programmerjake
|
It's stable (in the latest releases): rust-lang/rust#64273 What's unstable is non-inline #[my_proc_macro]
mod my_external_mod; It's unstable because they haven't yet decided if the proc-macro will see
just use the name that the
|
Wow, thank you for letting me know that 👍
Another idea: How about removing the whole |
I would leave the #[pymodule]
mod my_mod {
#[pyfunction]
use my::other_fn;
#[pyfunction]
fn my_fn() -> i32 {
other_fn(123)
}
} |
Proc-macros on |
Indeed, I'm dangerously tempted to try to do this very soon! If anyone else wants to play with it, feel free to claim this and have a go and I'll be a happy reviewer / co-designer 😄 |
It seems confusing to me to have two different attributes,
#[pyfunction]
and#[pyfn]
, which both wrap Rust functions to create python functions. Pyo3 users need to know which applies in two different contexts.I'd argue we should merge these. Then there's only one attribute which pyo3 users need to know about. It would also help us to simplify / refactor the macro code if we didn't need two different parsing systems.
I think this would need to go in the following steps:
#[pyfunction]
to be used in place of#[pyfn]
#[pyfunction]
everywhere#[pyfn]
to expand to#[pyfunction]
#[pyfn]
to emit an error, stating the#[pyfunction]
invocation to use instead#[pyfn]
I've already opened a PR #693 to achieve step 1.
The text was updated successfully, but these errors were encountered: