-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for
typing.overload
decorators
- Loading branch information
Showing
3 changed files
with
127 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from textwrap import dedent | ||
from typing import NamedTuple | ||
|
||
|
||
class OverloadDecoratorTestCase(NamedTuple): | ||
"""Helper container for tests for the suppression of errors for `typing.overload` decorators.""" | ||
|
||
src: str | ||
should_yield_error: bool | ||
|
||
|
||
overload_decorator_test_cases = { | ||
"overload_decorated_attribute": OverloadDecoratorTestCase( | ||
src=dedent( | ||
"""\ | ||
@typing.overload | ||
def foo(a: int) -> int: | ||
... | ||
def foo(a): | ||
... | ||
""" | ||
), | ||
should_yield_error=False, | ||
), | ||
"overload_decorated_aliased_attribute": OverloadDecoratorTestCase( | ||
src=dedent( | ||
"""\ | ||
@t.overload | ||
def foo(a: int) -> int: | ||
... | ||
def foo(a): | ||
... | ||
""" | ||
), | ||
should_yield_error=False, | ||
), | ||
"overload_decorated_direct_import": OverloadDecoratorTestCase( | ||
src=dedent( | ||
"""\ | ||
@overload | ||
def foo(a: int) -> int: | ||
... | ||
def foo(a): | ||
... | ||
""" | ||
), | ||
should_yield_error=False, | ||
), | ||
"overload_decorated_aliased_import": OverloadDecoratorTestCase( # Aliased import not suppoerted | ||
src=dedent( | ||
"""\ | ||
@ovrld | ||
def foo(a: int) -> int: | ||
... | ||
def foo(a): | ||
... | ||
""" | ||
), | ||
should_yield_error=True, | ||
), | ||
"overload_decorated_name_mismatch": OverloadDecoratorTestCase( | ||
src=dedent( | ||
"""\ | ||
@typing.overload | ||
def foo(a: int) -> int: | ||
... | ||
def bar(a): | ||
... | ||
""" | ||
), | ||
should_yield_error=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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import Tuple | ||
|
||
import pytest | ||
from flake8_annotations.error_codes import Error | ||
from testing.helpers import check_is_empty, check_is_not_empty, check_source | ||
|
||
from .test_cases.overload_decorator_test_cases import ( | ||
OverloadDecoratorTestCase, | ||
overload_decorator_test_cases, | ||
) | ||
|
||
|
||
class TestOverloadDecoratorErrorSuppression: | ||
"""Test suppression of errors for the closing def of a `typing.overload` series.""" | ||
|
||
@pytest.fixture( | ||
params=overload_decorator_test_cases.items(), ids=overload_decorator_test_cases.keys() | ||
) | ||
def yielded_errors( | ||
self, request # noqa: ANN001 | ||
) -> Tuple[str, OverloadDecoratorTestCase, Tuple[Error]]: | ||
""" | ||
Build a fixture for the errors emitted from parsing `@overload` decorated test code. | ||
Fixture provides a tuple of: test case name, its corresponding | ||
`OverloadDecoratorTestCase` instance, and a tuple of the errors yielded by the | ||
checker, which should be empty if the test case's `should_yield_error` is `False`. | ||
""" | ||
test_case_name, test_case = request.param | ||
|
||
return ( | ||
test_case_name, | ||
test_case, | ||
tuple(check_source(test_case.src)), | ||
) | ||
|
||
def test_overload_decorator_error_suppression( | ||
self, yielded_errors: Tuple[str, OverloadDecoratorTestCase, Tuple[Error]] | ||
) -> None: | ||
"""Test that no errors are yielded for the closing def of a `typing.overload` series.""" | ||
test_case_name, test_case, errors = yielded_errors | ||
failure_msg = f"Check failed for case '{test_case_name}'" | ||
|
||
if test_case.should_yield_error: | ||
check_is_not_empty(errors, msg=failure_msg) | ||
else: | ||
check_is_empty(errors, msg=failure_msg) |
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