Skip to content

Commit 52f71fa

Browse files
MichaReisersharkdp
authored andcommitted
[red-knot] Default python-platform to current platform
1 parent dc02732 commit 52f71fa

File tree

7 files changed

+51
-32
lines changed

7 files changed

+51
-32
lines changed

crates/red_knot_project/src/metadata/options.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,25 @@ impl Options {
5454
project_root: &SystemPath,
5555
system: &dyn System,
5656
) -> ProgramSettings {
57-
let (python_version, python_platform) = self
57+
let python_version = self
5858
.environment
5959
.as_ref()
60-
.map(|env| {
61-
(
62-
env.python_version.as_deref().copied(),
63-
env.python_platform.as_deref(),
64-
)
65-
})
60+
.and_then(|env| env.python_version.as_deref().copied())
6661
.unwrap_or_default();
67-
62+
let python_platform = self
63+
.environment
64+
.as_ref()
65+
.and_then(|env| env.python_platform.as_deref().cloned())
66+
.unwrap_or_else(|| {
67+
let default = PythonPlatform::default();
68+
tracing::info!(
69+
"Defaulting to default python version for this platform: '{default}'",
70+
);
71+
default
72+
});
6873
ProgramSettings {
69-
python_version: python_version.unwrap_or_default(),
70-
python_platform: python_platform.cloned().unwrap_or_default(),
74+
python_version,
75+
python_platform,
7176
search_paths: self.to_search_path_settings(project_root, system),
7277
}
7378
}
@@ -237,7 +242,12 @@ pub struct EnvironmentOptions {
237242
/// If specified, Red Knot will tailor its use of type stub files,
238243
/// which conditionalize type definitions based on the platform.
239244
///
240-
/// If no platform is specified, knot will use `all` or the current platform in the LSP use case.
245+
/// If no platform is specified, knot will use current platform:
246+
/// - `win32` for Windows
247+
/// - `darwin` for macOS
248+
/// - `android` for Android
249+
/// - `ios` for iOS
250+
/// - `linux` for everything else
241251
#[serde(skip_serializing_if = "Option::is_none")]
242252
pub python_platform: Option<RangedValue<PythonPlatform>>,
243253

crates/red_knot_python_semantic/resources/mdtest/call/getattr_static.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Basic usage
44

5+
```toml
6+
[environment]
7+
python-platform = "all"
8+
```
9+
510
`inspect.getattr_static` is a function that returns attributes of an object without invoking the
611
descriptor protocol (for caveats, see the [official documentation]).
712

crates/red_knot_python_semantic/resources/mdtest/sys_platform.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
# `sys.platform`
22

3-
## Default value
3+
## Explicit selection of `all` platforms
44

5-
When no target platform is specified, we fall back to the type of `sys.platform` declared in
5+
When `python-platform="all"` is specified, we fall back to the type of `sys.platform` declared in
66
typeshed:
77

8-
```toml
9-
[environment]
10-
# No python-platform entry
11-
```
12-
13-
```py
14-
import sys
15-
16-
reveal_type(sys.platform) # revealed: LiteralString
17-
```
18-
19-
## Explicit selection of `all` platforms
20-
218
```toml
229
[environment]
2310
python-platform = "all"

crates/red_knot_python_semantic/resources/mdtest/unreachable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ If `python-platform` is not specified, we currently default to `all`:
201201

202202
```toml
203203
[environment]
204-
# python-platform not specified
204+
python-platform = "all"
205205
```
206206

207207
```py

crates/red_knot_python_semantic/src/python_platform.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use std::fmt::{Display, Formatter};
22

33
/// The target platform to assume when resolving types.
4-
#[derive(Debug, Clone, Default, PartialEq, Eq)]
4+
#[derive(Debug, Clone, PartialEq, Eq)]
55
#[cfg_attr(
66
feature = "serde",
77
derive(serde::Serialize, serde::Deserialize),
88
serde(rename_all = "kebab-case")
99
)]
1010
pub enum PythonPlatform {
1111
/// Do not make any assumptions about the target platform.
12-
#[default]
1312
All,
1413

1514
/// Assume a specific target platform like `linux`, `darwin` or `win32`.
@@ -39,6 +38,22 @@ impl Display for PythonPlatform {
3938
}
4039
}
4140

41+
impl Default for PythonPlatform {
42+
fn default() -> Self {
43+
if cfg!(target_os = "windows") {
44+
PythonPlatform::Identifier("win32".to_string())
45+
} else if cfg!(target_os = "macos") {
46+
PythonPlatform::Identifier("darwin".to_string())
47+
} else if cfg!(target_os = "android") {
48+
PythonPlatform::Identifier("android".to_string())
49+
} else if cfg!(target_os = "ios") {
50+
PythonPlatform::Identifier("ios".to_string())
51+
} else {
52+
PythonPlatform::Identifier("linux".to_string())
53+
}
54+
}
55+
}
56+
4257
#[cfg(feature = "schemars")]
4358
mod schema {
4459
use crate::PythonPlatform;

crates/red_knot_test/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use config::SystemKind;
66
use parser as test_parser;
77
use red_knot_python_semantic::types::check_types;
88
use red_knot_python_semantic::{
9-
Program, ProgramSettings, PythonPath, SearchPathSettings, SysPrefixPathOrigin,
9+
Program, ProgramSettings, PythonPath, PythonPlatform, SearchPathSettings, SysPrefixPathOrigin,
1010
};
1111
use ruff_db::diagnostic::{create_parse_diagnostic, Diagnostic, DisplayDiagnosticConfig};
1212
use ruff_db::files::{system_path_to_file, File};
@@ -265,7 +265,9 @@ fn run_test(
265265

266266
let settings = ProgramSettings {
267267
python_version,
268-
python_platform: configuration.python_platform().unwrap_or_default(),
268+
python_platform: configuration
269+
.python_platform()
270+
.unwrap_or(PythonPlatform::Identifier("linux".to_string())),
269271
search_paths: SearchPathSettings {
270272
src_roots: vec![src_path],
271273
extra_paths: configuration.extra_paths().unwrap_or_default().to_vec(),

knot.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
]
9090
},
9191
"python-platform": {
92-
"description": "Specifies the target platform that will be used to analyze the source code. If specified, Red Knot will tailor its use of type stub files, which conditionalize type definitions based on the platform.\n\nIf no platform is specified, knot will use `all` or the current platform in the LSP use case.",
92+
"description": "Specifies the target platform that will be used to analyze the source code. If specified, Red Knot will tailor its use of type stub files, which conditionalize type definitions based on the platform.\n\nIf no platform is specified, knot will use current platform: - `win32` for Windows - `darwin` for macOS - `android` for Android - `ios` for iOS - `linux` for everything else",
9393
"anyOf": [
9494
{
9595
"$ref": "#/definitions/PythonPlatform"

0 commit comments

Comments
 (0)