Skip to content

Unraw complex struct enum field python names#5050

Merged
Icxolu merged 6 commits intoPyO3:mainfrom
hgmich:fix-raw-ident-enum-variant
Apr 7, 2025
Merged

Unraw complex struct enum field python names#5050
Icxolu merged 6 commits intoPyO3:mainfrom
hgmich:fix-raw-ident-enum-variant

Conversation

@hgmich
Copy link
Copy Markdown
Contributor

@hgmich hgmich commented Apr 6, 2025

Stray case related to #2395 (probably unnoticed because the functionality is newer and a bit of an edge case).

The python_name of struct-type enum variant fields (and the __match_args__ of the variant) do not unraw the names of these fields, which effectively makes them unusable as the resulting identifiers contain '#' which the Python lexer interprets as comments. The only reason you'd probably want to do this anyway is to use reserved identifiers (type in my case).

hgmich added 2 commits April 6, 2025 00:15
Stray case related to PyO3#2395 (probably unnoticed because the functionality is
newer and a bit of an edge case).

The `python_name` of struct-type enum variant fields (and the `__match_args__`
of the variant) do not unraw the names of these fields, which effectively
makes them unusable as the resulting identifiers contain '#' which the Python
lexer interprets as comments. The only reason you'd probably want to do this
anyway is to use reserved identifiers (`type` in my case).
Comment thread pyo3-macros-backend/src/pyclass.rs Outdated
field_names: &mut Vec<Ident>,
) -> syn::Result<(MethodAndMethodDef, syn::ImplItemFn)> {
let ident = format_ident!("__match_args__");
let field_names_unraw: Vec<_> = field_names.iter().map(|name| name.unraw()).collect();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not quite sure this is the best way to do this. Feedback appreciated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you don't have to collect here, an iterator should be enough for the quote below. (But that's just a tiny optimization)

Copy link
Copy Markdown
Member

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

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

Thank you! This looks good to me. Maybe we can add a small test case for it?

Comment thread pyo3-macros-backend/src/pyclass.rs Outdated
field_names: &mut Vec<Ident>,
) -> syn::Result<(MethodAndMethodDef, syn::ImplItemFn)> {
let ident = format_ident!("__match_args__");
let field_names_unraw: Vec<_> = field_names.iter().map(|name| name.unraw()).collect();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you don't have to collect here, an iterator should be enough for the quote below. (But that's just a tiny optimization)

@hgmich hgmich force-pushed the fix-raw-ident-enum-variant branch from 231c0e5 to f72533d Compare April 7, 2025 17:35
@hgmich
Copy link
Copy Markdown
Contributor Author

hgmich commented Apr 7, 2025

Thank you! This looks good to me. Maybe we can add a small test case for it?

Have done now. Originally missed that tests were in the workspace root. I've added a test which covers both field lookup and destructuring via match blocks.

@hgmich hgmich force-pushed the fix-raw-ident-enum-variant branch 2 times, most recently from 6de4d76 to c3ca207 Compare April 7, 2025 17:44
@hgmich hgmich force-pushed the fix-raw-ident-enum-variant branch from c3ca207 to 0c4c78f Compare April 7, 2025 17:46
Copy link
Copy Markdown
Member

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

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

Perfect, thank you very much!

@Icxolu Icxolu added this pull request to the merge queue Apr 7, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 7, 2025
@hgmich
Copy link
Copy Markdown
Contributor Author

hgmich commented Apr 7, 2025

Whoops, looks like I need to consider how to avoid running the pattern matching test on Python versions < 3.10.

@Icxolu
Copy link
Copy Markdown
Member

Icxolu commented Apr 7, 2025

Ah, I think you can gate it with #[cfg(Py_3_10)]

@hgmich
Copy link
Copy Markdown
Contributor Author

hgmich commented Apr 7, 2025

Thanks, I've done that and refactored into separate tests to allow easier gating. Should be good to go now!

@Icxolu Icxolu added this pull request to the merge queue Apr 7, 2025
Merged via the queue into PyO3:main with commit 8acb9a1 Apr 7, 2025
51 of 52 checks passed
davidhewitt pushed a commit that referenced this pull request Apr 11, 2025
* Unraw complex struct enum field python names

Stray case related to #2395 (probably unnoticed because the functionality is
newer and a bit of an edge case).

The `python_name` of struct-type enum variant fields (and the `__match_args__`
of the variant) do not unraw the names of these fields, which effectively
makes them unusable as the resulting identifiers contain '#' which the Python
lexer interprets as comments. The only reason you'd probably want to do this
anyway is to use reserved identifiers (`type` in my case).

* Add newsfragment for fix

* Fix clippy lints in impl_complex_enum_variant_match_args and callers

No need to pass mutable vec ref anymore

* don't overeagerly collect field_names iterator

* add test case for complex enums containing raw identifiers

* only test raw ident pattern matching on Python 3.10+
davidhewitt pushed a commit that referenced this pull request Apr 21, 2025
* Unraw complex struct enum field python names

Stray case related to #2395 (probably unnoticed because the functionality is
newer and a bit of an edge case).

The `python_name` of struct-type enum variant fields (and the `__match_args__`
of the variant) do not unraw the names of these fields, which effectively
makes them unusable as the resulting identifiers contain '#' which the Python
lexer interprets as comments. The only reason you'd probably want to do this
anyway is to use reserved identifiers (`type` in my case).

* Add newsfragment for fix

* Fix clippy lints in impl_complex_enum_variant_match_args and callers

No need to pass mutable vec ref anymore

* don't overeagerly collect field_names iterator

* add test case for complex enums containing raw identifiers

* only test raw ident pattern matching on Python 3.10+
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.

2 participants