Unraw complex struct enum field python names#5050
Conversation
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).
| 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(); |
There was a problem hiding this comment.
Not quite sure this is the best way to do this. Feedback appreciated.
There was a problem hiding this comment.
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)
No need to pass mutable vec ref anymore
Icxolu
left a comment
There was a problem hiding this comment.
Thank you! This looks good to me. Maybe we can add a small test case for it?
| 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(); |
There was a problem hiding this comment.
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)
231c0e5 to
f72533d
Compare
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 |
6de4d76 to
c3ca207
Compare
c3ca207 to
0c4c78f
Compare
|
Whoops, looks like I need to consider how to avoid running the pattern matching test on Python versions < 3.10. |
|
Ah, I think you can gate it with |
|
Thanks, I've done that and refactored into separate tests to allow easier gating. Should be good to go now! |
* 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+
* 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+
Stray case related to #2395 (probably unnoticed because the functionality is newer and a bit of an edge case).
The
python_nameof 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 (typein my case).