From 9840a4d083c1f2754104dcf4be43bcfa8af71d57 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Sat, 8 Jul 2023 11:04:24 +0530 Subject: [PATCH] Tests --- .../flake8_annotations/annotation_presence.py | 26 +++++++-- .../flake8_annotations/rules/definition.rs | 4 +- ...__flake8_annotations__tests__defaults.snap | 56 +++++++++++++++++++ 3 files changed, 78 insertions(+), 8 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_annotations/annotation_presence.py b/crates/ruff/resources/test/fixtures/flake8_annotations/annotation_presence.py index d37f178bbb938a..7778c85072b9a8 100644 --- a/crates/ruff/resources/test/fixtures/flake8_annotations/annotation_presence.py +++ b/crates/ruff/resources/test/fixtures/flake8_annotations/annotation_presence.py @@ -1,4 +1,4 @@ -from typing import Any, Type +from typing import Annotated, Any, Optional, Type, Union from typing_extensions import override # Error @@ -95,27 +95,27 @@ def foo(self: "Foo", a: int, *params: Any, **options: str) -> int: def foo(self: "Foo", a: int, *params: str, **options: Any) -> int: pass - # ANN401 + # OK @override def foo(self: "Foo", a: Any, *params: str, **options: str) -> int: pass - # ANN401 + # OK @override def foo(self: "Foo", a: int, *params: str, **options: str) -> Any: pass - # ANN401 + # OK @override def foo(self: "Foo", a: int, *params: Any, **options: Any) -> int: pass - # ANN401 + # OK @override def foo(self: "Foo", a: int, *params: Any, **options: str) -> int: pass - # ANN401 + # OK @override def foo(self: "Foo", a: int, *params: str, **options: Any) -> int: pass @@ -137,3 +137,17 @@ def foo(self, /, a: int, b: int) -> int: # OK def f(*args: *tuple[int]) -> None: ... +def f(a: object) -> None: ... +def f(a: str | bytes) -> None: ... +def f(a: Union[str, bytes]) -> None: ... +def f(a: Optional[str]) -> None: ... +def f(a: Annotated[str, ...]) -> None: ... +def f(a: "Union[str, bytes]") -> None: ... + +# ANN401 +def f(a: Any | int) -> None: ... +def f(a: int | Any) -> None: ... +def f(a: Union[str, bytes, Any]) -> None: ... +def f(a: Optional[Any]) -> None: ... +def f(a: Annotated[Any, ...]) -> None: ... +def f(a: "Union[str, bytes, Any]") -> None: ... diff --git a/crates/ruff/src/rules/flake8_annotations/rules/definition.rs b/crates/ruff/src/rules/flake8_annotations/rules/definition.rs index 56094110ff83d2..f672afd9b7956f 100644 --- a/crates/ruff/src/rules/flake8_annotations/rules/definition.rs +++ b/crates/ruff/src/rules/flake8_annotations/rules/definition.rs @@ -448,9 +448,9 @@ fn check_dynamically_typed( }) = annotation { // Quoted annotations - if let Ok((annotation, _)) = parse_type_annotation(string, *range, checker.locator) { + if let Ok((parsed_annotation, _)) = parse_type_annotation(string, *range, checker.locator) { if type_hint_resolves_to_any( - &annotation, + &parsed_annotation, checker.semantic(), checker.locator, checker.settings.target_version.minor(), diff --git a/crates/ruff/src/rules/flake8_annotations/snapshots/ruff__rules__flake8_annotations__tests__defaults.snap b/crates/ruff/src/rules/flake8_annotations/snapshots/ruff__rules__flake8_annotations__tests__defaults.snap index 0eba6a0469bf57..7cd87414d40d77 100644 --- a/crates/ruff/src/rules/flake8_annotations/snapshots/ruff__rules__flake8_annotations__tests__defaults.snap +++ b/crates/ruff/src/rules/flake8_annotations/snapshots/ruff__rules__flake8_annotations__tests__defaults.snap @@ -186,4 +186,60 @@ annotation_presence.py:134:13: ANN101 Missing type annotation for `self` in meth 135 | pass | +annotation_presence.py:148:10: ANN401 Dynamically typed expressions (typing.Any) are disallowed in `a` + | +147 | # ANN401 +148 | def f(a: Any | int) -> None: ... + | ^^^^^^^^^ ANN401 +149 | def f(a: int | Any) -> None: ... +150 | def f(a: Union[str, bytes, Any]) -> None: ... + | + +annotation_presence.py:149:10: ANN401 Dynamically typed expressions (typing.Any) are disallowed in `a` + | +147 | # ANN401 +148 | def f(a: Any | int) -> None: ... +149 | def f(a: int | Any) -> None: ... + | ^^^^^^^^^ ANN401 +150 | def f(a: Union[str, bytes, Any]) -> None: ... +151 | def f(a: Optional[Any]) -> None: ... + | + +annotation_presence.py:150:10: ANN401 Dynamically typed expressions (typing.Any) are disallowed in `a` + | +148 | def f(a: Any | int) -> None: ... +149 | def f(a: int | Any) -> None: ... +150 | def f(a: Union[str, bytes, Any]) -> None: ... + | ^^^^^^^^^^^^^^^^^^^^^^ ANN401 +151 | def f(a: Optional[Any]) -> None: ... +152 | def f(a: Annotated[Any, ...]) -> None: ... + | + +annotation_presence.py:151:10: ANN401 Dynamically typed expressions (typing.Any) are disallowed in `a` + | +149 | def f(a: int | Any) -> None: ... +150 | def f(a: Union[str, bytes, Any]) -> None: ... +151 | def f(a: Optional[Any]) -> None: ... + | ^^^^^^^^^^^^^ ANN401 +152 | def f(a: Annotated[Any, ...]) -> None: ... +153 | def f(a: "Union[str, bytes, Any]") -> None: ... + | + +annotation_presence.py:152:10: ANN401 Dynamically typed expressions (typing.Any) are disallowed in `a` + | +150 | def f(a: Union[str, bytes, Any]) -> None: ... +151 | def f(a: Optional[Any]) -> None: ... +152 | def f(a: Annotated[Any, ...]) -> None: ... + | ^^^^^^^^^^^^^^^^^^^ ANN401 +153 | def f(a: "Union[str, bytes, Any]") -> None: ... + | + +annotation_presence.py:153:10: ANN401 Dynamically typed expressions (typing.Any) are disallowed in `a` + | +151 | def f(a: Optional[Any]) -> None: ... +152 | def f(a: Annotated[Any, ...]) -> None: ... +153 | def f(a: "Union[str, bytes, Any]") -> None: ... + | ^^^^^^^^^^^^^^^^^^^^^^^^ ANN401 + | +