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

Some Improvments for getter #137

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

zao111222333
Copy link
Contributor

Hi,
As the conversation here.
This PR contains following updates. Since I introduce #[gen_stub(xxx)] attribute, I will update the document/readme when those APIs are stabilized.

1. add indexmap feature

2. propagate getter's documents to class member, e.g.,

#[gen_stub_pyclass]
#[pyclass]
struct A {
    /// Class variable `x`
    #[pyo3(get, set)]
    x: usize,
}

or

#[gen_stub_pyclass]
#[pyclass]
struct A {
    x: usize,
}
#[gen_stub_pymethods]
#[pymethods]
impl A {
    /// Class variable `x`
    fn get_x(&self) -> usize {
        self.x
    }
}

will get the output:

class A:
    x: int
    r"""
    Class variable `x`
    """

3. support #[gen_stub(default=xx)]

User specified default value for getter's class member, e.g.,

#[pyclass]
#[stub_gen_pyclass]
struct A {
    #[pyo3(get,set)]
    #[stub_gen(default = A::default().x)]
    x: usize,
}
impl Default for A {
    fn default() -> A {
        Self { x: 2 }
    }
}

or

#[gen_stub_pyclass]
#[pyclass]
struct A {
    x: usize,
}
impl Default for A {
    fn default() -> A {
        Self { x: 2 }
    }
}
#[gen_stub_pymethods]
#[pymethods]
impl A {
    #[stub_gen(default = A::default().x)]
    fn get_x(&self) -> usize {
        self.x
    }
}

Output:

class A:
    x: int = 2

4. support #[gen_stub(skip)]

User specified, ignore a function in #[pymethods], e.g.,

#[gen_stub_pymethods]
#[pymethods]
impl A {
    #[new]
    fn new(x: usize) -> Self {
        Self { x }
    }
    #[gen_stub(skip)]
    fn need_skip(&self) {}

Output:

class A:
    def __new__(cls,x:int): ...

@zao111222333
Copy link
Contributor Author

BTW, I believe #[gen_stub(skip)] matters becase the fn set_x will show in .pyi, that is a bit ugly :)

@konn
Copy link
Collaborator

konn commented Jan 17, 2025

Thank you for contributions. It takes some time to take a review this PR, but please consider the following:

  1. As index-map related change seems orthogonal to getters-related ones and introduces new dependency, please separate this as another PR.
  2. As I asked here, would you elaborate the relation of getter documents to PR Google style docs for #70 #96 ?

@zao111222333
Copy link
Contributor Author

zao111222333 commented Jan 17, 2025

Thank you for taking time to review my code :)

  1. As index-map related change seems orthogonal to getters-related ones and introduces new dependency, please separate this as another PR.

Yes, I will do that. And the all indexmap's change are in the first commit of this PR, I hope it won't influence your viewing the getter's changes. I will remove them and create a new PR for that.

  1. As I asked here, would you elaborate the relation of getter documents to PR Google style docs for #70 #96 ?

I think #70 want to collect all fields' docs and generate them into class document. While in this PR, I only want the field's doc, which seems more basic and useful.
But I think we can append those Google style attributes docs into class document in the further, by a macro's setting like

#[gen_stub_pyclass]
#[pyclass]
#[gen_stub(attribute_doc)]
struct A {
    ...
}

Or turn on attributes docs for default, and use #[gen_stub(no_attribute_doc)] to turn off that.

@zao111222333
Copy link
Contributor Author

zao111222333 commented Jan 17, 2025

For #133, if you are willing I can fix that in this PR as well. Here is what I consider:
When both getter and setter are specified for a field, the .pyi should only have one class property, that seems easy to handle by using HashSet or something like that.
The issue will be complex when we want to propagate the rust field's document to .pyi class properties document, we should use whose document? from getter, setter, or both?

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