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

Implement eyre feature #1893

Merged
merged 11 commits into from Oct 13, 2021
Merged

Implement eyre feature #1893

merged 11 commits into from Oct 13, 2021

Conversation

mejrs
Copy link
Member

@mejrs mejrs commented Sep 29, 2021

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks great! Should we merge this at the same time as #1822 ?

Cargo.toml Outdated
Comment on lines 19 to 20
# must stay at 0.3.x for Rust 1.41 compatibility
eyre = {version = ">= 0.4" , optional = true}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think these need to be swapped (new dep has split comment from its target). Also, should we place an upper bound?

Suggested change
# must stay at 0.3.x for Rust 1.41 compatibility
eyre = {version = ">= 0.4" , optional = true}
eyre = {version = ">= 0.4, <0.5" , optional = true}
# must stay at 0.3.x for Rust 1.41 compatibility

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(oops)

Eyre is at 0.6.5 now, so that upper bound is too strict. 0.4 is the first version that has the eyre::Report type (?), so I think any version above that should be fine.

I'll admit that I don't know anything about how cargo resolves dependency versions, but this looks like how you should say "works with any eyre version of 0.4 and up". Is that too liberal and should it be pinned to ">= 0.4, <0.7" instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that by default cargo will pick the highest eyre possible for the pyo3 dependency, not the one which the user already has in their own Cargo.toml. (This is a bit awkward.) The user can adjust it manually using some incantation of cargo update --precise (like we do in our MSRV CI jobs).

Is that too liberal and should it be pinned to ">= 0.4, <0.7" instead?

Generally we've only offered a range for dependencies when the MSRV support requires an older version. I'd be tempted to ask the other question - is just supporting eyre = "0.6" acceptable?

As for the upper bound: if we were to take semver literally, eyre 0.7 could hypothetically be very breaking and completely remove the eyre::Report type. (Although I would be suprised if this happened in practice.) So I'd definitely prefer have an upper bound of < 0.7 if we go for a range. Dependabot can remind us to update when 0.7 releases 😄

Comment on lines 45 to 52
//! // A wrapper around a Rust function.
//! #[pyfunction]
//! fn py_open(filename: PathBuf) -> PyResult<Vec<u8>> {
//! // The `?` ("try") operator performs the conversion
//! // into a `PyErr` - if it is an error.
//! let data = rust_open(filename)?;
//! Ok(data)
//! }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be possible to also just have a #[pyfunction] which returns eyre::Result<Vec<u8>> - which might be useful to have as an example (as a kind of test)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about doing that but I want to point at the where the conversion happens. A #[pyfunction] returning eyre::Result<Vec<u8>> has that conversion hidden somewhere in the pyfunction macro.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair point. I wonder if your second example "using eyre in general" could be tweaked slightly so that the function decompress is a pyfunction returning eyre::Result? The conversion from PyErr -> eyre::Result can still be there.

Alternatively maybe it's better to keep away from that here, and instead in the guide's function.md there is probably scope for an improved "error handling" section which comments on how to use Result with error types that convert to PyErr. (And can use Result<_, eyre::Report> as an example?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I do want a pyfunction returning eyre's error. I think it is too easy for readers to assume that a pyfunction must return a PyResult and they have to wrap functions. I'm not sure if that was your reasoning though.

I do want to expand the error handling in the guide, but not with this PR.

@davidhewitt
Copy link
Member

Oh, also this is definitely worth a CHANGELOG line :)

CHANGELOG.md Outdated
- Add `abi3-py310` feature. [#1889](https://github.com/PyO3/pyo3/pull/1889)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonus newline!

Suggested change

CHANGELOG.md Outdated
@@ -22,8 +22,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `PyMapping` type to represent the Python mapping protocol. [#1844](https://github.com/PyO3/pyo3/pull/1844)
- Add commonly-used sequence methods to `PyList` and `PyTuple`. [#1849](https://github.com/PyO3/pyo3/pull/1849)
- Add `as_sequence` methods to `PyList` and `PyTuple`. [#1860](https://github.com/PyO3/pyo3/pull/1860)
- Add `eyre` feature to convert `eyre::report` into `PyErr`. [#1893](https://github.com/PyO3/pyo3/pull/1893)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Add `eyre` feature to convert `eyre::report` into `PyErr`. [#1893](https://github.com/PyO3/pyo3/pull/1893)
- Add `eyre` feature to convert `eyre::Report` into `PyErr`. [#1893](https://github.com/PyO3/pyo3/pull/1893)

Also for dependencies I quite like to have something in the Packaging section along the lines of "added optional eyre dependency" - what do you think of having that as well (or instead)?

Cargo.toml Outdated
Comment on lines 19 to 20
# must stay at 0.3.x for Rust 1.41 compatibility
eyre = {version = ">= 0.4" , optional = true}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that by default cargo will pick the highest eyre possible for the pyo3 dependency, not the one which the user already has in their own Cargo.toml. (This is a bit awkward.) The user can adjust it manually using some incantation of cargo update --precise (like we do in our MSRV CI jobs).

Is that too liberal and should it be pinned to ">= 0.4, <0.7" instead?

Generally we've only offered a range for dependencies when the MSRV support requires an older version. I'd be tempted to ask the other question - is just supporting eyre = "0.6" acceptable?

As for the upper bound: if we were to take semver literally, eyre 0.7 could hypothetically be very breaking and completely remove the eyre::Report type. (Although I would be suprised if this happened in practice.) So I'd definitely prefer have an upper bound of < 0.7 if we go for a range. Dependabot can remind us to update when 0.7 releases 😄

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, all looks really good. Sorry for the delay - I was ill over the weekend. Feeling a bit more capable this morning finally!

Just commented on the remaining points with some thoughts. All is great as-is though so also happy if you want to call this done for now and merge it!

//! let text = decompress(bytes)?;
//!
//! println!("The text is \"{}\"", text);
//! # assert_eq!(text, "You should probably use the libflate crate instead.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂

Comment on lines 45 to 52
//! // A wrapper around a Rust function.
//! #[pyfunction]
//! fn py_open(filename: PathBuf) -> PyResult<Vec<u8>> {
//! // The `?` ("try") operator performs the conversion
//! // into a `PyErr` - if it is an error.
//! let data = rust_open(filename)?;
//! Ok(data)
//! }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair point. I wonder if your second example "using eyre in general" could be tweaked slightly so that the function decompress is a pyfunction returning eyre::Result? The conversion from PyErr -> eyre::Result can still be there.

Alternatively maybe it's better to keep away from that here, and instead in the guide's function.md there is probably scope for an improved "error handling" section which comments on how to use Result with error types that convert to PyErr. (And can use Result<_, eyre::Report> as an example?)

@mejrs mejrs marked this pull request as draft October 8, 2021 00:42
@mejrs mejrs marked this pull request as ready for review October 13, 2021 12:26
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks! Feel free to merge this at your will (at the same time as finishing up #1822?)

CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
@mejrs
Copy link
Member Author

mejrs commented Oct 13, 2021

at the same time as finishing up #1822?

I don't see a reason to let this sit around here. I think we'd still want to have them both in 0.15 though.

@davidhewitt davidhewitt enabled auto-merge (squash) October 13, 2021 22:19
@davidhewitt davidhewitt merged commit ae87369 into PyO3:main Oct 13, 2021
@davidhewitt davidhewitt mentioned this pull request Oct 14, 2021
2 tasks
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.

None yet

2 participants