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

Add rename_all attribute to #[pyclass] #3384

Merged
merged 5 commits into from
Aug 16, 2023

Conversation

DataTriny
Copy link
Contributor

Closes #3383

@DataTriny
Copy link
Contributor Author

I'll try to update the rename_all attribute so that it accepts a case as argument and applies to struct fields as well, if there is interest in having this. Thanks for your feedback.

@DataTriny DataTriny force-pushed the pyclass_rename_variants branch 2 times, most recently from 4f41e79 to 928c3f9 Compare August 14, 2023 22:29
@DataTriny
Copy link
Contributor Author

@adamreichold Ready for another review.

Copy link
Member

@adamreichold adamreichold 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 for going the extra mile and making this fully generic! I think this is now a powerful helper that can save quite a bit of boiler plate for many people.

@DataTriny
Copy link
Contributor Author

codecov is failing again... I think I added enough tests and it can be ignored right?

@adamreichold
Copy link
Member

codecov is failing again... I think I added enough tests and it can be ignored right?

Yes, I think so too. The only reason I did not merge this yet is to give the other maintainers some time to chime in on this version of the code.

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.

Looks great to me, thanks for the solid implementation.

I just have one suggestion on an addition to the test suite (which will drive the coverage just a teeny bit higher too), otherwise I'm happy to see this merged whenever.

Comment on lines +186 to +200
use pyo3::types::PyBool;

Python::with_gil(|py| {
let struct_class = py.get_type::<StructWithRenamedFields>();
let struct_obj = struct_class.call0().unwrap();
assert!(struct_obj
.setattr("firstField", PyBool::new(py, false))
.is_ok());
py_assert!(py, struct_obj, "struct_obj.firstField == False");
py_assert!(py, struct_obj, "struct_obj.secondField == 5");
assert!(struct_obj
.setattr("third_field", PyBool::new(py, true))
.is_ok());
py_assert!(py, struct_obj, "struct_obj.third_field == True");
});
Copy link
Member

Choose a reason for hiding this comment

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

It would be quite nice to have some kind of macro here which can test each type of rename_all in turn, so that we test they all work as expected.

test_case!(CamelCase, "camelCase", "ab_cd-ef", "abCdEf");
test_case!(SnakeCase, "snake_case", "ab_cd-ef", "ab_cd_ef");

etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a macro which I think does the job. At least codecov likes it.

@adamreichold adamreichold added this pull request to the merge queue Aug 16, 2023
Merged via the queue into PyO3:main with commit 9363491 Aug 16, 2023
31 checks passed
@DataTriny DataTriny deleted the pyclass_rename_variants branch August 16, 2023 19:55
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.

Support renaming enum variants
3 participants