Compare commits

...

3 Commits

Author SHA1 Message Date
Charlie Marsh
d436cd560a Add a no-parallel mode 2023-03-13 16:30:26 -04:00
Charlie Marsh
a8c1915e2e Remove erroneous C4-to-C40 redirect (#3488) 2023-03-13 19:52:05 +00:00
Xuehai Pan
c515a1b31a PYI011: allow math constants in defaults (#3484) 2023-03-13 14:23:00 -04:00
6 changed files with 187 additions and 3 deletions

View File

@@ -61,3 +61,43 @@ def f22(
x: complex = -42.5j # Error PYI011 Only simple default values allowed for typed arguments
+ 4.3j,
) -> None: ...
def f23(
x: bool = True, # OK
) -> None: ...
def f24(
x: float = 3.14, # OK
) -> None: ...
def f25(
x: float = -3.14, # OK
) -> None: ...
def f26(
x: complex = -3.14j, # OK
) -> None: ...
def f27(
x: complex = -3 - 3.14j, # OK
) -> None: ...
def f28(
x: float = math.tau, # OK
) -> None: ...
def f29(
x: float = math.inf, # OK
) -> None: ...
def f30(
x: float = -math.inf, # OK
) -> None: ...
def f31(
x: float = inf, # Error PYI011 Only simple default values allowed for typed arguments
) -> None: ...
def f32(
x: float = np.inf, # Error PYI011 Only simple default values allowed for typed arguments
) -> None: ...
def f33(
x: float = math.nan, # OK
) -> None: ...
def f34(
x: float = -math.nan, # Error PYI011 Only simple default values allowed for typed arguments
) -> None: ...
def f35(
x: complex = math.inf # Error PYI011 Only simple default values allowed for typed arguments
+ 1j,
) -> None: ...

View File

@@ -18,7 +18,6 @@ static REDIRECTS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
// The following are here because we don't yet have the many-to-one mapping enabled.
("SIM111", "SIM110"),
// The following are deprecated.
("C4", "C40"),
("C9", "C90"),
("T1", "T10"),
("T2", "T20"),

View File

@@ -28,6 +28,14 @@ impl Violation for ArgumentSimpleDefaults {
}
}
const ALLOWED_MATH_ATTRIBUTES_IN_DEFAULTS: &[&[&str]] = &[
&["math", "inf"],
&["math", "nan"],
&["math", "e"],
&["math", "pi"],
&["math", "tau"],
];
const ALLOWED_ATTRIBUTES_IN_DEFAULTS: &[&[&str]] = &[
&["sys", "stdin"],
&["sys", "stdout"],
@@ -99,6 +107,21 @@ fn is_valid_default_value_with_annotation(default: &Expr, checker: &Checker) ->
return checker.locator.slice(operand).len() <= 10;
}
}
// Ex) `-math.inf`, `-math.pi`, etc.
if let ExprKind::Attribute { .. } = &operand.node {
if checker
.ctx
.resolve_call_path(default)
.map_or(false, |call_path| {
ALLOWED_MATH_ATTRIBUTES_IN_DEFAULTS.iter().any(|target| {
// reject `-math.nan`
call_path.as_slice() == *target && *target != ["math", "nan"]
})
})
{
return true;
}
}
}
ExprKind::BinOp {
left,
@@ -134,14 +157,15 @@ fn is_valid_default_value_with_annotation(default: &Expr, checker: &Checker) ->
}
}
}
// Ex) `sys.stdin`, etc.
// Ex) `math.inf`, `sys.stdin`, etc.
ExprKind::Attribute { .. } => {
if checker
.ctx
.resolve_call_path(default)
.map_or(false, |call_path| {
ALLOWED_ATTRIBUTES_IN_DEFAULTS
ALLOWED_MATH_ATTRIBUTES_IN_DEFAULTS
.iter()
.chain(ALLOWED_ATTRIBUTES_IN_DEFAULTS.iter())
.any(|target| call_path.as_slice() == *target)
})
{

View File

@@ -145,4 +145,108 @@ expression: diagnostics
column: 10
fix: ~
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
location:
row: 80
column: 15
end_location:
row: 80
column: 23
fix: ~
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
location:
row: 83
column: 15
end_location:
row: 83
column: 23
fix: ~
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
location:
row: 86
column: 15
end_location:
row: 86
column: 24
fix: ~
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
location:
row: 89
column: 15
end_location:
row: 89
column: 18
fix: ~
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
location:
row: 92
column: 15
end_location:
row: 92
column: 21
fix: ~
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
location:
row: 95
column: 15
end_location:
row: 95
column: 23
fix: ~
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
location:
row: 98
column: 15
end_location:
row: 98
column: 24
fix: ~
parent: ~
- kind:
name: TypedArgumentSimpleDefaults
body: Only simple default values allowed for typed arguments
suggestion: ~
fixable: false
location:
row: 101
column: 17
end_location:
row: 102
column: 8
fix: ~
parent: ~

View File

@@ -95,6 +95,9 @@ pub struct CheckArgs {
/// Ignore any `# noqa` comments.
#[arg(long)]
ignore_noqa: bool,
/// Run in a single thread, rather than using all available cores.
#[clap(long, hide = true)]
no_parallel: bool,
/// Output serialization format for violations.
#[arg(long, value_enum, env = "RUFF_FORMAT")]
pub format: Option<SerializationFormat>,
@@ -267,6 +270,7 @@ pub struct CheckArgs {
conflicts_with = "show_settings",
// Unsupported default-command arguments.
conflicts_with = "ignore_noqa",
conflicts_with = "no_parallel",
conflicts_with = "statistics",
conflicts_with = "stdin_filename",
conflicts_with = "watch",
@@ -282,6 +286,7 @@ pub struct CheckArgs {
conflicts_with = "show_settings",
// Unsupported default-command arguments.
conflicts_with = "ignore_noqa",
conflicts_with = "no_parallel",
conflicts_with = "statistics",
conflicts_with = "stdin_filename",
conflicts_with = "watch",
@@ -296,6 +301,7 @@ pub struct CheckArgs {
// conflicts_with = "show_settings",
// Unsupported default-command arguments.
conflicts_with = "ignore_noqa",
conflicts_with = "no_parallel",
conflicts_with = "statistics",
conflicts_with = "stdin_filename",
conflicts_with = "watch",
@@ -371,6 +377,7 @@ impl CheckArgs {
ignore_noqa: self.ignore_noqa,
isolated: self.isolated,
no_cache: self.no_cache,
no_parallel: self.no_parallel,
show_files: self.show_files,
show_settings: self.show_settings,
statistics: self.statistics,
@@ -435,6 +442,7 @@ pub struct Arguments {
pub ignore_noqa: bool,
pub isolated: bool,
pub no_cache: bool,
pub no_parallel: bool,
pub show_files: bool,
pub show_settings: bool,
pub statistics: bool,

View File

@@ -140,6 +140,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
};
let cache = !cli.no_cache;
let noqa = !cli.ignore_noqa;
let parallel = !cli.no_parallel;
let mut printer_flags = PrinterFlags::empty();
if !(cli.diff || fix_only) {
printer_flags |= PrinterFlags::SHOW_VIOLATIONS;
@@ -155,6 +156,14 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
warn_user_once!("Detected debug build without --no-cache.");
}
if !parallel {
// If we're not running in parallel, we need to set up a single-threaded
// executor.
rayon::ThreadPoolBuilder::new()
.num_threads(1)
.build_global()?;
}
if cli.add_noqa {
if !matches!(autofix, fix::FixMode::None) {
warn_user_once!("--fix is incompatible with --add-noqa.");