Skip to content

Commit

Permalink
pyo3_path, part 4: rename to crate to keep consistent with serde
Browse files Browse the repository at this point in the history
  • Loading branch information
birkenfeld committed Dec 9, 2021
1 parent 21df260 commit cae3783
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add `Py::setattr` method. [#2009](https://github.com/PyO3/pyo3/pull/2009)
- Add `PyCapsule`, exposing the [Capsule API](https://docs.python.org/3/c-api/capsule.html#capsules). [#1980](https://github.com/PyO3/pyo3/pull/1980)
- All PyO3 proc-macros except the deprecated `#[pyproto]` now accept a supplemental attribute `#[pyo3(pyo3_path = "some::path")]` that specifies
- All PyO3 proc-macros except the deprecated `#[pyproto]` now accept a supplemental attribute `#[pyo3(crate = "some::path")]` that specifies
where to find the `pyo3` crate, in case it has been renamed or is re-exported and not found at the crate root. [#2022](https://github.com/PyO3/pyo3/pull/2022)

### Changed
Expand Down
4 changes: 2 additions & 2 deletions guide/src/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ crate.

However, when the dependency is renamed, or your crate only indirectly depends
on `pyo3`, you need to let the macro code know where to find the crate. This is
done with the `pyo3_path` attribute:
done with the `crate` attribute:

```rust
# use pyo3::prelude::*;
# pub extern crate pyo3;
# mod reexported { pub use ::pyo3; }
#[pyclass]
#[pyo3(pyo3_path = "reexported::pyo3")]
#[pyo3(crate = "reexported::pyo3")]
struct MyClass;
```
3 changes: 1 addition & 2 deletions pyo3-macros-backend/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod kw {
syn::custom_keyword!(signature);
syn::custom_keyword!(text_signature);
syn::custom_keyword!(transparent);
syn::custom_keyword!(pyo3_path);
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -50,7 +49,7 @@ pub struct PyO3PathAttribute(pub Path);

impl Parse for PyO3PathAttribute {
fn parse(input: ParseStream) -> Result<Self> {
let _: kw::pyo3_path = input.parse()?;
let _: Token![crate] = input.parse()?;
let _: Token![=] = input.parse()?;
let string_literal: LitStr = input.parse()?;
string_literal.parse().map(PyO3PathAttribute)
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/from_pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl Parse for ContainerPyO3Attribute {
let _: attributes::kw::annotation = input.parse()?;
let _: Token![=] = input.parse()?;
input.parse().map(ContainerPyO3Attribute::ErrorAnnotation)
} else if lookahead.peek(attributes::kw::pyo3_path) {
} else if lookahead.peek(Token![crate]) {
input.parse().map(ContainerPyO3Attribute::PyO3Path)
} else {
Err(lookahead.error())
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl Parse for PyModulePyO3Option {
let lookahead = input.lookahead1();
if lookahead.peek(attributes::kw::name) {
input.parse().map(PyModulePyO3Option::Name)
} else if lookahead.peek(attributes::kw::pyo3_path) {
} else if lookahead.peek(syn::Token![crate]) {
input.parse().map(PyModulePyO3Option::PyO3Path)
} else {
Err(lookahead.error())
Expand Down
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl Parse for PyClassPyO3Option {
let lookahead = input.lookahead1();
if lookahead.peek(attributes::kw::text_signature) {
input.parse().map(PyClassPyO3Option::TextSignature)
} else if lookahead.peek(attributes::kw::pyo3_path) {
} else if lookahead.peek(Token![crate]) {
input.parse().map(PyClassPyO3Option::PyO3Path)
} else {
Err(lookahead.error())
Expand Down
4 changes: 2 additions & 2 deletions pyo3-macros-backend/src/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl Parse for PyFunctionOptions {
if !input.is_empty() {
let _: Comma = input.parse()?;
}
} else if lookahead.peek(attributes::kw::pyo3_path) {
} else if lookahead.peek(syn::Token![crate]) {
// TODO needs duplicate check?
options.pyo3_path = Some(input.parse()?);
} else {
Expand Down Expand Up @@ -292,7 +292,7 @@ impl Parse for PyFunctionOption {
input.parse().map(PyFunctionOption::Signature)
} else if lookahead.peek(attributes::kw::text_signature) {
input.parse().map(PyFunctionOption::TextSignature)
} else if lookahead.peek(attributes::kw::pyo3_path) {
} else if lookahead.peek(syn::Token![crate]) {
input.parse().map(PyFunctionOption::PyO3Path)
} else {
Err(lookahead.error())
Expand Down
4 changes: 2 additions & 2 deletions pyo3-macros-backend/src/pyimpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::collections::HashSet;

use crate::{
attributes::{self, take_pyo3_options, PyO3PathAttribute},
attributes::{take_pyo3_options, PyO3PathAttribute},
konst::{ConstAttributes, ConstSpec},
pyfunction::PyFunctionOptions,
pymethod::{self, is_proto_method},
Expand Down Expand Up @@ -32,7 +32,7 @@ enum PyImplPyO3Option {
impl Parse for PyImplPyO3Option {
fn parse(input: ParseStream) -> Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(attributes::kw::pyo3_path) {
if lookahead.peek(syn::Token![crate]) {
input.parse().map(PyImplPyO3Option::PyO3Path)
} else {
Err(lookahead.error())
Expand Down
8 changes: 4 additions & 4 deletions src/test_hygiene/misc.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#![no_implicit_prelude]

#[derive(crate::FromPyObject)]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
struct Derive1(i32); // newtype case

#[derive(crate::FromPyObject)]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
#[allow(dead_code)]
struct Derive2(i32, i32); // tuple case

#[derive(crate::FromPyObject)]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
#[allow(dead_code)]
struct Derive3 {
f: i32,
g: i32,
} // struct case

#[derive(crate::FromPyObject)]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
#[allow(dead_code)]
enum Derive4 {
A(i32),
Expand Down
6 changes: 3 additions & 3 deletions src/test_hygiene/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#![allow(unused_variables)]

#[crate::pyclass]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
#[derive(::std::clone::Clone)]
pub struct Foo;

#[crate::pyclass]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
pub struct Foo2;

#[crate::pyclass(
Expand All @@ -19,7 +19,7 @@ pub struct Foo2;
extends = crate::types::PyAny,
module = "Spam"
)]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
pub struct Bar {
#[pyo3(get, set)]
a: u8,
Expand Down
2 changes: 1 addition & 1 deletion src/test_hygiene/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(unused_variables)]

#[crate::pyfunction]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
fn do_something(x: i32) -> crate::PyResult<i32> {
::std::result::Result::Ok(x)
}
Expand Down
6 changes: 3 additions & 3 deletions src/test_hygiene/pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#![allow(unused_variables)]

#[crate::pyclass]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
pub struct Dummy;

#[crate::pyclass]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
pub struct DummyIter;

#[crate::pymethods]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
impl Dummy {
//////////////////////
// Basic customization
Expand Down
6 changes: 3 additions & 3 deletions src/test_hygiene/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
#![allow(unused_variables)]

#[crate::pyfunction]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
fn do_something(x: i32) -> crate::PyResult<i32> {
::std::result::Result::Ok(x)
}

#[crate::pymodule]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
fn foo(_py: crate::Python, _m: &crate::types::PyModule) -> crate::PyResult<()> {
::std::result::Result::Ok(())
}

#[crate::pymodule]
#[pyo3(pyo3_path = "crate")]
#[pyo3(crate = "crate")]
fn my_module(_py: crate::Python, m: &crate::types::PyModule) -> crate::PyResult<()> {
m.add_function(crate::wrap_pyfunction!(do_something, m)?)?;
m.add_wrapped(crate::wrap_pymodule!(foo))?;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/invalid_frompy_derive.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ error: only one of `attribute` or `item` can be provided
118 | #[pyo3(item, attribute)]
| ^

error: expected one of: `transparent`, `annotation`, `pyo3_path`
error: expected one of: `transparent`, `annotation`, `crate`
--> tests/ui/invalid_frompy_derive.rs:123:8
|
123 | #[pyo3(unknown = "should not work")]
Expand Down

0 comments on commit cae3783

Please sign in to comment.