Add known-standard-library for each Python version (#2491)

This commit is contained in:
Jonathan Plasse 2023-02-02 22:22:47 +01:00 committed by GitHub
parent b232c43824
commit ec8b827d26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1222 additions and 226 deletions

View 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(
"""\
])
});
""",
)

View File

@ -1,3 +1,8 @@
[project]
name = "scripts"
version = "0.0.1"
dependencies = ["sphinx"]
[tool.ruff] [tool.ruff]
select = ["ALL"] select = ["ALL"]
ignore = [ ignore = [

File diff suppressed because it is too large Load Diff

View File

@ -166,6 +166,7 @@ pub fn typing_only_runtime_import(
&settings.isort.known_first_party, &settings.isort.known_first_party,
&settings.isort.known_third_party, &settings.isort.known_third_party,
&settings.isort.extra_standard_library, &settings.isort.extra_standard_library,
settings.target_version,
) { ) {
ImportType::LocalFolder | ImportType::FirstParty => Some(Diagnostic::new( ImportType::LocalFolder | ImportType::FirstParty => Some(Diagnostic::new(
TypingOnlyFirstPartyImport { TypingOnlyFirstPartyImport {

View File

@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
use super::types::{ImportBlock, Importable}; use super::types::{ImportBlock, Importable};
use crate::python::sys::KNOWN_STANDARD_LIBRARY; use crate::python::sys::KNOWN_STANDARD_LIBRARY;
use crate::settings::types::PythonVersion;
#[derive( #[derive(
Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Serialize, Deserialize, JsonSchema, Hash, Debug, PartialOrd, Ord, PartialEq, Eq, Clone, Serialize, Deserialize, JsonSchema, Hash,
@ -34,6 +35,7 @@ enum Reason<'a> {
NoMatch, NoMatch,
} }
#[allow(clippy::too_many_arguments)]
pub fn categorize( pub fn categorize(
module_base: &str, module_base: &str,
level: Option<&usize>, level: Option<&usize>,
@ -42,6 +44,7 @@ pub fn categorize(
known_first_party: &BTreeSet<String>, known_first_party: &BTreeSet<String>,
known_third_party: &BTreeSet<String>, known_third_party: &BTreeSet<String>,
extra_standard_library: &BTreeSet<String>, extra_standard_library: &BTreeSet<String>,
target_version: PythonVersion,
) -> ImportType { ) -> ImportType {
let (import_type, reason) = { let (import_type, reason) = {
if level.map_or(false, |level| *level > 0) { if level.map_or(false, |level| *level > 0) {
@ -54,7 +57,11 @@ pub fn categorize(
(ImportType::StandardLibrary, Reason::ExtraStandardLibrary) (ImportType::StandardLibrary, Reason::ExtraStandardLibrary)
} else if module_base == "__future__" { } else if module_base == "__future__" {
(ImportType::Future, Reason::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) (ImportType::StandardLibrary, Reason::KnownStandardLibrary)
} else if same_package(package, module_base) { } else if same_package(package, module_base) {
(ImportType::FirstParty, Reason::SamePackage) (ImportType::FirstParty, Reason::SamePackage)
@ -98,6 +105,7 @@ pub fn categorize_imports<'a>(
known_first_party: &BTreeSet<String>, known_first_party: &BTreeSet<String>,
known_third_party: &BTreeSet<String>, known_third_party: &BTreeSet<String>,
extra_standard_library: &BTreeSet<String>, extra_standard_library: &BTreeSet<String>,
target_version: PythonVersion,
) -> BTreeMap<ImportType, ImportBlock<'a>> { ) -> BTreeMap<ImportType, ImportBlock<'a>> {
let mut block_by_type: BTreeMap<ImportType, ImportBlock> = BTreeMap::default(); let mut block_by_type: BTreeMap<ImportType, ImportBlock> = BTreeMap::default();
// Categorize `StmtKind::Import`. // Categorize `StmtKind::Import`.
@ -110,6 +118,7 @@ pub fn categorize_imports<'a>(
known_first_party, known_first_party,
known_third_party, known_third_party,
extra_standard_library, extra_standard_library,
target_version,
); );
block_by_type block_by_type
.entry(import_type) .entry(import_type)
@ -127,6 +136,7 @@ pub fn categorize_imports<'a>(
known_first_party, known_first_party,
known_third_party, known_third_party,
extra_standard_library, extra_standard_library,
target_version,
); );
block_by_type block_by_type
.entry(classification) .entry(classification)
@ -144,6 +154,7 @@ pub fn categorize_imports<'a>(
known_first_party, known_first_party,
known_third_party, known_third_party,
extra_standard_library, extra_standard_library,
target_version,
); );
block_by_type block_by_type
.entry(classification) .entry(classification)
@ -161,6 +172,7 @@ pub fn categorize_imports<'a>(
known_first_party, known_first_party,
known_third_party, known_third_party,
extra_standard_library, extra_standard_library,
target_version,
); );
block_by_type block_by_type
.entry(classification) .entry(classification)

View File

@ -18,6 +18,7 @@ use types::EitherImport::{Import, ImportFrom};
use types::{AliasData, CommentSet, EitherImport, OrderedImportBlock, TrailingComma}; use types::{AliasData, CommentSet, EitherImport, OrderedImportBlock, TrailingComma};
use crate::rules::isort::types::ImportBlock; use crate::rules::isort::types::ImportBlock;
use crate::settings::types::PythonVersion;
use crate::source_code::{Locator, Stylist}; use crate::source_code::{Locator, Stylist};
mod annotate; mod annotate;
@ -132,6 +133,7 @@ pub fn format_imports(
no_lines_before: &BTreeSet<ImportType>, no_lines_before: &BTreeSet<ImportType>,
lines_after_imports: isize, lines_after_imports: isize,
forced_separate: &[String], forced_separate: &[String],
target_version: PythonVersion,
) -> String { ) -> String {
let trailer = &block.trailer; let trailer = &block.trailer;
let block = annotate_imports(&block.imports, comments, locator, split_on_trailing_comma); let block = annotate_imports(&block.imports, comments, locator, split_on_trailing_comma);
@ -162,6 +164,7 @@ pub fn format_imports(
constants, constants,
variables, variables,
no_lines_before, no_lines_before,
target_version,
); );
if !block_output.is_empty() && !output.is_empty() { if !block_output.is_empty() && !output.is_empty() {
@ -218,6 +221,7 @@ fn format_import_block(
constants: &BTreeSet<String>, constants: &BTreeSet<String>,
variables: &BTreeSet<String>, variables: &BTreeSet<String>,
no_lines_before: &BTreeSet<ImportType>, no_lines_before: &BTreeSet<ImportType>,
target_version: PythonVersion,
) -> String { ) -> String {
// Categorize by type (e.g., first-party vs. third-party). // Categorize by type (e.g., first-party vs. third-party).
let block_by_type = categorize_imports( let block_by_type = categorize_imports(
@ -227,6 +231,7 @@ fn format_import_block(
known_first_party, known_first_party,
known_third_party, known_third_party,
extra_standard_library, extra_standard_library,
target_version,
); );
let mut output = String::new(); let mut output = String::new();

View File

@ -120,6 +120,7 @@ pub fn organize_imports(
&settings.isort.no_lines_before, &settings.isort.no_lines_before,
settings.isort.lines_after_imports, settings.isort.lines_after_imports,
&settings.isort.forced_separate, &settings.isort.forced_separate,
settings.target_version,
); );
// Expand the span the entire range, including leading and trailing space. // Expand the span the entire range, including leading and trailing space.