-
Notifications
You must be signed in to change notification settings - Fork 940
Allows custom signatures for #[getter] and #[setter] in order to provide type hints #5738
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
Open
varchasgopalaswamy
wants to merge
8
commits into
PyO3:main
Choose a base branch
from
varchasgopalaswamy:introspection-allow-property-signature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e76816f
relax requirements on signature overrides
varchasgopalaswamy 73f9ae0
Merge branch 'PyO3:main' into introspection-allow-property-signature
varchasgopalaswamy 2b6633c
disable text sig
varchasgopalaswamy a6b7d90
Merge branch 'introspection-allow-property-signature' of https://gith…
varchasgopalaswamy 18f5217
added a test for manual signature generation for getters and setters,…
varchasgopalaswamy 63fcc50
added check to make sure automatic stub generation still works with g…
varchasgopalaswamy ab00724
added tests for bad signature specifications for getters and setters
varchasgopalaswamy ef664f2
better checks and messaging
varchasgopalaswamy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -204,6 +204,39 @@ fn map_a_class(cls: AClass) -> AClass { | |||||||||||||||||||||||||||||
| cls | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #[pyclass] | ||||||||||||||||||||||||||||||
| struct ClassWithCustomGetterSetterSignature { | ||||||||||||||||||||||||||||||
| foo: usize, | ||||||||||||||||||||||||||||||
| bar: usize, | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| #[pymethods] | ||||||||||||||||||||||||||||||
|
Comment on lines
+207
to
+212
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||
| impl ClassWithCustomGetterSetterSignature { | ||||||||||||||||||||||||||||||
| #[new] | ||||||||||||||||||||||||||||||
| fn new() -> Self { | ||||||||||||||||||||||||||||||
| Self { foo: 0, bar: 10 } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| #[getter] | ||||||||||||||||||||||||||||||
| #[pyo3(signature = () -> "int")] | ||||||||||||||||||||||||||||||
| fn get_foo(&self) -> usize { | ||||||||||||||||||||||||||||||
| self.foo | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #[setter] | ||||||||||||||||||||||||||||||
| #[pyo3(signature = (value:"int"))] | ||||||||||||||||||||||||||||||
| fn set_foo(&mut self, value: usize) { | ||||||||||||||||||||||||||||||
| self.foo = value; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #[getter] | ||||||||||||||||||||||||||||||
| fn get_bar(&self) -> usize { | ||||||||||||||||||||||||||||||
| self.bar | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| #[setter] | ||||||||||||||||||||||||||||||
| fn set_bar(&mut self, value: usize) { | ||||||||||||||||||||||||||||||
| self.bar = value; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #[pymodule] | ||||||||||||||||||||||||||||||
| pub mod pyclasses { | ||||||||||||||||||||||||||||||
| #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] | ||||||||||||||||||||||||||||||
|
|
@@ -214,7 +247,7 @@ pub mod pyclasses { | |||||||||||||||||||||||||||||
| use super::SubClassWithInit; | ||||||||||||||||||||||||||||||
| #[pymodule_export] | ||||||||||||||||||||||||||||||
| use super::{ | ||||||||||||||||||||||||||||||
| map_a_class, AssertingBaseClass, ClassWithDecorators, ClassWithoutConstructor, EmptyClass, | ||||||||||||||||||||||||||||||
| PlainObject, PyClassIter, PyClassThreadIter, | ||||||||||||||||||||||||||||||
| map_a_class, AssertingBaseClass, ClassWithCustomGetterSetterSignature, ClassWithDecorators, | ||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll need to move this to a separate |
||||||||||||||||||||||||||||||
| ClassWithoutConstructor, EmptyClass, PlainObject, PyClassIter, PyClassThreadIter, | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| use pyo3::prelude::*; | ||
|
|
||
| #[pyclass] | ||
| struct ClassWithBadGetterSignature { | ||
| foo: usize, | ||
| bar: usize, | ||
| } | ||
| #[pymethods] | ||
| impl ClassWithBadGetterSignature { | ||
| #[getter] | ||
| #[pyo3(signature = (extra_arg:"int"))] | ||
| fn get_foo(&self) -> usize { | ||
| self.foo | ||
| } | ||
| } | ||
|
|
||
| #[pyclass] | ||
| struct ClassWithMismatchedSetterSignature { | ||
| foo: usize, | ||
| bar: usize, | ||
| } | ||
| #[pymethods] | ||
| impl ClassWithMismatchedSetterSignature { | ||
| #[getter] | ||
| #[pyo3(signature = (extra_arg:"int"))] | ||
| fn set_foo(&mut self, value: usize) { | ||
| self.foo = value; | ||
| } | ||
| } | ||
|
|
||
| #[pyclass] | ||
| struct ClassWithMissingSetterSignature { | ||
| foo: usize, | ||
| bar: usize, | ||
| } | ||
| #[pymethods] | ||
| impl ClassWithMissingSetterSignature { | ||
| #[getter] | ||
| #[pyo3(signature = ())] | ||
| fn set_foo(&mut self, value: usize) { | ||
| self.foo = value; | ||
| } | ||
| } | ||
|
|
||
| #[pyclass] | ||
| struct ClassWithExtraSetterSignature { | ||
| foo: usize, | ||
| bar: usize, | ||
| } | ||
| #[pymethods] | ||
| impl ClassWithExtraSetterSignature { | ||
| #[getter] | ||
| #[pyo3(signature = (value:"int", extra_arg:"int"))] | ||
| fn set_foo(&mut self, value: usize) { | ||
| self.foo = value; | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| error: signature entry does not have a corresponding function argument | ||
| --> tests/ui/invalid_getter_setter_signatures.rs:11:25 | ||
| | | ||
| 11 | #[pyo3(signature = (extra_arg:"int"))] | ||
| | ^^^^^^^^^ | ||
|
|
||
| error: expected argument from function definition `value` but got argument `extra_arg` | ||
| --> tests/ui/invalid_getter_setter_signatures.rs:25:25 | ||
| | | ||
| 25 | #[pyo3(signature = (extra_arg:"int"))] | ||
| | ^^^^^^^^^ | ||
|
|
||
| error: missing signature entry for argument `value` | ||
| --> tests/ui/invalid_getter_setter_signatures.rs:39:12 | ||
| | | ||
| 39 | #[pyo3(signature = ())] | ||
| | ^^^^^^^^^ | ||
|
|
||
| error: signature entry does not have a corresponding function argument | ||
| --> tests/ui/invalid_getter_setter_signatures.rs:53:38 | ||
| | | ||
| 53 | #[pyo3(signature = (value:"int", extra_arg:"int"))] | ||
| | ^^^^^^^^^ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I think this makes the development process easier for us, happy to add these depedencies.