Skip to content

Commit

Permalink
Respect typing_extensions imports of Annotated for B006. (#6361)
Browse files Browse the repository at this point in the history
`typing_extensions.Annotated` should be treated the same way as
`typing.Annotated`.
  • Loading branch information
PIG208 committed Aug 5, 2023
1 parent 76148dd commit be657f5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,16 @@ def foo(f=lambda x: print(x)):

from collections import abc
from typing import Annotated, Dict, Optional, Sequence, Union, Set
import typing_extensions


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

Expand All @@ -254,5 +258,6 @@ def mutable_annotations(
a: list[int] | None = [],
b: Optional[Dict[int, int]] = {},
c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
d: typing_extensions.Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,43 @@ B006_B008.py:221:20: B006 Do not use mutable data structures for argument defaul
222 | pass
|

B006_B008.py:254:27: B006 Do not use mutable data structures for argument defaults
B006_B008.py:258:27: B006 Do not use mutable data structures for argument defaults
|
253 | def mutable_annotations(
254 | a: list[int] | None = [],
257 | def mutable_annotations(
258 | a: list[int] | None = [],
| ^^ B006
255 | b: Optional[Dict[int, int]] = {},
256 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
259 | b: Optional[Dict[int, int]] = {},
260 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
|

B006_B008.py:255:35: B006 Do not use mutable data structures for argument defaults
B006_B008.py:259:35: B006 Do not use mutable data structures for argument defaults
|
253 | def mutable_annotations(
254 | a: list[int] | None = [],
255 | b: Optional[Dict[int, int]] = {},
257 | def mutable_annotations(
258 | a: list[int] | None = [],
259 | b: Optional[Dict[int, int]] = {},
| ^^ B006
256 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
257 | ):
260 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
261 | d: typing_extensions.Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
|

B006_B008.py:256:62: B006 Do not use mutable data structures for argument defaults
B006_B008.py:260:62: B006 Do not use mutable data structures for argument defaults
|
254 | a: list[int] | None = [],
255 | b: Optional[Dict[int, int]] = {},
256 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
258 | a: list[int] | None = [],
259 | b: Optional[Dict[int, int]] = {},
260 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
| ^^^^^ B006
257 | ):
258 | pass
261 | d: typing_extensions.Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
262 | ):
|

B006_B008.py:261:80: B006 Do not use mutable data structures for argument defaults
|
259 | b: Optional[Dict[int, int]] = {},
260 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
261 | d: typing_extensions.Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
| ^^^^^ B006
262 | ):
263 | 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 be657f5

Please sign in to comment.