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

#[pyfunction] inside #[pymethods] could emit a better error #4340

Closed
davidhewitt opened this issue Jul 12, 2024 · 0 comments · Fixed by #4349
Closed

#[pyfunction] inside #[pymethods] could emit a better error #4340

davidhewitt opened this issue Jul 12, 2024 · 0 comments · Fixed by #4349

Comments

@davidhewitt
Copy link
Member

Consider this code:

#[pyo3::pymodule]
mod pyo3_scratch {
    use pyo3::prelude::*;

    #[pyclass]
    struct Foo {}

    #[pymethods]
    impl Foo {
        #[pyfunction]
        fn bug() {}
    }
}

The #[pyfunction] inside #[pymethods] is an innocent user error that I've seen by folks new to PyO3. At the moment this creates a load of confusing garbage errors (due to the #[pyfunction] implementation details):

Click to show error details
error: module is not supported in `trait`s or `impl`s
  --> src/lib.rs:10:9
   |
10 |         #[pyfunction]
   |         ^^^^^^^^^^^^^
   |
   = help: consider moving the module out to a nearby module scope
   = note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

error: implementation is not supported in `trait`s or `impl`s
  --> src/lib.rs:10:9
   |
10 |         #[pyfunction]
   |         ^^^^^^^^^^^^^
   |
   = help: consider moving the implementation out to a nearby module scope
   = note: this error originates in the attribute macro `pyfunction` (in Nightly builds, run with -Z macro-backtrace for more info)

error: static method needs #[staticmethod] attribute
  --> src/lib.rs:11:9
   |
11 |         fn bug() {}
   |         ^^

error[E0425]: cannot find value `bug` in this scope
  --> src/lib.rs:11:12
   |
11 |         fn bug() {}
   |            ^^^
   |            |
   |            an associated function by that name is available on `Self` here
   |            not found in this scope

For more information about this error, try `rustc --explain E0425`.

We should be able to detect the #[pyfunction] attribute the same way declarative modules detect #[pyclass] / #[pyfunction] / #[pymodule] attributes. In this case, we should emit a helpful error like "functions inside #[pymethods] do not need to be annotated with #[pyfunction]".

I think this is a good first issue for someone wanting to improve the macro code.

csernazs added a commit to csernazs/pyo3 that referenced this issue Jul 14, 2024
csernazs added a commit to csernazs/pyo3 that referenced this issue Jul 14, 2024
Make error message more specific when `#[pyfunction]` is used in
`#[pymethods]`.

Effectively, this replaces the error message:

```
error: static method needs #[staticmethod] attribute
```

To:
```
functions inside #[pymethods] do not need to be annotated with #[pyfunction]
```

Fixes PyO3#4340

Co-authored-by: László Vaskó <[email protected]>
csernazs added a commit to csernazs/pyo3 that referenced this issue Jul 14, 2024
Make error message more specific when `#[pyfunction]` is used in
`#[pymethods]`.

Effectively, this replaces the error message:

```
error: static method needs #[staticmethod] attribute
```

To:
```
functions inside #[pymethods] do not need to be annotated with #[pyfunction]
```

Fixes PyO3#4340

Co-authored-by: László Vaskó <[email protected]>
csernazs added a commit to csernazs/pyo3 that referenced this issue Jul 14, 2024
Make error message more specific when `#[pyfunction]` is used in
`#[pymethods]`.

Effectively, this replaces the error message:

```
error: static method needs #[staticmethod] attribute
```

To:
```
functions inside #[pymethods] do not need to be annotated with #[pyfunction]
```

Fixes PyO3#4340

Co-authored-by: László Vaskó <[email protected]>
csernazs added a commit to csernazs/pyo3 that referenced this issue Jul 14, 2024
Make error message more specific when `#[pyfunction]` is used in
`#[pymethods]`.

Effectively, this replaces the error message:

```
error: static method needs #[staticmethod] attribute
```

To:
```
functions inside #[pymethods] do not need to be annotated with #[pyfunction]
```

...and also removes the other misleading error messages to the function in
question.

Fixes PyO3#4340

Co-authored-by: László Vaskó <[email protected]>
csernazs added a commit to csernazs/pyo3 that referenced this issue Jul 16, 2024
Make error message more specific when `#[pyfunction]` is used in
`#[pymethods]`.

Effectively, this replaces the error message:

```
error: static method needs #[staticmethod] attribute
```

To:
```
functions inside #[pymethods] do not need to be annotated with #[pyfunction]
```

...and also removes the other misleading error messages to the function in
question.

Fixes PyO3#4340

Co-authored-by: László Vaskó <[email protected]>
github-merge-queue bot pushed a commit that referenced this issue Jul 24, 2024
…4349)

* Improve error messages for #[pyfunction] defined inside #[pymethods]

Make error message more specific when `#[pyfunction]` is used in
`#[pymethods]`.

Effectively, this replaces the error message:

```
error: static method needs #[staticmethod] attribute
```

To:
```
functions inside #[pymethods] do not need to be annotated with #[pyfunction]
```

...and also removes the other misleading error messages to the function in
question.

Fixes #4340

Co-authored-by: László Vaskó <[email protected]>

* review fixes

---------

Co-authored-by: László Vaskó <[email protected]>
davidhewitt pushed a commit that referenced this issue Sep 3, 2024
…4349)

* Improve error messages for #[pyfunction] defined inside #[pymethods]

Make error message more specific when `#[pyfunction]` is used in
`#[pymethods]`.

Effectively, this replaces the error message:

```
error: static method needs #[staticmethod] attribute
```

To:
```
functions inside #[pymethods] do not need to be annotated with #[pyfunction]
```

...and also removes the other misleading error messages to the function in
question.

Fixes #4340

Co-authored-by: László Vaskó <[email protected]>

* review fixes

---------

Co-authored-by: László Vaskó <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant