Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add #[name = "foo"] attribute to #[pymethods] #692

Merged
merged 5 commits into from
Dec 19, 2019

Conversation

davidhewitt
Copy link
Member

@davidhewitt davidhewitt commented Dec 17, 2019

Closes #663

To achieve this, I added to FnSpec:

  • a new field name - the name of the Rust function this maps to
  • a new field python_name - either the optionally-set #[name], or the .unraw() rust name

While I was at it I did some refactoring to swap some panic!-s to spanned errors, and a bit of cleanup.

TODO:

  • Bikeshed name - #[pyname] perhaps?
  • Support #[pyfunction]?
  • Update documentation

CHANGELOG.md Outdated
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

* Support for `#[name = "foo"]` attribute in `#[pymethods]`. [#692](https://github.com/PyO3/pyo3/pull/692)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bikeshed: maybe the attribute should be #[pyname] instead of #[name]?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[pyname] could work, on the other hand, we already have #[text_signature] and not #[pytext_signature], however #[name] is much more likely to conflict with something in the future due to being a much more common word than #[text_signature]

Copy link
Contributor

@programmerjake programmerjake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the issues mentioned, it looks good!

Copy link
Contributor

@programmerjake programmerjake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@kngwyu
Copy link
Member

kngwyu commented Dec 18, 2019

Thank you!
Though I have a concern about this feature itself...(isn't it confusing to give different names to Python API?), this PR is generally well written and refactorings are good 👍
But, if possible, I want you to avoid refactoring codes unrelated to the PR.
It makes code review more difficult and introduces unnecessary merge conflicts.

@davidhewitt
Copy link
Member Author

Though I have a concern about this feature itself...(isn't it confusing to give different names to Python API?)

I agree most users won't need this. There will be edge cases like the example originally supplied on #663.

Note that the #[pyfn] macro actually requires a python name as the second argument. So adding this #[name] attribute brings the other macros to feature parity without forcing all users to specify names.

It's probably too breaking but I now actually think it would be nice to remove the required name argument from #[pyfn] and replace it with this optional attribute.

@davidhewitt
Copy link
Member Author

I've pushed changes to all the things you suggested.

But, if possible, I want you to avoid refactoring codes unrelated to the PR.
It makes code review more difficult and introduces unnecessary merge conflicts.

Sorry. I'll do refactorings as separate PRs next time 😄

@programmerjake
Copy link
Contributor

For #[getter] and #[setter], maybe it would be worthwhile to have the error message be something like: "#[name] can't be used with `#[getter]`, to override the Python name, use `#[getter(python_name)]`"

alternatively, for consistency, just accept:

#[getter]
#[name = "python_name"]
fn rust_name(&self) -> i32 {
    0
}
#[getter(python_name2)]
fn rust_name2(&self) -> i32 {
    0
}

but not:

#[getter(other_python_name)]
#[name = "python_name"]
fn rust_name(&self) -> i32 {
    0
}

@davidhewitt
Copy link
Member Author

davidhewitt commented Dec 18, 2019

For #[getter] and #[setter], maybe it would be worthwhile to have the error message be something like: "#[name] can't be used with #[getter], to override the Python name, use #[getter(python_name)]"

I actually did have different error messages (not quite so detailed as that), but removed it on kngwyu's recommendation 😄

alternatively, for consistency, just accept: [...] but not: [...]

I did consider this too, but I felt that it's better to have just one way to set the python name for a getter / setter. If the consensus is to enable #[name] for getters & setters, and only fail when it's set both ways, then I can change this PR to do that.

@kngwyu
Copy link
Member

kngwyu commented Dec 19, 2019

Thank you!

alternatively, for consistency, just accept:

I often feel this kind of design decision difficult.
Let's decide if it's good or not after using the current interface for a while.
If some users complain about this, then we should change the interface.

@kngwyu kngwyu merged commit c8cb3ad into PyO3:master Dec 19, 2019
@davidhewitt davidhewitt deleted the override-method-names branch December 19, 2019 06:31
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow overriding generated python method names
3 participants