Skip to content

Commit

Permalink
Add tests for typing.overload decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
sco1 committed Sep 6, 2020
1 parent b729a01 commit 294d1e3
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
78 changes: 78 additions & 0 deletions testing/test_cases/overload_decorator_test_cases.py
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,
),
}
47 changes: 47 additions & 0 deletions testing/test_overload_decorator.py
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)
4 changes: 2 additions & 2 deletions testing/test_type_comment_arg_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def yielded_errors(
tuple(check_source(test_case.src)),
)

def test_suppressed_return_error(
def test_type_comment_arg_injection(
self, yielded_errors: Tuple[str, TypeCommentArgInjectTestCase, Tuple[Error]]
) -> None:
"""Test that ANN101 error is yielded where necessary."""
"""Test that ANN100 errors are yielded appropriately for type comment annotated defs."""
failure_msg = f"Check failed for case '{yielded_errors[0]}'"

yielded_ANN100 = any("ANN1" in error[2] for error in yielded_errors[2])
Expand Down

0 comments on commit 294d1e3

Please sign in to comment.