Skip to content

Commit

Permalink
FIX: resolve pylint assignment-from-no-return errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mplanchard committed Feb 21, 2020
1 parent 4287807 commit ac20009
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix pylint `assignment-from-no-return` warnings for methods that can only
raise, seen when pylint can determine whether a value is an Ok/Err or
Some/Nothing and you try to e.g. `Err(5).expect("no good")`.

## [1.3.0] - 2019-01-12

### Added
Expand Down
24 changes: 24 additions & 0 deletions src/safetywrap/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ def expect_err(
keyword argument.
"""
raise exc_cls(f"{msg}: {self._value}")
# Hack: pylint will warn that you're assigning from a function
# that doesn't return if there isn't at least one return statement
# in a function
return self._value # pylint: disable=unreachable

def is_err(self) -> bool:
"""Returl whether the result is an Err."""
Expand Down Expand Up @@ -229,6 +233,10 @@ def unwrap(self) -> T:
def unwrap_err(self) -> E:
"""Return an Ok result, or throw an error if an Err."""
raise RuntimeError(f"Tried to unwrap_err {self}!")
# Hack: pylint will warn that you're assigning from a function
# that doesn't return if there isn't at least one return statement
# in a function
return self._value # pylint: disable=unreachable

def unwrap_or(self, alternative: U) -> t.Union[T, U]:
"""Return the `Ok` value, or `alternative` if `self` is `Err`."""
Expand Down Expand Up @@ -317,6 +325,10 @@ def expect(self, msg: str, exc_cls: t.Type[Exception] = RuntimeError) -> T:
keyword argument.
"""
raise exc_cls(f"{msg}: {self._value}")
# Hack: pylint will warn that you're assigning from a function
# that doesn't return if there isn't at least one return statement
# in a function
return self._value # pylint: disable=unreachable

def raise_if_err(
self, msg: str, exc_cls: t.Type[Exception] = RuntimeError
Expand Down Expand Up @@ -366,6 +378,10 @@ def map_err(self, fn: t.Callable[[E], F]) -> "Result[T, F]":
def unwrap(self) -> T:
"""Return an Ok result, or throw an error if an Err."""
raise RuntimeError(f"Tried to unwrap {self}!")
# Hack: pylint will warn that you're assigning from a function
# that doesn't return if there isn't at least one return statement
# in a function
return self._value # pylint: disable=unreachable

def unwrap_err(self) -> E:
"""Return an Ok result, or throw an error if an Err."""
Expand Down Expand Up @@ -633,6 +649,10 @@ def expect(self, msg: str, exc_cls: t.Type[Exception] = RuntimeError) -> T:
argument.
"""
raise exc_cls(msg)
# Hack: pylint will warn that you're assigning from a function
# that doesn't return if there isn't at least one return statement
# in a function
return self._value # pylint: disable=unreachable

def raise_if_err(
self, msg: str, exc_cls: t.Type[Exception] = RuntimeError
Expand Down Expand Up @@ -722,6 +742,10 @@ def ok_or_else(self, err_fn: t.Callable[[], E]) -> Result[T, E]:
def unwrap(self) -> T:
"""Return `Some` value, or raise an error."""
raise RuntimeError("Tried to unwrap Nothing")
# Hack: pylint will warn that you're assigning from a function
# that doesn't return if there isn't at least one return statement
# in a function
return self._value # pylint: disable=unreachable

def unwrap_or(self, default: U) -> t.Union[T, U]:
"""Return the contained value or `default`."""
Expand Down

0 comments on commit ac20009

Please sign in to comment.