-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B039.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from contextvars import ContextVar | ||
from types import MappingProxyType | ||
import re | ||
import collections | ||
import time | ||
|
||
# Okay | ||
ContextVar("cv") | ||
ContextVar("cv", default=()) | ||
ContextVar("cv", default=(1, 2, 3)) | ||
ContextVar("cv", default="foo") | ||
ContextVar("cv", default=tuple()) | ||
ContextVar("cv", default=frozenset()) | ||
ContextVar("cv", default=MappingProxyType({})) | ||
ContextVar("cv", default=re.compile("foo")) | ||
ContextVar("cv", default=float(1)) | ||
|
||
# Bad | ||
ContextVar("cv", default=[]) | ||
ContextVar("cv", default={}) | ||
ContextVar("cv", default=list()) | ||
ContextVar("cv", default=set()) | ||
ContextVar("cv", default=dict()) | ||
ContextVar("cv", default=[char for char in "foo"]) | ||
ContextVar("cv", default={char for char in "foo"}) | ||
ContextVar("cv", default={char: idx for idx, char in enumerate("foo")}) | ||
ContextVar("cv", default=collections.deque()) | ||
|
||
def bar() -> list[int]: | ||
return [1, 2, 3] | ||
|
||
ContextVar("cv", default=bar()) | ||
ContextVar("cv", default=time.time()) | ||
|
||
def baz(): ... | ||
ContextVar("cv", default=baz()) |
7 changes: 7 additions & 0 deletions
7
crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B039_extended.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from contextvars import ContextVar | ||
|
||
from fastapi import Query | ||
ContextVar("cv", default=Query(None)) | ||
|
||
from something_else import Depends | ||
ContextVar("cv", default=Depends()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_contextvar_default.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use ruff_diagnostics::{Diagnostic, FixAvailability, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::name::QualifiedName; | ||
use ruff_python_ast::{self as ast, Expr}; | ||
use ruff_python_semantic::analyze::typing::{is_immutable_func, is_mutable_expr, is_mutable_func}; | ||
use ruff_python_semantic::Modules; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of mutable objects as ContextVar defaults. | ||
/// | ||
/// ## Why is this bad? | ||
/// | ||
/// The ContextVar default is evaluated once, when the ContextVar is defined. | ||
/// | ||
/// The same mutable object is then shared across all `.get()` method calls to | ||
/// the ContextVar. If the object is modified, those modifications will persist | ||
/// across calls, which can lead to unexpected behavior. | ||
/// | ||
/// Instead, prefer to use immutable data structures, or take `None` as a | ||
/// default, and initialize a new mutable object inside for each call using the | ||
/// `.set()` method. | ||
/// | ||
/// Types outside of the standard library can be marked as immutable with the | ||
/// [`lint.flake8-bugbear.extend-immutable-calls`] configuration option. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from contextvars import ContextVar | ||
/// | ||
/// | ||
/// cv: ContextVar[list] = ContextVar("cv", default=[]) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from contextvars import ContextVar | ||
/// | ||
/// | ||
/// cv: ContextVar[list | None] = ContextVar("cv", default=None) | ||
/// | ||
/// ... | ||
/// | ||
/// if cv.get() is None: | ||
/// cv.set([]) | ||
/// ``` | ||
/// | ||
/// ## Options | ||
/// - `lint.flake8-bugbear.extend-immutable-calls` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: [`contextvars` — Context Variables](https://docs.python.org/3/library/contextvars.html) | ||
#[violation] | ||
pub struct MutableContextvarDefault; | ||
|
||
impl Violation for MutableContextvarDefault { | ||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; | ||
|
||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Do not use mutable data structures for ContextVar defaults") | ||
} | ||
|
||
fn fix_title(&self) -> Option<String> { | ||
Some(format!( | ||
"Replace with `None`; initialize with `.set()` after checking for `None`" | ||
)) | ||
} | ||
} | ||
|
||
/// B039 | ||
pub(crate) fn mutable_contextvar_default(checker: &mut Checker, call: &ast::ExprCall) { | ||
if !checker.semantic().seen_module(Modules::CONTEXTVARS) { | ||
return; | ||
} | ||
|
||
let Some(default) = call | ||
.arguments | ||
.find_keyword("default") | ||
.map(|keyword| &keyword.value) | ||
else { | ||
return; | ||
}; | ||
|
||
let extend_immutable_calls: Vec<QualifiedName> = checker | ||
.settings | ||
.flake8_bugbear | ||
.extend_immutable_calls | ||
.iter() | ||
.map(|target| QualifiedName::from_dotted_name(target)) | ||
.collect(); | ||
|
||
if (is_mutable_expr(default, checker.semantic()) | ||
|| matches!( | ||
default, | ||
Expr::Call(ast::ExprCall { func, .. }) | ||
if !is_mutable_func(func, checker.semantic()) | ||
&& !is_immutable_func(func, checker.semantic(), &extend_immutable_calls))) | ||
&& checker | ||
.semantic() | ||
.resolve_qualified_name(&call.func) | ||
.is_some_and(|qualified_name| { | ||
matches!(qualified_name.segments(), ["contextvars", "ContextVar"]) | ||
}) | ||
{ | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(MutableContextvarDefault, default.range())); | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
...les/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B039_B039.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs | ||
--- | ||
B039.py:19:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
18 | # Bad | ||
19 | ContextVar("cv", default=[]) | ||
| ^^ B039 | ||
20 | ContextVar("cv", default={}) | ||
21 | ContextVar("cv", default=list()) | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:20:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
18 | # Bad | ||
19 | ContextVar("cv", default=[]) | ||
20 | ContextVar("cv", default={}) | ||
| ^^ B039 | ||
21 | ContextVar("cv", default=list()) | ||
22 | ContextVar("cv", default=set()) | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:21:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
19 | ContextVar("cv", default=[]) | ||
20 | ContextVar("cv", default={}) | ||
21 | ContextVar("cv", default=list()) | ||
| ^^^^^^ B039 | ||
22 | ContextVar("cv", default=set()) | ||
23 | ContextVar("cv", default=dict()) | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:22:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
20 | ContextVar("cv", default={}) | ||
21 | ContextVar("cv", default=list()) | ||
22 | ContextVar("cv", default=set()) | ||
| ^^^^^ B039 | ||
23 | ContextVar("cv", default=dict()) | ||
24 | ContextVar("cv", default=[char for char in "foo"]) | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:23:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
21 | ContextVar("cv", default=list()) | ||
22 | ContextVar("cv", default=set()) | ||
23 | ContextVar("cv", default=dict()) | ||
| ^^^^^^ B039 | ||
24 | ContextVar("cv", default=[char for char in "foo"]) | ||
25 | ContextVar("cv", default={char for char in "foo"}) | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:24:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
22 | ContextVar("cv", default=set()) | ||
23 | ContextVar("cv", default=dict()) | ||
24 | ContextVar("cv", default=[char for char in "foo"]) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ B039 | ||
25 | ContextVar("cv", default={char for char in "foo"}) | ||
26 | ContextVar("cv", default={char: idx for idx, char in enumerate("foo")}) | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:25:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
23 | ContextVar("cv", default=dict()) | ||
24 | ContextVar("cv", default=[char for char in "foo"]) | ||
25 | ContextVar("cv", default={char for char in "foo"}) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ B039 | ||
26 | ContextVar("cv", default={char: idx for idx, char in enumerate("foo")}) | ||
27 | ContextVar("cv", default=collections.deque()) | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:26:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
24 | ContextVar("cv", default=[char for char in "foo"]) | ||
25 | ContextVar("cv", default={char for char in "foo"}) | ||
26 | ContextVar("cv", default={char: idx for idx, char in enumerate("foo")}) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B039 | ||
27 | ContextVar("cv", default=collections.deque()) | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:27:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
25 | ContextVar("cv", default={char for char in "foo"}) | ||
26 | ContextVar("cv", default={char: idx for idx, char in enumerate("foo")}) | ||
27 | ContextVar("cv", default=collections.deque()) | ||
| ^^^^^^^^^^^^^^^^^^^ B039 | ||
28 | | ||
29 | def bar() -> list[int]: | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:32:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
30 | return [1, 2, 3] | ||
31 | | ||
32 | ContextVar("cv", default=bar()) | ||
| ^^^^^ B039 | ||
33 | ContextVar("cv", default=time.time()) | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:33:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
32 | ContextVar("cv", default=bar()) | ||
33 | ContextVar("cv", default=time.time()) | ||
| ^^^^^^^^^^^ B039 | ||
34 | | ||
35 | def baz(): ... | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` | ||
|
||
B039.py:36:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
35 | def baz(): ... | ||
36 | ContextVar("cv", default=baz()) | ||
| ^^^^^ B039 | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` |
10 changes: 10 additions & 0 deletions
10
...apshots/ruff_linter__rules__flake8_bugbear__tests__extend_mutable_contextvar_default.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs | ||
--- | ||
B039_extended.py:7:26: B039 Do not use mutable data structures for ContextVar defaults | ||
| | ||
6 | from something_else import Depends | ||
7 | ContextVar("cv", default=Depends()) | ||
| ^^^^^^^^^ B039 | ||
| | ||
= help: Replace with `None`; initialize with `.set()` after checking for `None` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.