Skip to content

Commit

Permalink
Hook up opinionated any starg flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sco1 committed Apr 8, 2022
1 parent 34ebdbe commit 629432e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 21 deletions.
9 changes: 7 additions & 2 deletions flake8_annotations/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@ def run(self) -> t.Generator[FORMATTED_ERROR, None, None]:
yield error_codes.ANN301.from_function(function).to_flake8()
break

# Iterate over the annotated args to look for `typing.Any`` annotations
# Iterate over the annotated args to look for `typing.Any` annotations
# We could combine this with the above loop but I'd rather not add even more sentinels
# unless we'd notice a significant enough performance impact
for arg in annotated_args:
if arg.is_dynamically_typed:
# Always yield these and let flake8 take care of ignoring
if self.allow_star_arg_any and arg.annotation_type in {
enums.AnnotationType.VARARG,
enums.AnnotationType.KWARG,
}:
continue

yield error_codes.ANN401.from_argument(arg).to_flake8()

# Before we iterate over the function's missing annotations, check to see if it's the
Expand Down
2 changes: 2 additions & 0 deletions testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def check_source(
allow_untyped_defs: bool = False,
allow_untyped_nested: bool = False,
mypy_init_return: bool = False,
allow_star_arg_any: bool = False,
dispatch_decorators: t.AbstractSet[str] = frozenset(_DEFAULT_DISPATCH_DECORATORS),
overload_decorators: t.AbstractSet[str] = frozenset(_DEFAULT_OVERLOAD_DECORATORS),
) -> t.Generator[FORMATTED_ERROR, None, None]:
Expand All @@ -51,6 +52,7 @@ def check_source(
checker_instance.allow_untyped_defs = allow_untyped_defs
checker_instance.allow_untyped_nested = allow_untyped_nested
checker_instance.mypy_init_return = mypy_init_return
checker_instance.allow_star_arg_any = allow_star_arg_any
checker_instance.dispatch_decorators = dispatch_decorators
checker_instance.overload_decorators = overload_decorators

Expand Down
80 changes: 61 additions & 19 deletions testing/test_opinionated_any.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,78 @@ def foo(a):
@pytest.mark.parametrize(("src",), TEST_CASES)
def test_dynamic_typing_errors(src: str) -> None:
found_errors = list(check_source(src))

assert len(found_errors) == 1

_, _, err_msg, _ = found_errors[0]
assert "ANN401" in err_msg


def test_ANN401_ignored_default() -> None:
inp = dedent(
"""\
import typing
def foo(a: typing.Any) -> None:
...
"""
)
p = run(["flake8", "--select=ANN", "-"], stdout=PIPE, input=inp, encoding="ascii")
INP = dedent(
"""\
import typing
def foo(a: typing.Any) -> None:
...
"""
)


def test_ANN401_ignored_default() -> None:
p = run(["flake8", "--select=ANN", "-"], stdout=PIPE, input=INP, encoding="ascii")
assert len(p.stdout) == 0


def test_ANN401_fire_when_selected() -> None:
inp = dedent(
"""\
import typing
def foo(a: typing.Any) -> None:
...
"""
)
p = run(
["flake8", "--select=ANN", "--ignore=''", "-"], stdout=PIPE, input=inp, encoding="ascii"
["flake8", "--select=ANN", "--ignore=''", "-"], stdout=PIPE, input=INP, encoding="ascii"
)

assert "ANN401" in p.stdout


STARG_CASES = (
(
dedent(
"""\
from typing import Any
def foo(*a: Any) -> None:
...
"""
),
),
(
dedent(
"""\
from typing import Any
def foo(**a: Any) -> None:
...
"""
),
),
(
dedent(
"""\
from typing import Any
def foo(a: int, *b: Any, c: int) -> None:
...
"""
),
),
(
dedent(
"""\
from typing import Any
def foo(a: int, *, b: int, **c: Any) -> None:
...
"""
),
),
)


@pytest.mark.parametrize(("src",), STARG_CASES)
def test_ignore_stargs(src: str) -> None:
found_errors = list(check_source(src, allow_star_arg_any=True))
assert len(found_errors) == 0

0 comments on commit 629432e

Please sign in to comment.