mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-30 08:23:53 +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")):
|
def bool_okay(value=bool("bar")):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Allow immutable bytes() value
|
||||||
|
def bytes_okay(value=bytes(1)):
|
||||||
|
pass
|
||||||
|
|
||||||
# Allow immutable int() value
|
# Allow immutable int() value
|
||||||
def int_okay(value=int("12")):
|
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 | ...
|
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
|
219 | # B006 and B008
|
||||||
217 | # We should handle arbitrary nesting of these B008.
|
220 | # We should handle arbitrary nesting of these B008.
|
||||||
218 | def nested_combo(a=[float(3), dt.datetime.now()]):
|
221 | def nested_combo(a=[float(3), dt.datetime.now()]):
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B006
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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(
|
253 | def mutable_annotations(
|
||||||
251 | a: list[int] | None = [],
|
254 | a: list[int] | None = [],
|
||||||
| ^^ B006
|
| ^^ B006
|
||||||
252 | b: Optional[Dict[int, int]] = {},
|
255 | b: Optional[Dict[int, int]] = {},
|
||||||
253 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
|
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(
|
253 | def mutable_annotations(
|
||||||
251 | a: list[int] | None = [],
|
254 | a: list[int] | None = [],
|
||||||
252 | b: Optional[Dict[int, int]] = {},
|
255 | b: Optional[Dict[int, int]] = {},
|
||||||
| ^^ B006
|
| ^^ B006
|
||||||
253 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
|
256 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
|
||||||
254 | ):
|
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 = [],
|
254 | a: list[int] | None = [],
|
||||||
252 | b: Optional[Dict[int, int]] = {},
|
255 | b: Optional[Dict[int, int]] = {},
|
||||||
253 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
|
256 | c: Annotated[Union[Set[str], abc.Sized], "annotation"] = set(),
|
||||||
| ^^^^^ B006
|
| ^^^^^ B006
|
||||||
254 | ):
|
257 | ):
|
||||||
255 | pass
|
258 | pass
|
||||||
|
|
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -46,38 +46,38 @@ B006_B008.py:120:30: B008 Do not perform function call in argument defaults
|
||||||
121 | ...
|
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
|
219 | # B006 and B008
|
||||||
217 | # We should handle arbitrary nesting of these B008.
|
220 | # We should handle arbitrary nesting of these B008.
|
||||||
218 | def nested_combo(a=[float(3), dt.datetime.now()]):
|
221 | def nested_combo(a=[float(3), dt.datetime.now()]):
|
||||||
| ^^^^^^^^^^^^^^^^^ B008
|
| ^^^^^^^^^^^^^^^^^ 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
|
225 | # Don't flag nested B006 since we can't guarantee that
|
||||||
223 | # it isn't made mutable by the outer operation.
|
226 | # it isn't made mutable by the outer operation.
|
||||||
224 | def no_nested_b006(a=map(lambda s: s.upper(), ["a", "b", "c"])):
|
227 | def no_nested_b006(a=map(lambda s: s.upper(), ["a", "b", "c"])):
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B008
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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.
|
231 | # B008-ception.
|
||||||
229 | def nested_b008(a=random.randint(0, dt.datetime.now().year)):
|
232 | def nested_b008(a=random.randint(0, dt.datetime.now().year)):
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B008
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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.
|
231 | # B008-ception.
|
||||||
229 | def nested_b008(a=random.randint(0, dt.datetime.now().year)):
|
232 | def nested_b008(a=random.randint(0, dt.datetime.now().year)):
|
||||||
| ^^^^^^^^^^^^^^^^^ B008
|
| ^^^^^^^^^^^^^^^^^ B008
|
||||||
230 | pass
|
233 | pass
|
||||||
|
|
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -364,7 +364,7 @@ pub fn is_immutable_return_type(call_path: &[&str]) -> bool {
|
||||||
| ["re", "compile"]
|
| ["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