Skip to content
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

Use Ident::parse_any for name attributes #4226

Merged
merged 1 commit into from
Jun 3, 2024
Merged

Conversation

Databean
Copy link
Contributor

@Databean Databean commented Jun 2, 2024

This makes it possible to use rust keywords as the name of python class methods and standalone functions. For example:

struct MyClass {
}

impl MyClass {
    #[new]
    fn new() -> Self {
        MyClass {}
    }

    #[pyo3(name = "struct")]
    fn struct_method(&self) -> usize {
        42
    }
}

fn struct_function() -> usize {
    42
}

From the syn::Ident documentation:

An identifier constructed with Ident::new is permitted to be a Rust keyword, though parsing one through its Parse implementation rejects Rust keywords. Use input.call(Ident::parse_any) when parsing to match the behaviour of Ident::new.

Fixes issue #4225

@adamreichold
Copy link
Member

Could you please add your example as a test case so that we do not regress on this in the future? Thank!

(It would be nice if it covered as many places as possible, e.g. functions, methods, properties, etc.)

@Databean Databean force-pushed the keyword branch 3 times, most recently from cb20999 to 5d49cee Compare June 2, 2024 20:15
This makes it possible to use rust keywords as the name of python class
methods and standalone functions. For example:

```
struct MyClass {
}

impl MyClass {
    #[new]
    fn new() -> Self {
        MyClass {}
    }

    #[pyo3(name = "struct")]
    fn struct_method(&self) -> usize {
        42
    }
}

fn struct_function() -> usize {
    42
}
```

From the [`syn::Ident`
documentation](https://docs.rs/syn/2.0.66/syn/struct.Ident.html):

> An identifier constructed with `Ident::new` is permitted to be a Rust
keyword, though parsing one through its
[`Parse`](https://docs.rs/syn/2.0.66/syn/parse/trait.Parse.html)
implementation rejects Rust keywords. Use `input.call(Ident::parse_any)`
when parsing to match the behaviour of `Ident::new`.

Fixes issue PyO3#4225
@Databean
Copy link
Contributor Author

Databean commented Jun 2, 2024

Added test cases and a newsfragments entry.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@davidhewitt davidhewitt added this pull request to the merge queue Jun 3, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Jun 3, 2024
@davidhewitt davidhewitt added this pull request to the merge queue Jun 3, 2024
Merged via the queue into PyO3:main with commit 93ef056 Jun 3, 2024
41 checks passed
@Databean Databean deleted the keyword branch June 3, 2024 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants