From 8a6afe2036d016ba6f49a5dafad2152cd219a9ff Mon Sep 17 00:00:00 2001 From: Harutaka Kawamura Date: Sat, 15 Jul 2023 22:04:33 +0900 Subject: [PATCH] [B006] Add `bytes` to immutable types (#5776) ## Summary `B006` should allow using `bytes(...)` as an argument defaule value. ## Test Plan A new test case --------- Co-authored-by: Dhruv Manilawala --- .../test/fixtures/flake8_bugbear/B006_B008.py | 3 ++ ...ke8_bugbear__tests__B006_B006_B008.py.snap | 44 +++++++++---------- ...ke8_bugbear__tests__B008_B006_B008.py.snap | 36 +++++++-------- crates/ruff_python_stdlib/src/typing.rs | 2 +- 4 files changed, 44 insertions(+), 41 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_B008.py b/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_B008.py index 297e3ca49e837a..475506225a8766 100644 --- a/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_B008.py +++ b/crates/ruff/resources/test/fixtures/flake8_bugbear/B006_B008.py @@ -177,6 +177,9 @@ def str_okay(value=str("foo")): def bool_okay(value=bool("bar")): pass +# Allow immutable bytes() value +def bytes_okay(value=bytes(1)): + pass # Allow immutable int() value def int_okay(value=int("12")): diff --git a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_B008.py.snap b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_B008.py.snap index 8bde19f3201843..f1fc165498682c 100644 --- a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_B008.py.snap +++ b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B006_B006_B008.py.snap @@ -72,42 +72,42 @@ B006_B008.py:100:33: B006 Do not use mutable data structures for argument defaul 101 | ... | -B006_B008.py:218:20: B006 Do not use mutable data structures for argument defaults +B006_B008.py:221:20: B006 Do not use mutable data structures for argument defaults | -216 | # B006 and B008 -217 | # We should handle arbitrary nesting of these B008. -218 | def nested_combo(a=[float(3), dt.datetime.now()]): +219 | # B006 and B008 +220 | # We should handle arbitrary nesting of these B008. +221 | def nested_combo(a=[float(3), dt.datetime.now()]): | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B006 -219 | pass +222 | pass | -B006_B008.py:251:27: B006 Do not use mutable data structures for argument defaults +B006_B008.py:254:27: B006 Do not use mutable data structures for argument defaults | -250 | def mutable_annotations( -251 | a: list[int] | None = [], +253 | def mutable_annotations( +254 | a: list[int] | None = [], | ^^ B006 -252 | b: Optional[Dict[int, int]] = {}, -253 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(), +255 | b: Optional[Dict[int, int]] = {}, +256 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(), | -B006_B008.py:252:35: B006 Do not use mutable data structures for argument defaults +B006_B008.py:255:35: B006 Do not use mutable data structures for argument defaults | -250 | def mutable_annotations( -251 | a: list[int] | None = [], -252 | b: Optional[Dict[int, int]] = {}, +253 | def mutable_annotations( +254 | a: list[int] | None = [], +255 | b: Optional[Dict[int, int]] = {}, | ^^ B006 -253 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(), -254 | ): +256 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(), +257 | ): | -B006_B008.py:253:62: B006 Do not use mutable data structures for argument defaults +B006_B008.py:256:62: B006 Do not use mutable data structures for argument defaults | -251 | a: list[int] | None = [], -252 | b: Optional[Dict[int, int]] = {}, -253 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(), +254 | a: list[int] | None = [], +255 | b: Optional[Dict[int, int]] = {}, +256 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(), | ^^^^^ B006 -254 | ): -255 | pass +257 | ): +258 | pass | diff --git a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B008_B006_B008.py.snap b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B008_B006_B008.py.snap index 29fe310acafa61..36a464813a00cf 100644 --- a/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B008_B006_B008.py.snap +++ b/crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B008_B006_B008.py.snap @@ -46,38 +46,38 @@ B006_B008.py:120:30: B008 Do not perform function call in argument defaults 121 | ... | -B006_B008.py:218:31: B008 Do not perform function call `dt.datetime.now` in argument defaults +B006_B008.py:221:31: B008 Do not perform function call `dt.datetime.now` in argument defaults | -216 | # B006 and B008 -217 | # We should handle arbitrary nesting of these B008. -218 | def nested_combo(a=[float(3), dt.datetime.now()]): +219 | # B006 and B008 +220 | # We should handle arbitrary nesting of these B008. +221 | def nested_combo(a=[float(3), dt.datetime.now()]): | ^^^^^^^^^^^^^^^^^ B008 -219 | pass +222 | pass | -B006_B008.py:224:22: B008 Do not perform function call `map` in argument defaults +B006_B008.py:227:22: B008 Do not perform function call `map` in argument defaults | -222 | # Don't flag nested B006 since we can't guarantee that -223 | # it isn't made mutable by the outer operation. -224 | def no_nested_b006(a=map(lambda s: s.upper(), ["a", "b", "c"])): +225 | # Don't flag nested B006 since we can't guarantee that +226 | # it isn't made mutable by the outer operation. +227 | def no_nested_b006(a=map(lambda s: s.upper(), ["a", "b", "c"])): | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B008 -225 | pass +228 | pass | -B006_B008.py:229:19: B008 Do not perform function call `random.randint` in argument defaults +B006_B008.py:232:19: B008 Do not perform function call `random.randint` in argument defaults | -228 | # B008-ception. -229 | def nested_b008(a=random.randint(0, dt.datetime.now().year)): +231 | # B008-ception. +232 | def nested_b008(a=random.randint(0, dt.datetime.now().year)): | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B008 -230 | pass +233 | pass | -B006_B008.py:229:37: B008 Do not perform function call `dt.datetime.now` in argument defaults +B006_B008.py:232:37: B008 Do not perform function call `dt.datetime.now` in argument defaults | -228 | # B008-ception. -229 | def nested_b008(a=random.randint(0, dt.datetime.now().year)): +231 | # B008-ception. +232 | def nested_b008(a=random.randint(0, dt.datetime.now().year)): | ^^^^^^^^^^^^^^^^^ B008 -230 | pass +233 | pass | diff --git a/crates/ruff_python_stdlib/src/typing.rs b/crates/ruff_python_stdlib/src/typing.rs index c36455025df317..d210964efbebcf 100644 --- a/crates/ruff_python_stdlib/src/typing.rs +++ b/crates/ruff_python_stdlib/src/typing.rs @@ -364,7 +364,7 @@ pub fn is_immutable_return_type(call_path: &[&str]) -> bool { | ["re", "compile"] | [ "", - "bool" | "complex" | "float" | "frozenset" | "int" | "str" | "tuple" + "bool" | "bytes" | "complex" | "float" | "frozenset" | "int" | "str" | "tuple" ] ) }