Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ignore syn::Type::Group in is_python #1505

Merged
merged 2 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fix incorrect `TypeError` raised when keyword-only argument passed along with a positional argument in `*args`. [#1440](https://github.com/PyO3/pyo3/pull/1440)
- Fix inability to use a named lifetime for `&PyTuple` of `*args` in `#[pyfunction]`. [#1440](https://github.com/PyO3/pyo3/pull/1440)
- Fix inability to add `#[text_signature]` to some `#[pyproto]` methods. [#1483](https://github.com/PyO3/pyo3/pull/1483)
- Fix use of Python argument for #[pymethods] inside macro expansions. [#1505](https://github.com/PyO3/pyo3/pull/1505)

## [0.13.2] - 2021-02-12
### Packaging
Expand Down
6 changes: 5 additions & 1 deletion pyo3-macros-backend/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ macro_rules! ensure_spanned {
}

/// Check if the given type `ty` is `pyo3::Python`.
pub fn is_python(ty: &syn::Type) -> bool {
pub fn is_python(mut ty: &syn::Type) -> bool {
while let syn::Type::Group(group) = ty {
// Macros can create invisible delimiters around types.
ty = &*group.elem;
}
match ty {
syn::Type::Path(typath) => typath
.path
Expand Down
26 changes: 26 additions & 0 deletions tests/test_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,3 +727,29 @@ fn test_raw_idents() {
);
})
}

// Regression test for issue 1505 - Python argument not detected correctly when inside a macro.

#[pyclass]
struct Issue1505 {}

macro_rules! issue_1505 {
(
#[pymethods]
impl $ty: ty {
fn $fn:ident (&self, $arg:ident : $arg_ty:ty) {}
}
) => {
#[pymethods]
impl $ty {
fn $fn(&self, $arg: $arg_ty) {}
}
};
}

issue_1505!(
#[pymethods]
impl Issue1505 {
fn issue_1505(&self, _py: Python<'_>) {}
}
);