mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-01 12:25:45 +00:00
Remove HashMap and HashSet for known-standard-library detection (#5345)
## Summary This is a lot more concise and probably much more performant (with fewer instructions).
This commit is contained in:
parent
4b65446de6
commit
f45d1c2b84
6 changed files with 326 additions and 1149 deletions
|
|
@ -12,13 +12,14 @@ from pathlib import Path
|
|||
from sphinx.ext.intersphinx import fetch_inventory
|
||||
|
||||
URL = "https://docs.python.org/{}/objects.inv"
|
||||
PATH = Path("crates") / "ruff_python" / "src" / "sys.rs"
|
||||
PATH = Path("crates") / "ruff_python_stdlib" / "src" / "sys.rs"
|
||||
VERSIONS: list[tuple[int, int]] = [
|
||||
(3, 7),
|
||||
(3, 8),
|
||||
(3, 9),
|
||||
(3, 10),
|
||||
(3, 11),
|
||||
(3, 12),
|
||||
]
|
||||
|
||||
|
||||
|
|
@ -37,18 +38,16 @@ 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};
|
||||
|
||||
// See: https://pycqa.github.io/isort/docs/configuration/options.html#known-standard-library
|
||||
pub static KNOWN_STANDARD_LIBRARY: Lazy<FxHashMap<(u32, u32), FxHashSet<&'static str>>> =
|
||||
Lazy::new(|| {
|
||||
FxHashMap::from_iter([
|
||||
""", # noqa: E501
|
||||
pub fn is_known_standard_library(minor_version: u32, module: &str) -> bool {
|
||||
matches!((minor_version, module),
|
||||
""",
|
||||
)
|
||||
for major, minor in VERSIONS:
|
||||
version = f"{major}.{minor}"
|
||||
url = URL.format(version)
|
||||
|
||||
modules_by_version = {}
|
||||
|
||||
for major_version, minor_version in VERSIONS:
|
||||
url = URL.format(f"{major_version}.{minor_version}")
|
||||
invdata = fetch_inventory(FakeApp(), "", url)
|
||||
|
||||
modules = {
|
||||
|
|
@ -60,33 +59,44 @@ pub static KNOWN_STANDARD_LIBRARY: Lazy<FxHashMap<(u32, u32), FxHashSet<&'static
|
|||
"sre_compile",
|
||||
"sre",
|
||||
}
|
||||
|
||||
for module in invdata["py:module"]:
|
||||
root, *_ = module.split(".")
|
||||
if root not in ["__future__", "__main__"]:
|
||||
modules.add(root)
|
||||
|
||||
f.write(
|
||||
f"""\
|
||||
(
|
||||
({major}, {minor}),
|
||||
FxHashSet::from_iter([
|
||||
""",
|
||||
)
|
||||
for module in sorted(modules):
|
||||
f.write(
|
||||
f"""\
|
||||
"{module}",
|
||||
""",
|
||||
)
|
||||
f.write(
|
||||
"""\
|
||||
]),
|
||||
),
|
||||
""",
|
||||
modules_by_version[minor_version] = modules
|
||||
|
||||
# First, add a case for the modules that are in all versions.
|
||||
ubiquitous_modules = set.intersection(*modules_by_version.values())
|
||||
|
||||
f.write("(_, ")
|
||||
for i, module in enumerate(sorted(ubiquitous_modules)):
|
||||
if i > 0:
|
||||
f.write(" | ")
|
||||
f.write(f'"{module}"')
|
||||
f.write(")")
|
||||
f.write("\n")
|
||||
|
||||
# Next, add any version-specific modules.
|
||||
for _major_version, minor_version in VERSIONS:
|
||||
version_modules = set.difference(
|
||||
modules_by_version[minor_version],
|
||||
ubiquitous_modules,
|
||||
)
|
||||
|
||||
f.write(" | ")
|
||||
f.write(f"({minor_version}, ")
|
||||
for i, module in enumerate(sorted(version_modules)):
|
||||
if i > 0:
|
||||
f.write(" | ")
|
||||
f.write(f'"{module}"')
|
||||
f.write(")")
|
||||
f.write("\n")
|
||||
|
||||
f.write(
|
||||
"""\
|
||||
])
|
||||
});
|
||||
""",
|
||||
)
|
||||
}
|
||||
""",
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue