-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathsub_configs.rs
More file actions
357 lines (293 loc) · 11.7 KB
/
sub_configs.rs
File metadata and controls
357 lines (293 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
use configparser::ini::Ini;
use itertools::Itertools as _;
use pyrefly_util::globs::Glob;
use crate::base::ConfigBase;
use crate::config::ConfigFile;
use crate::config::SubConfig;
use crate::error::ErrorDisplayConfig;
use crate::migration::config_option_migrater::ConfigOptionMigrater;
use crate::migration::mypy::util;
use crate::migration::pyright::PyrightConfig;
/// Configuration option for sub-configs (per-module options)
pub struct SubConfigs;
impl ConfigOptionMigrater for SubConfigs {
fn migrate_from_mypy(
&self,
mypy_cfg: &Ini,
pyrefly_cfg: &mut ConfigFile,
) -> anyhow::Result<()> {
let mut sub_configs: Vec<(String, ErrorDisplayConfig)> = vec![];
// Check all sections for per-module options
util::visit_ini_sections(
mypy_cfg,
|section_name| section_name.starts_with("mypy-"),
|section_name, ini| {
if let Some(stripped) = section_name.strip_prefix("mypy-") {
// For subconfigs, the only config that needs to be extracted is enable/disable error codes.
let disable_error_code =
util::string_to_array(&ini.get(section_name, "disable_error_code"));
let enable_error_code =
util::string_to_array(&ini.get(section_name, "enable_error_code"));
if disable_error_code.is_empty() && enable_error_code.is_empty() {
return;
}
if let Some(error_config) =
util::make_error_config(None, disable_error_code, enable_error_code)
{
sub_configs.push((stripped.to_owned(), error_config));
}
}
},
);
if sub_configs.is_empty() {
return Err(anyhow::anyhow!("No sub configs found in mypy config"));
}
let sub_configs_vec = sub_configs
.into_iter()
.map(|(section, errors)| -> anyhow::Result<SubConfig> {
// Split the section headers into individual modules and pair them with the section's error config.
// mypy uses module wildcards for its per-module sections, but we use globs.
// A simple translation: turn `.` into `/` and `*` into `**`, e.g. `a.*.b` -> `a/**/b`.
let matches = section
.split(",")
.map(|x| x.trim())
.filter(|x| !x.is_empty())
.map(|module| Glob::new(module.replace('.', "/").replace('*', "**")))
.collect::<Result<Vec<_>, _>>()?;
Ok(SubConfig {
matches,
settings: ConfigBase {
errors: Some(errors),
..Default::default()
},
})
})
.process_results(|i| i.collect::<Vec<_>>())?;
if sub_configs_vec.is_empty() {
return Err(anyhow::anyhow!("No valid sub configs found in mypy config"));
}
pyrefly_cfg.sub_configs = sub_configs_vec;
Ok(())
}
fn migrate_from_pyright(
&self,
pyright_cfg: &PyrightConfig,
pyrefly_cfg: &mut ConfigFile,
) -> anyhow::Result<()> {
// In pyright, sub configs are specified in the "executionEnvironments" field
// Each execution environment has a root path and error settings
let sub_configs: Vec<SubConfig> = pyright_cfg
.execution_environments
.iter()
.map(|env| env.clone().convert())
.process_results(|i| i.collect())?;
if sub_configs.is_empty() {
return Err(anyhow::anyhow!(
"No execution environments found in pyright config"
));
}
pyrefly_cfg.sub_configs = sub_configs;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error_kind::ErrorKind;
use crate::error_kind::Severity;
use crate::migration::pyright::ExecEnv;
use crate::migration::pyright::RuleOverrides;
use crate::migration::test_util::default_pyright_config;
#[test]
fn test_migrate_from_mypy_with_single_module() {
let mut mypy_cfg = Ini::new();
mypy_cfg.set(
"mypy-app.models",
"disable_error_code",
Some("union-attr".to_owned()),
);
let mut pyrefly_cfg = ConfigFile::default();
let sub_configs = SubConfigs;
let _ = sub_configs.migrate_from_mypy(&mypy_cfg, &mut pyrefly_cfg);
assert_eq!(pyrefly_cfg.sub_configs.len(), 1);
let sub_config = &pyrefly_cfg.sub_configs[0];
assert_eq!(sub_config.matches[0].to_string(), "app/models");
let errors = sub_config.settings.errors.as_ref().unwrap();
assert_eq!(
errors.severity(ErrorKind::MissingAttribute),
Severity::Ignore
);
}
#[test]
fn test_migrate_from_mypy_with_multiple_modules() {
let mut mypy_cfg = Ini::new();
mypy_cfg.set(
"mypy-app.models",
"disable_error_code",
Some("union-attr".to_owned()),
);
mypy_cfg.set(
"mypy-app.views",
"enable_error_code",
Some("union-attr".to_owned()),
);
let mut pyrefly_cfg = ConfigFile::default();
let sub_configs = SubConfigs;
let _ = sub_configs.migrate_from_mypy(&mypy_cfg, &mut pyrefly_cfg);
assert_eq!(pyrefly_cfg.sub_configs.len(), 2);
// Find the sub_config for app.models
let models_config = pyrefly_cfg
.sub_configs
.iter()
.find(|c| c.matches.iter().any(|glob| glob.to_string() == "app/models"))
.unwrap();
let models_errors = models_config.settings.errors.as_ref().unwrap();
assert_eq!(
models_errors.severity(ErrorKind::MissingAttribute),
Severity::Ignore
);
// Find the sub_config for app.views
let views_config = pyrefly_cfg
.sub_configs
.iter()
.find(|c| c.matches.iter().any(|glob| glob.to_string() == "app/views"))
.unwrap();
let views_errors = views_config.settings.errors.as_ref().unwrap();
assert_eq!(
views_errors.severity(ErrorKind::MissingAttribute),
Severity::Error
);
}
#[test]
fn test_migrate_from_mypy_with_no_error_codes() {
let mut mypy_cfg = Ini::new();
mypy_cfg.set("mypy-app.models", "follow_imports", Some("skip".to_owned()));
let mut pyrefly_cfg = ConfigFile::default();
let default_sub_configs = pyrefly_cfg.sub_configs.clone();
let sub_configs = SubConfigs;
let _ = sub_configs.migrate_from_mypy(&mypy_cfg, &mut pyrefly_cfg);
assert_eq!(pyrefly_cfg.sub_configs, default_sub_configs);
}
#[test]
fn test_migrate_from_mypy_with_comma_separated_modules() {
let mut mypy_cfg = Ini::new();
mypy_cfg.set(
"mypy-app.models, app.views",
"disable_error_code",
Some("union-attr".to_owned()),
);
let mut pyrefly_cfg = ConfigFile::default();
let sub_configs = SubConfigs;
let _ = sub_configs.migrate_from_mypy(&mypy_cfg, &mut pyrefly_cfg);
assert_eq!(pyrefly_cfg.sub_configs.len(), 1);
// Check that both modules share the same error config in one sub-config.
let models_config = pyrefly_cfg
.sub_configs
.iter()
.find(|c| c.matches.iter().any(|glob| glob.to_string() == "app/models"))
.unwrap();
assert!(models_config
.matches
.iter()
.any(|glob| glob.to_string() == "app/views"));
let models_errors = models_config.settings.errors.as_ref().unwrap();
assert_eq!(
models_errors.severity(ErrorKind::MissingAttribute),
Severity::Ignore
);
}
#[test]
fn test_migrate_from_mypy_with_module_wildcards() {
let mut mypy_cfg = Ini::new();
mypy_cfg.set(
"mypy-app.*.models",
"disable_error_code",
Some("union-attr".to_owned()),
);
let mut pyrefly_cfg = ConfigFile::default();
let sub_configs = SubConfigs;
let _ = sub_configs.migrate_from_mypy(&mypy_cfg, &mut pyrefly_cfg);
assert_eq!(pyrefly_cfg.sub_configs.len(), 1);
let sub_config = &pyrefly_cfg.sub_configs[0];
// Check that the module wildcard was converted to a glob
assert_eq!(sub_config.matches[0].to_string(), "app/**/models");
let errors = sub_config.settings.errors.as_ref().unwrap();
assert_eq!(
errors.severity(ErrorKind::MissingAttribute),
Severity::Ignore
);
}
#[test]
fn test_migrate_from_mypy_with_empty_config() {
let mypy_cfg = Ini::new();
let mut pyrefly_cfg = ConfigFile::default();
let default_sub_configs = pyrefly_cfg.sub_configs.clone();
let sub_configs = SubConfigs;
let _ = sub_configs.migrate_from_mypy(&mypy_cfg, &mut pyrefly_cfg);
assert_eq!(pyrefly_cfg.sub_configs, default_sub_configs);
}
#[test]
fn test_migrate_from_pyright() {
let mut pyright_cfg = default_pyright_config();
// Create execution environments with different error settings
let env1 = ExecEnv {
root: "src".to_owned(),
errors: RuleOverrides {
report_missing_imports: Some(Severity::Ignore),
..Default::default()
},
};
let env2 = ExecEnv {
root: "tests".to_owned(),
errors: RuleOverrides {
report_missing_module_source: Some(Severity::Error),
..Default::default()
},
};
pyright_cfg.execution_environments = vec![env1, env2];
let mut pyrefly_cfg = ConfigFile::default();
let sub_configs = SubConfigs;
let result = sub_configs.migrate_from_pyright(&pyright_cfg, &mut pyrefly_cfg);
assert!(result.is_ok());
assert_eq!(pyrefly_cfg.sub_configs.len(), 2);
// Find the sub_config for src
let src_config = pyrefly_cfg
.sub_configs
.iter()
.find(|c| c.matches.iter().any(|glob| glob.to_string() == "src"))
.unwrap();
// Find the sub_config for tests
let tests_config = pyrefly_cfg
.sub_configs
.iter()
.find(|c| c.matches.iter().any(|glob| glob.to_string() == "tests"))
.unwrap();
// Verify that the error settings were properly migrated
let src_errors = src_config.settings.errors.as_ref().unwrap();
let tests_errors = tests_config.settings.errors.as_ref().unwrap();
assert_eq!(
src_errors.severity(ErrorKind::MissingImport),
Severity::Ignore
);
assert_eq!(
tests_errors.severity(ErrorKind::MissingImport),
Severity::Error
);
}
#[test]
fn test_migrate_from_pyright_empty() {
let pyright_cfg = default_pyright_config();
let mut pyrefly_cfg = ConfigFile::default();
let default_sub_configs = pyrefly_cfg.sub_configs.clone();
let sub_configs = SubConfigs;
let result = sub_configs.migrate_from_pyright(&pyright_cfg, &mut pyrefly_cfg);
assert!(result.is_err());
assert_eq!(pyrefly_cfg.sub_configs, default_sub_configs);
}
}