mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 10:23:11 +00:00
[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 <dhruvmanila@gmail.com>
This commit is contained in:
parent
f029f8b784
commit
daa4b72d5f
4 changed files with 44 additions and 41 deletions
|
@ -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")):
|
||||
|
|
|
@ -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
|
||||
|
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
||||
|
||||
|
||||
|
|
|
@ -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"
|
||||
]
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue