Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[B006] Add bytes to immutable types #5776

Merged
merged 2 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
|


Original file line number Diff line number Diff line change
Expand Up @@ -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
|


2 changes: 1 addition & 1 deletion crates/ruff_python_stdlib/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
)
}
Expand Down