Skip to content

Commit

Permalink
Respect typing_extensions imports of Annotated for B006.
Browse files Browse the repository at this point in the history
typing_extensions.Annotated should be treated the same way as
typing.Annotated.

Signed-off-by: Zixuan James Li <[email protected]>
  • Loading branch information
PIG208 committed Aug 5, 2023
1 parent 1ac2699 commit effc23b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
17 changes: 17 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_bugbear/B006_B008.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,20 @@ def mutable_annotations(
c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
):
pass

from typing_extensions import Annotated as Annotated_te

def immutable_annotations(
a: Sequence[int] | None = [],
b: Optional[abc.Mapping[int, int]] = {},
c: Annotated_te[Union[abc.Set[str], abc.Sized], "annotation"] = set(),
):
pass


def mutable_annotations(
a: list[int] | None = [],
b: Optional[Dict[int, int]] = {},
c: Annotated_te[Union[Set[str], abc.Sized], "annotation"] = set(),
):
pass
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
assertion_line: 59
---
B006_B008.py:63:25: B006 Do not use mutable data structures for argument defaults
|
Expand Down Expand Up @@ -110,4 +111,33 @@ B006_B008.py:256:62: B006 Do not use mutable data structures for argument defaul
258 | pass
|

B006_B008.py:271:27: B006 Do not use mutable data structures for argument defaults
|
270 | def mutable_annotations(
271 | a: list[int] | None = [],
| ^^ B006
272 | b: Optional[Dict[int, int]] = {},
273 | c: Annotated_te[Union[Set[str], abc.Sized], "annotation"] = set(),
|

B006_B008.py:272:35: B006 Do not use mutable data structures for argument defaults
|
270 | def mutable_annotations(
271 | a: list[int] | None = [],
272 | b: Optional[Dict[int, int]] = {},
| ^^ B006
273 | c: Annotated_te[Union[Set[str], abc.Sized], "annotation"] = set(),
274 | ):
|

B006_B008.py:273:65: B006 Do not use mutable data structures for argument defaults
|
271 | a: list[int] | None = [],
272 | b: Optional[Dict[int, int]] = {},
273 | c: Annotated_te[Union[Set[str], abc.Sized], "annotation"] = set(),
| ^^^^^ B006
274 | ):
275 | pass
|


2 changes: 1 addition & 1 deletion crates/ruff_python_semantic/src/analyze/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub fn is_immutable_annotation(expr: &Expr, semantic: &SemanticModel) -> bool {
}
} else if matches!(call_path.as_slice(), ["typing", "Optional"]) {
is_immutable_annotation(slice, semantic)
} else if matches!(call_path.as_slice(), ["typing", "Annotated"]) {
} else if is_pep_593_generic_type(call_path.as_slice()) {
if let Expr::Tuple(ast::ExprTuple { elts, .. }) = slice.as_ref() {
elts.first()
.is_some_and(|elt| is_immutable_annotation(elt, semantic))
Expand Down

0 comments on commit effc23b

Please sign in to comment.