-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds adds type annotations to tests and fixes all errors. (This change also runs Mypy in CI to avoid regressions.) Practically, add ‘-> None’ annotations to unit tests. This tells mypy to check these tests. This is a good thing since most unit tests are essentially a ‘public API consumer’. This revealed a number of issues, which can also be seen by passing --check-untyped-defs on the version of the code before this commit: $ mypy --check-untyped-defs result/tests.py [...] Found 39 errors in 1 file (checked 1 source file) The first category is simple and happens many times, namely missing annotations: result/tests.py: note: In function "test_ok_factories": result/tests.py:9:1: error: Function is missing a return type annotation result/tests.py:9:1: note: Use "-> None" if function does not return a value The second category are missing annotations that inference cannot solve. For example, the type of ‘x = Ok()’ is unknown because it's a generic class. The fix is to annotate using ‘x: Result[None, None] = Ok()’. The third category are mostly mypy limitations on type inference: result/tests.py: note: In function "test_map_or": result/tests.py:128:1: error: Function is missing a return type annotation result/tests.py:132:12: error: Cannot infer type argument 1 of "map_or" of "Err" result/tests.py:132:38: error: Unsupported left operand type for + ("object") result/tests.py:132:38: error: Returning Any from function declared to return "str" result/tests.py:137:12: error: Cannot infer type argument 1 of "map_or" of "Err" These are fixed by avoiding unnecessary lambdas. For example, ‘lambda x: str(x)’ is the same as just ‘str’, so use that. Other example: instead of a lambda to ‘x+x’ use ‘str.upper’, then adapt the expected values accordingly. Finally, suppress flake8 errors in the pattern matching tests.
- Loading branch information
Showing
4 changed files
with
51 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
# -*- coding: utf-8 -*- | ||
# Pytest test suite | ||
from result import Result, Ok, Err | ||
from result import Err, Ok, Result | ||
|
||
|
||
def test_pattern_matching_on_ok_type(): | ||
def test_pattern_matching_on_ok_type() -> None: | ||
o: Result[str, int] = Ok('yay') | ||
match o: | ||
case Ok(f): | ||
f # Avoids flake8 F841 unused variable | ||
assert True | ||
case Err(e): | ||
e | ||
assert False | ||
|
||
|
||
def test_pattern_matching_on_err_type(): | ||
e: Result[int, str] = Err('nay') | ||
match e: | ||
def test_pattern_matching_on_err_type() -> None: | ||
n: Result[int, str] = Err('nay') | ||
match n: | ||
case Ok(f): | ||
f | ||
assert False | ||
case Err(e): | ||
e | ||
assert True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters