Skip to content

Commit

Permalink
DEP: add deprecation warning for raise_if_err()
Browse files Browse the repository at this point in the history
  • Loading branch information
mplanchard committed Jan 12, 2020
1 parent 0cc370d commit 58f83ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/safetywrap/_impl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementations of Ok, Err, Some, and None."""

import typing as t
import warnings

from ._interface import _Option, _Result

Expand Down Expand Up @@ -464,6 +465,11 @@ def raise_if_err(
Alias of `Some.expect`.
"""
warnings.warn(
"Option.raise_if_err() is deprecated. "
"Use raise_if_nothing() or expect() instead",
DeprecationWarning,
)
return self.expect(msg, exc_cls=exc_cls)

def raise_if_nothing(
Expand Down Expand Up @@ -640,6 +646,11 @@ def raise_if_err(
Alias of `Nothing.expect`.
"""
warnings.warn(
"Option.raise_if_err() is deprecated. "
"Use raise_if_nothing() or expect() instead",
DeprecationWarning,
)
return self.expect(msg, exc_cls=exc_cls)

def raise_if_nothing(
Expand Down
16 changes: 10 additions & 6 deletions tests/test_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ def test_or_else(
"""Chains option-generating functions if results are `None`."""
assert start.or_else(fn) == exp

@pytest.mark.parametrize(
"method", ("expect", "raise_if_err", "raise_if_nothing")
)
@pytest.mark.parametrize("method", ("expect", "raise_if_nothing"))
@pytest.mark.parametrize("exc_cls", (None, IOError))
def test_expect_and_aliases_raising(
self, method: str, exc_cls: t.Type[Exception]
Expand All @@ -167,13 +165,19 @@ def test_expect_and_aliases_raising(

assert msg in str(exc_info.value)

@pytest.mark.parametrize(
"method", ("expect", "raise_if_err", "raise_if_nothing")
)
@pytest.mark.parametrize("method", ("expect", "raise_if_nothing"))
def test_expect_and_aliases_not_raising(self, method: str) -> None:
"""Expecting on a Some() returns the value."""
assert getattr(Some("hello"), method)("not what I expected") == "hello"

def test_raise_if_err(self) -> None:
"""This method is deprecated."""
with pytest.deprecated_call():
assert Some("hello").raise_if_err("error") == "hello"
with pytest.deprecated_call():
with pytest.raises(RuntimeError):
Nothing().raise_if_err("error")

@pytest.mark.parametrize(
"start, exp",
((Nothing(), Nothing()), (Some(3), Nothing()), (Some(4), Some(4))),
Expand Down

0 comments on commit 58f83ac

Please sign in to comment.