Skip to content

Commit

Permalink
Add mypy type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrgn committed Feb 21, 2020
1 parent 8130af0 commit f218cac
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ script:
- pip install -e .
- py.test
- mypy result/result.py
- mypy result/typetests.py
after_success:
- coveralls
7 changes: 4 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Result
:alt: Coverage
:target: https://coveralls.io/github/dbrgn/result

A simple Result type for Python 3 `inspired by Rust <https://doc.rust-lang.org/std/result/>`__.
A simple Result type for Python 3 `inspired by Rust
<https://doc.rust-lang.org/std/result/>`__, fully type annotated.

The idea is that a ``Result`` value can be either ``Ok(value)`` or ``Err(error)``,
with a way to differentiate between the two. It will change code like this:
Expand Down Expand Up @@ -62,8 +63,8 @@ side, you don't have to return semantically unclear tuples anymore.

Not all methods (https://doc.rust-lang.org/std/result/enum.Result.html) have
been implemented, only the ones that make sense in the Python context. You still
don't get any type safety, but some easier handling of types that can be OK or
not, without resorting to custom exceptions.
don't get any type safety at runtime, but some easier handling of types that can
be OK or not, without resorting to custom exceptions.


API
Expand Down
12 changes: 12 additions & 0 deletions result/typetests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import List, Optional

from .result import Result


res1 = Result.Ok('hello') # type: Result[str, int]
if res1.is_ok():
ok = res1.ok() # type: Optional[str]
mapped_to_float = res1.map_or(1.0, lambda s: len(s) * 1.5) # type: float
else:
err = res1.err() # type: Optional[int]
mapped_to_list = res1.map_err(lambda e: [e]).err() # type: Optional[List[int]]

0 comments on commit f218cac

Please sign in to comment.