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

Implement get/set all on pyclass #2692

Merged
merged 3 commits into from
Oct 18, 2022
Merged

Implement get/set all on pyclass #2692

merged 3 commits into from
Oct 18, 2022

Conversation

mejrs
Copy link
Member

@mejrs mejrs commented Oct 17, 2022

Fixes #1003 and most of #780

I'm happy to bikeshed the naming; I'm not sure whether I prefer get_all or just get.

@birkenfeld
Copy link
Member

LGTM, I think get_all is better than get.

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! I also agree with get_all/set_all being better than get/set.

Will need newsfragments/2692.added.md please :)

Comment on lines 256 to 268
for (_, FieldPyO3Options { get, .. }) in &mut field_options {
if mem::replace(get, true) {
return Err(syn::Error::new(attr.span(), DUPE_GET));
}
}
}

if let Some(attr) = args.options.set_all {
for (_, FieldPyO3Options { set, .. }) in &mut field_options {
if mem::replace(set, true) {
return Err(syn::Error::new(attr.span(), DUPE_SET));
}
}
Copy link
Member

Choose a reason for hiding this comment

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

IMO the spans for the errors here aren't ideal because they use the span of get_all and set_all, but the error is describing the get and set as the source of the error.

Using the syn::Field span (bound as _ in these snippets) may be better as it'll point to the field which has the invalid attribute?

The ideal span would probably be the get attribute itself - you could convert the FieldPyO3Options to have get: Option<kw::get>, and then use those spans. It would however mean that your elegant solution of using mem::replace here wouldn't be quite enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good idea, done.

Comment on lines 1122 to 1123
const DUPE_SET: &str = "duplicate `set` - the struct is already annotated with `set_all`";
const DUPE_GET: &str = "duplicate `get` - the struct is already annotated with `get_all`";
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think I'd use "useless" or "redundant" rather than duplicate?

Suggested change
const DUPE_SET: &str = "duplicate `set` - the struct is already annotated with `set_all`";
const DUPE_GET: &str = "duplicate `get` - the struct is already annotated with `get_all`";
const DUPE_SET: &str = "useless `set` - the struct is already annotated with `set_all`";
const DUPE_GET: &str = "useless `get` - the struct is already annotated with `get_all`";

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, thanks very much! 👍

@davidhewitt davidhewitt merged commit 446c0e8 into PyO3:main Oct 18, 2022
@joshparallel
Copy link

joshparallel commented Dec 5, 2022

Thank you for implementing this! Any idea when it'll make it to crates.io? Placing PyO3 attributes behind features is pretty essential for something I'm working on so I'm eager to use this.

@mejrs
Copy link
Member Author

mejrs commented Dec 6, 2022

See #2785 I think there's a few things we want to merge before the next release.

Also see #2786 - if this PR is useful for you then that one might also be.

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.

pyo3(get, set) attribute not found when using cfg_attr
4 participants