-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: enable type checking of funparam function arguments
To enable type checking on funparam function arguments, I need to change the API. Currently, we're using "magic" keyword arguments: `_id` and `_marks`. There's no way to support this with Python's current static typing. The best way I could come up with was making funparam functions a class, where the `__call__` method has the type signature of the wrapped function. The funparam function can accept an `id` or `marks` via its `.id()` and `.marks()` methods. Both methods return a funparam function object with the same signature as the originally wrapped function. It's kind of gross-looking, but it gets the job done. I'll keep exploring alternatives. For now, this is an improvement. This commit also implements some testing utilities for validating static typing behavior with mypy. BREAKING CHANGE: Using the `_marks` and `_id` keyword arguments is no longer supported.
- Loading branch information
1 parent
4b5de04
commit ddc7be6
Showing
11 changed files
with
496 additions
and
52 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
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,4 +1,5 @@ | ||
pytest_plugins = [ | ||
'pytester', | ||
"tests.fixtures.verify_examples", | ||
"tests.fixtures.type_checking", | ||
] |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import pytest | ||
from .type_checking import MypyLine, parse_mypy_line | ||
|
||
|
||
def test_parse_mypy_line(): | ||
line = '<string>:16: error: Argument 2 to "verify_sum" has incompatible type "str"; expected "int" [arg-type]' # noqa | ||
parsed = MypyLine( | ||
source='<string>', | ||
lineno=16, | ||
severity="error", | ||
message='Argument 2 to "verify_sum" has incompatible type "str"; expected "int"', # noqa | ||
code='arg-type', | ||
) | ||
assert parse_mypy_line(line) == parsed | ||
|
||
|
||
def test_assert_mypy_error_codes(assert_mypy_error_codes): | ||
# Happy path | ||
assert_mypy_error_codes( | ||
""" | ||
foo: int | ||
foo = 3 | ||
foo = 'bar' # [assignment] | ||
""" | ||
) | ||
# Expected failure did not happen | ||
with pytest.raises(AssertionError): | ||
assert_mypy_error_codes( | ||
""" | ||
foo: int | ||
foo = 3 # [assigment] | ||
""" | ||
) | ||
# Did not expect failure | ||
with pytest.raises(AssertionError): | ||
assert_mypy_error_codes( | ||
""" | ||
foo: int | ||
foo = 'foo' | ||
""" | ||
) | ||
# Wrong error expected | ||
with pytest.raises(AssertionError): | ||
assert_mypy_error_codes( | ||
""" | ||
foo: int | ||
foo = 'foo' # [arg-type] | ||
""" | ||
) |
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
Oops, something went wrong.