mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
Add known-standard-library for each Python version (#2491)
This commit is contained in:
parent
b232c43824
commit
ec8b827d26
7 changed files with 1222 additions and 226 deletions
91
scripts/generate_known_standard_library.py
Normal file
91
scripts/generate_known_standard_library.py
Normal file
|
@ -0,0 +1,91 @@
|
|||
"""Vendored from [scripts/mkstdlibs.py in PyCQA/isort](https://github.com/PyCQA/isort/blob/e321a670d0fefdea0e04ed9d8d696434cf49bdec/scripts/mkstdlibs.py).
|
||||
|
||||
Only the generation of the file has been modified for use in this project.
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from sphinx.ext.intersphinx import fetch_inventory
|
||||
|
||||
URL = "https://docs.python.org/{}/objects.inv"
|
||||
PATH = Path("src") / "python" / "sys.rs"
|
||||
VERSIONS = [
|
||||
("3", "7"),
|
||||
("3", "8"),
|
||||
("3", "9"),
|
||||
("3", "10"),
|
||||
("3", "11"),
|
||||
]
|
||||
|
||||
|
||||
class FakeConfig: # noqa: D101
|
||||
intersphinx_timeout = None
|
||||
tls_verify = True
|
||||
user_agent = ""
|
||||
|
||||
|
||||
class FakeApp: # noqa: D101
|
||||
srcdir = ""
|
||||
config = FakeConfig()
|
||||
|
||||
|
||||
with PATH.open("w") as f:
|
||||
f.write(
|
||||
"""\
|
||||
//! This file is generated by `scripts/generate_known_standard_library.py`
|
||||
use once_cell::sync::Lazy;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
|
||||
use crate::settings::types::PythonVersion;
|
||||
|
||||
// See: https://pycqa.github.io/isort/docs/configuration/options.html#known-standard-library
|
||||
pub static KNOWN_STANDARD_LIBRARY: Lazy<FxHashMap<PythonVersion, FxHashSet<&'static str>>> =
|
||||
Lazy::new(|| {
|
||||
FxHashMap::from_iter([
|
||||
""",
|
||||
)
|
||||
for version_info in VERSIONS:
|
||||
version = ".".join(version_info)
|
||||
url = URL.format(version)
|
||||
invdata = fetch_inventory(FakeApp(), "", url)
|
||||
|
||||
# Any modules we want to enforce across Python versions stdlib can be included in set init
|
||||
modules = {
|
||||
"_ast",
|
||||
"posixpath",
|
||||
"ntpath",
|
||||
"sre_constants",
|
||||
"sre_parse",
|
||||
"sre_compile",
|
||||
"sre",
|
||||
}
|
||||
for module in invdata["py:module"]:
|
||||
root, *_ = module.split(".")
|
||||
if root not in ["__future__", "__main__"]:
|
||||
modules.add(root)
|
||||
|
||||
f.write(
|
||||
f"""\
|
||||
(
|
||||
PythonVersion::Py{"".join(version_info)},
|
||||
FxHashSet::from_iter([
|
||||
""",
|
||||
)
|
||||
for module in sorted(modules):
|
||||
f.write(
|
||||
f"""\
|
||||
"{module}",
|
||||
""",
|
||||
)
|
||||
f.write(
|
||||
"""\
|
||||
]),
|
||||
),
|
||||
""",
|
||||
)
|
||||
f.write(
|
||||
"""\
|
||||
])
|
||||
});
|
||||
""",
|
||||
)
|
|
@ -1,3 +1,8 @@
|
|||
[project]
|
||||
name = "scripts"
|
||||
version = "0.0.1"
|
||||
dependencies = ["sphinx"]
|
||||
|
||||
[tool.ruff]
|
||||
select = ["ALL"]
|
||||
ignore = [
|
||||
|
|
1331
src/python/sys.rs
1331
src/python/sys.rs
File diff suppressed because it is too large
Load diff
|
@ -166,6 +166,7 @@ pub fn typing_only_runtime_import(
|
|||
&settings.isort.known_first_party,
|
||||
&settings.isort.known_third_party,
|
||||
&settings.isort.extra_standard_library,
|
||||
settings.target_version,
|
||||
) {
|
||||
ImportType::LocalFolder | ImportType::FirstParty => Some(Diagnostic::new(
|
||||
TypingOnlyFirstPartyImport {
|
||||
|
|
|
@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
use super::types::{ImportBlock, Importable};
|
||||
use crate::python::sys::KNOWN_STANDARD_LIBRARY;
|
||||
use crate::settings::types::PythonVersion;
|
||||
|
||||
#[derive(
|
||||
Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Serialize, Deserialize, JsonSchema, Hash,
|
||||
|
@ -34,6 +35,7 @@ enum Reason<'a> {
|
|||
NoMatch,
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn categorize(
|
||||
module_base: &str,
|
||||
level: Option<&usize>,
|
||||
|
@ -42,6 +44,7 @@ pub fn categorize(
|
|||
known_first_party: &BTreeSet<String>,
|
||||
known_third_party: &BTreeSet<String>,
|
||||
extra_standard_library: &BTreeSet<String>,
|
||||
target_version: PythonVersion,
|
||||
) -> ImportType {
|
||||
let (import_type, reason) = {
|
||||
if level.map_or(false, |level| *level > 0) {
|
||||
|
@ -54,7 +57,11 @@ pub fn categorize(
|
|||
(ImportType::StandardLibrary, Reason::ExtraStandardLibrary)
|
||||
} else if module_base == "__future__" {
|
||||
(ImportType::Future, Reason::Future)
|
||||
} else if KNOWN_STANDARD_LIBRARY.contains(module_base) {
|
||||
} else if KNOWN_STANDARD_LIBRARY
|
||||
.get(&target_version)
|
||||
.unwrap()
|
||||
.contains(module_base)
|
||||
{
|
||||
(ImportType::StandardLibrary, Reason::KnownStandardLibrary)
|
||||
} else if same_package(package, module_base) {
|
||||
(ImportType::FirstParty, Reason::SamePackage)
|
||||
|
@ -98,6 +105,7 @@ pub fn categorize_imports<'a>(
|
|||
known_first_party: &BTreeSet<String>,
|
||||
known_third_party: &BTreeSet<String>,
|
||||
extra_standard_library: &BTreeSet<String>,
|
||||
target_version: PythonVersion,
|
||||
) -> BTreeMap<ImportType, ImportBlock<'a>> {
|
||||
let mut block_by_type: BTreeMap<ImportType, ImportBlock> = BTreeMap::default();
|
||||
// Categorize `StmtKind::Import`.
|
||||
|
@ -110,6 +118,7 @@ pub fn categorize_imports<'a>(
|
|||
known_first_party,
|
||||
known_third_party,
|
||||
extra_standard_library,
|
||||
target_version,
|
||||
);
|
||||
block_by_type
|
||||
.entry(import_type)
|
||||
|
@ -127,6 +136,7 @@ pub fn categorize_imports<'a>(
|
|||
known_first_party,
|
||||
known_third_party,
|
||||
extra_standard_library,
|
||||
target_version,
|
||||
);
|
||||
block_by_type
|
||||
.entry(classification)
|
||||
|
@ -144,6 +154,7 @@ pub fn categorize_imports<'a>(
|
|||
known_first_party,
|
||||
known_third_party,
|
||||
extra_standard_library,
|
||||
target_version,
|
||||
);
|
||||
block_by_type
|
||||
.entry(classification)
|
||||
|
@ -161,6 +172,7 @@ pub fn categorize_imports<'a>(
|
|||
known_first_party,
|
||||
known_third_party,
|
||||
extra_standard_library,
|
||||
target_version,
|
||||
);
|
||||
block_by_type
|
||||
.entry(classification)
|
||||
|
|
|
@ -18,6 +18,7 @@ use types::EitherImport::{Import, ImportFrom};
|
|||
use types::{AliasData, CommentSet, EitherImport, OrderedImportBlock, TrailingComma};
|
||||
|
||||
use crate::rules::isort::types::ImportBlock;
|
||||
use crate::settings::types::PythonVersion;
|
||||
use crate::source_code::{Locator, Stylist};
|
||||
|
||||
mod annotate;
|
||||
|
@ -132,6 +133,7 @@ pub fn format_imports(
|
|||
no_lines_before: &BTreeSet<ImportType>,
|
||||
lines_after_imports: isize,
|
||||
forced_separate: &[String],
|
||||
target_version: PythonVersion,
|
||||
) -> String {
|
||||
let trailer = &block.trailer;
|
||||
let block = annotate_imports(&block.imports, comments, locator, split_on_trailing_comma);
|
||||
|
@ -162,6 +164,7 @@ pub fn format_imports(
|
|||
constants,
|
||||
variables,
|
||||
no_lines_before,
|
||||
target_version,
|
||||
);
|
||||
|
||||
if !block_output.is_empty() && !output.is_empty() {
|
||||
|
@ -218,6 +221,7 @@ fn format_import_block(
|
|||
constants: &BTreeSet<String>,
|
||||
variables: &BTreeSet<String>,
|
||||
no_lines_before: &BTreeSet<ImportType>,
|
||||
target_version: PythonVersion,
|
||||
) -> String {
|
||||
// Categorize by type (e.g., first-party vs. third-party).
|
||||
let block_by_type = categorize_imports(
|
||||
|
@ -227,6 +231,7 @@ fn format_import_block(
|
|||
known_first_party,
|
||||
known_third_party,
|
||||
extra_standard_library,
|
||||
target_version,
|
||||
);
|
||||
|
||||
let mut output = String::new();
|
||||
|
|
|
@ -120,6 +120,7 @@ pub fn organize_imports(
|
|||
&settings.isort.no_lines_before,
|
||||
settings.isort.lines_after_imports,
|
||||
&settings.isort.forced_separate,
|
||||
settings.target_version,
|
||||
);
|
||||
|
||||
// Expand the span the entire range, including leading and trailing space.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue