diff --git a/Cargo.lock b/Cargo.lock index 61791812a6..2453977a0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2072,10 +2072,6 @@ dependencies = [ [[package]] name = "ruff_python_stdlib" version = "0.0.0" -dependencies = [ - "once_cell", - "rustc-hash", -] [[package]] name = "ruff_python_whitespace" diff --git a/crates/ruff/src/rules/isort/categorize.rs b/crates/ruff/src/rules/isort/categorize.rs index ac6348476d..c9ec34447a 100644 --- a/crates/ruff/src/rules/isort/categorize.rs +++ b/crates/ruff/src/rules/isort/categorize.rs @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize}; use strum_macros::EnumIter; use ruff_macros::CacheKey; -use ruff_python_stdlib::sys::KNOWN_STANDARD_LIBRARY; +use ruff_python_stdlib::sys::is_known_standard_library; use crate::settings::types::PythonVersion; use crate::warn_user_once; @@ -82,11 +82,7 @@ pub(crate) fn categorize<'a>( (&ImportSection::Known(ImportType::Future), Reason::Future) } else if let Some((import_type, reason)) = known_modules.categorize(module_name) { (import_type, reason) - } else if KNOWN_STANDARD_LIBRARY - .get(&target_version.as_tuple()) - .unwrap() - .contains(module_base) - { + } else if is_known_standard_library(target_version.minor(), module_base) { ( &ImportSection::Known(ImportType::StandardLibrary), Reason::KnownStandardLibrary, diff --git a/crates/ruff/src/settings/types.rs b/crates/ruff/src/settings/types.rs index ca813bcb0a..5395ce9ec5 100644 --- a/crates/ruff/src/settings/types.rs +++ b/crates/ruff/src/settings/types.rs @@ -52,6 +52,14 @@ impl PythonVersion { } } + pub const fn major(&self) -> u32 { + self.as_tuple().0 + } + + pub const fn minor(&self) -> u32 { + self.as_tuple().1 + } + pub fn get_minimum_supported_version(requires_version: &VersionSpecifiers) -> Option { let mut minimum_version = None; for python_version in PythonVersion::iter() { diff --git a/crates/ruff_python_stdlib/Cargo.toml b/crates/ruff_python_stdlib/Cargo.toml index 91e43d5377..167b9fad04 100644 --- a/crates/ruff_python_stdlib/Cargo.toml +++ b/crates/ruff_python_stdlib/Cargo.toml @@ -13,5 +13,3 @@ license = { workspace = true } [lib] [dependencies] -once_cell = { workspace = true } -rustc-hash = { workspace = true } diff --git a/crates/ruff_python_stdlib/src/sys.rs b/crates/ruff_python_stdlib/src/sys.rs index 791b7db2d9..c6d28db289 100644 --- a/crates/ruff_python_stdlib/src/sys.rs +++ b/crates/ruff_python_stdlib/src/sys.rs @@ -1,1107 +1,276 @@ //! 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>> = - Lazy::new(|| { - FxHashMap::from_iter([ - ( - (3, 7), - FxHashSet::from_iter([ - "_ast", - "_dummy_thread", - "_thread", - "abc", - "aifc", - "argparse", - "array", - "ast", - "asynchat", - "asyncio", - "asyncore", - "atexit", - "audioop", - "base64", - "bdb", - "binascii", - "binhex", - "bisect", - "builtins", - "bz2", - "cProfile", - "calendar", - "cgi", - "cgitb", - "chunk", - "cmath", - "cmd", - "code", - "codecs", - "codeop", - "collections", - "colorsys", - "compileall", - "concurrent", - "configparser", - "contextlib", - "contextvars", - "copy", - "copyreg", - "crypt", - "csv", - "ctypes", - "curses", - "dataclasses", - "datetime", - "dbm", - "decimal", - "difflib", - "dis", - "distutils", - "doctest", - "dummy_threading", - "email", - "encodings", - "ensurepip", - "enum", - "errno", - "faulthandler", - "fcntl", - "filecmp", - "fileinput", - "fnmatch", - "formatter", - "fractions", - "ftplib", - "functools", - "gc", - "getopt", - "getpass", - "gettext", - "glob", - "grp", - "gzip", - "hashlib", - "heapq", - "hmac", - "html", - "http", - "imaplib", - "imghdr", - "imp", - "importlib", - "inspect", - "io", - "ipaddress", - "itertools", - "json", - "keyword", - "lib2to3", - "linecache", - "locale", - "logging", - "lzma", - "macpath", - "mailbox", - "mailcap", - "marshal", - "math", - "mimetypes", - "mmap", - "modulefinder", - "msilib", - "msvcrt", - "multiprocessing", - "netrc", - "nis", - "nntplib", - "ntpath", - "numbers", - "operator", - "optparse", - "os", - "ossaudiodev", - "parser", - "pathlib", - "pdb", - "pickle", - "pickletools", - "pipes", - "pkgutil", - "platform", - "plistlib", - "poplib", - "posix", - "posixpath", - "pprint", - "profile", - "pstats", - "pty", - "pwd", - "py_compile", - "pyclbr", - "pydoc", - "queue", - "quopri", - "random", - "re", - "readline", - "reprlib", - "resource", - "rlcompleter", - "runpy", - "sched", - "secrets", - "select", - "selectors", - "shelve", - "shlex", - "shutil", - "signal", - "site", - "smtpd", - "smtplib", - "sndhdr", - "socket", - "socketserver", - "spwd", - "sqlite3", - "sre", - "sre_compile", - "sre_constants", - "sre_parse", - "ssl", - "stat", - "statistics", - "string", - "stringprep", - "struct", - "subprocess", - "sunau", - "symbol", - "symtable", - "sys", - "sysconfig", - "syslog", - "tabnanny", - "tarfile", - "telnetlib", - "tempfile", - "termios", - "test", - "textwrap", - "threading", - "time", - "timeit", - "tkinter", - "token", - "tokenize", - "trace", - "traceback", - "tracemalloc", - "tty", - "turtle", - "turtledemo", - "types", - "typing", - "unicodedata", - "unittest", - "urllib", - "uu", - "uuid", - "venv", - "warnings", - "wave", - "weakref", - "webbrowser", - "winreg", - "winsound", - "wsgiref", - "xdrlib", - "xml", - "xmlrpc", - "zipapp", - "zipfile", - "zipimport", - "zlib", - ]), - ), - ( - (3, 8), - FxHashSet::from_iter([ - "_ast", - "_dummy_thread", - "_thread", - "abc", - "aifc", - "argparse", - "array", - "ast", - "asynchat", - "asyncio", - "asyncore", - "atexit", - "audioop", - "base64", - "bdb", - "binascii", - "binhex", - "bisect", - "builtins", - "bz2", - "cProfile", - "calendar", - "cgi", - "cgitb", - "chunk", - "cmath", - "cmd", - "code", - "codecs", - "codeop", - "collections", - "colorsys", - "compileall", - "concurrent", - "configparser", - "contextlib", - "contextvars", - "copy", - "copyreg", - "crypt", - "csv", - "ctypes", - "curses", - "dataclasses", - "datetime", - "dbm", - "decimal", - "difflib", - "dis", - "distutils", - "doctest", - "dummy_threading", - "email", - "encodings", - "ensurepip", - "enum", - "errno", - "faulthandler", - "fcntl", - "filecmp", - "fileinput", - "fnmatch", - "formatter", - "fractions", - "ftplib", - "functools", - "gc", - "getopt", - "getpass", - "gettext", - "glob", - "grp", - "gzip", - "hashlib", - "heapq", - "hmac", - "html", - "http", - "imaplib", - "imghdr", - "imp", - "importlib", - "inspect", - "io", - "ipaddress", - "itertools", - "json", - "keyword", - "lib2to3", - "linecache", - "locale", - "logging", - "lzma", - "mailbox", - "mailcap", - "marshal", - "math", - "mimetypes", - "mmap", - "modulefinder", - "msilib", - "msvcrt", - "multiprocessing", - "netrc", - "nis", - "nntplib", - "ntpath", - "numbers", - "operator", - "optparse", - "os", - "ossaudiodev", - "parser", - "pathlib", - "pdb", - "pickle", - "pickletools", - "pipes", - "pkgutil", - "platform", - "plistlib", - "poplib", - "posix", - "posixpath", - "pprint", - "profile", - "pstats", - "pty", - "pwd", - "py_compile", - "pyclbr", - "pydoc", - "queue", - "quopri", - "random", - "re", - "readline", - "reprlib", - "resource", - "rlcompleter", - "runpy", - "sched", - "secrets", - "select", - "selectors", - "shelve", - "shlex", - "shutil", - "signal", - "site", - "smtpd", - "smtplib", - "sndhdr", - "socket", - "socketserver", - "spwd", - "sqlite3", - "sre", - "sre_compile", - "sre_constants", - "sre_parse", - "ssl", - "stat", - "statistics", - "string", - "stringprep", - "struct", - "subprocess", - "sunau", - "symbol", - "symtable", - "sys", - "sysconfig", - "syslog", - "tabnanny", - "tarfile", - "telnetlib", - "tempfile", - "termios", - "test", - "textwrap", - "threading", - "time", - "timeit", - "tkinter", - "token", - "tokenize", - "trace", - "traceback", - "tracemalloc", - "tty", - "turtle", - "turtledemo", - "types", - "typing", - "unicodedata", - "unittest", - "urllib", - "uu", - "uuid", - "venv", - "warnings", - "wave", - "weakref", - "webbrowser", - "winreg", - "winsound", - "wsgiref", - "xdrlib", - "xml", - "xmlrpc", - "zipapp", - "zipfile", - "zipimport", - "zlib", - ]), - ), - ( - (3, 9), - FxHashSet::from_iter([ - "_ast", - "_thread", - "abc", - "aifc", - "argparse", - "array", - "ast", - "asynchat", - "asyncio", - "asyncore", - "atexit", - "audioop", - "base64", - "bdb", - "binascii", - "binhex", - "bisect", - "builtins", - "bz2", - "cProfile", - "calendar", - "cgi", - "cgitb", - "chunk", - "cmath", - "cmd", - "code", - "codecs", - "codeop", - "collections", - "colorsys", - "compileall", - "concurrent", - "configparser", - "contextlib", - "contextvars", - "copy", - "copyreg", - "crypt", - "csv", - "ctypes", - "curses", - "dataclasses", - "datetime", - "dbm", - "decimal", - "difflib", - "dis", - "distutils", - "doctest", - "email", - "encodings", - "ensurepip", - "enum", - "errno", - "faulthandler", - "fcntl", - "filecmp", - "fileinput", - "fnmatch", - "formatter", - "fractions", - "ftplib", - "functools", - "gc", - "getopt", - "getpass", - "gettext", - "glob", - "graphlib", - "grp", - "gzip", - "hashlib", - "heapq", - "hmac", - "html", - "http", - "imaplib", - "imghdr", - "imp", - "importlib", - "inspect", - "io", - "ipaddress", - "itertools", - "json", - "keyword", - "lib2to3", - "linecache", - "locale", - "logging", - "lzma", - "mailbox", - "mailcap", - "marshal", - "math", - "mimetypes", - "mmap", - "modulefinder", - "msilib", - "msvcrt", - "multiprocessing", - "netrc", - "nis", - "nntplib", - "ntpath", - "numbers", - "operator", - "optparse", - "os", - "ossaudiodev", - "parser", - "pathlib", - "pdb", - "pickle", - "pickletools", - "pipes", - "pkgutil", - "platform", - "plistlib", - "poplib", - "posix", - "posixpath", - "pprint", - "profile", - "pstats", - "pty", - "pwd", - "py_compile", - "pyclbr", - "pydoc", - "queue", - "quopri", - "random", - "re", - "readline", - "reprlib", - "resource", - "rlcompleter", - "runpy", - "sched", - "secrets", - "select", - "selectors", - "shelve", - "shlex", - "shutil", - "signal", - "site", - "smtpd", - "smtplib", - "sndhdr", - "socket", - "socketserver", - "spwd", - "sqlite3", - "sre", - "sre_compile", - "sre_constants", - "sre_parse", - "ssl", - "stat", - "statistics", - "string", - "stringprep", - "struct", - "subprocess", - "sunau", - "symbol", - "symtable", - "sys", - "sysconfig", - "syslog", - "tabnanny", - "tarfile", - "telnetlib", - "tempfile", - "termios", - "test", - "textwrap", - "threading", - "time", - "timeit", - "tkinter", - "token", - "tokenize", - "trace", - "traceback", - "tracemalloc", - "tty", - "turtle", - "turtledemo", - "types", - "typing", - "unicodedata", - "unittest", - "urllib", - "uu", - "uuid", - "venv", - "warnings", - "wave", - "weakref", - "webbrowser", - "winreg", - "winsound", - "wsgiref", - "xdrlib", - "xml", - "xmlrpc", - "zipapp", - "zipfile", - "zipimport", - "zlib", - "zoneinfo", - ]), - ), - ( - (3, 10), - FxHashSet::from_iter([ - "_ast", - "_thread", - "abc", - "aifc", - "argparse", - "array", - "ast", - "asynchat", - "asyncio", - "asyncore", - "atexit", - "audioop", - "base64", - "bdb", - "binascii", - "binhex", - "bisect", - "builtins", - "bz2", - "cProfile", - "calendar", - "cgi", - "cgitb", - "chunk", - "cmath", - "cmd", - "code", - "codecs", - "codeop", - "collections", - "colorsys", - "compileall", - "concurrent", - "configparser", - "contextlib", - "contextvars", - "copy", - "copyreg", - "crypt", - "csv", - "ctypes", - "curses", - "dataclasses", - "datetime", - "dbm", - "decimal", - "difflib", - "dis", - "distutils", - "doctest", - "email", - "encodings", - "ensurepip", - "enum", - "errno", - "faulthandler", - "fcntl", - "filecmp", - "fileinput", - "fnmatch", - "fractions", - "ftplib", - "functools", - "gc", - "getopt", - "getpass", - "gettext", - "glob", - "graphlib", - "grp", - "gzip", - "hashlib", - "heapq", - "hmac", - "html", - "http", - "idlelib", - "imaplib", - "imghdr", - "imp", - "importlib", - "inspect", - "io", - "ipaddress", - "itertools", - "json", - "keyword", - "lib2to3", - "linecache", - "locale", - "logging", - "lzma", - "mailbox", - "mailcap", - "marshal", - "math", - "mimetypes", - "mmap", - "modulefinder", - "msilib", - "msvcrt", - "multiprocessing", - "netrc", - "nis", - "nntplib", - "ntpath", - "numbers", - "operator", - "optparse", - "os", - "ossaudiodev", - "pathlib", - "pdb", - "pickle", - "pickletools", - "pipes", - "pkgutil", - "platform", - "plistlib", - "poplib", - "posix", - "posixpath", - "pprint", - "profile", - "pstats", - "pty", - "pwd", - "py_compile", - "pyclbr", - "pydoc", - "queue", - "quopri", - "random", - "re", - "readline", - "reprlib", - "resource", - "rlcompleter", - "runpy", - "sched", - "secrets", - "select", - "selectors", - "shelve", - "shlex", - "shutil", - "signal", - "site", - "smtpd", - "smtplib", - "sndhdr", - "socket", - "socketserver", - "spwd", - "sqlite3", - "sre", - "sre_compile", - "sre_constants", - "sre_parse", - "ssl", - "stat", - "statistics", - "string", - "stringprep", - "struct", - "subprocess", - "sunau", - "symtable", - "sys", - "sysconfig", - "syslog", - "tabnanny", - "tarfile", - "telnetlib", - "tempfile", - "termios", - "test", - "textwrap", - "threading", - "time", - "timeit", - "tkinter", - "token", - "tokenize", - "trace", - "traceback", - "tracemalloc", - "tty", - "turtle", - "turtledemo", - "types", - "typing", - "unicodedata", - "unittest", - "urllib", - "uu", - "uuid", - "venv", - "warnings", - "wave", - "weakref", - "webbrowser", - "winreg", - "winsound", - "wsgiref", - "xdrlib", - "xml", - "xmlrpc", - "zipapp", - "zipfile", - "zipimport", - "zlib", - "zoneinfo", - ]), - ), - ( - (3, 11), - FxHashSet::from_iter([ - "_ast", - "_thread", - "abc", - "aifc", - "argparse", - "array", - "ast", - "asynchat", - "asyncio", - "asyncore", - "atexit", - "audioop", - "base64", - "bdb", - "binascii", - "bisect", - "builtins", - "bz2", - "cProfile", - "calendar", - "cgi", - "cgitb", - "chunk", - "cmath", - "cmd", - "code", - "codecs", - "codeop", - "collections", - "colorsys", - "compileall", - "concurrent", - "configparser", - "contextlib", - "contextvars", - "copy", - "copyreg", - "crypt", - "csv", - "ctypes", - "curses", - "dataclasses", - "datetime", - "dbm", - "decimal", - "difflib", - "dis", - "distutils", - "doctest", - "email", - "encodings", - "ensurepip", - "enum", - "errno", - "faulthandler", - "fcntl", - "filecmp", - "fileinput", - "fnmatch", - "fractions", - "ftplib", - "functools", - "gc", - "getopt", - "getpass", - "gettext", - "glob", - "graphlib", - "grp", - "gzip", - "hashlib", - "heapq", - "hmac", - "html", - "http", - "idlelib", - "imaplib", - "imghdr", - "imp", - "importlib", - "inspect", - "io", - "ipaddress", - "itertools", - "json", - "keyword", - "lib2to3", - "linecache", - "locale", - "logging", - "lzma", - "mailbox", - "mailcap", - "marshal", - "math", - "mimetypes", - "mmap", - "modulefinder", - "msilib", - "msvcrt", - "multiprocessing", - "netrc", - "nis", - "nntplib", - "ntpath", - "numbers", - "operator", - "optparse", - "os", - "ossaudiodev", - "pathlib", - "pdb", - "pickle", - "pickletools", - "pipes", - "pkgutil", - "platform", - "plistlib", - "poplib", - "posix", - "posixpath", - "pprint", - "profile", - "pstats", - "pty", - "pwd", - "py_compile", - "pyclbr", - "pydoc", - "queue", - "quopri", - "random", - "re", - "readline", - "reprlib", - "resource", - "rlcompleter", - "runpy", - "sched", - "secrets", - "select", - "selectors", - "shelve", - "shlex", - "shutil", - "signal", - "site", - "smtpd", - "smtplib", - "sndhdr", - "socket", - "socketserver", - "spwd", - "sqlite3", - "sre", - "sre_compile", - "sre_constants", - "sre_parse", - "ssl", - "stat", - "statistics", - "string", - "stringprep", - "struct", - "subprocess", - "sunau", - "symtable", - "sys", - "sysconfig", - "syslog", - "tabnanny", - "tarfile", - "telnetlib", - "tempfile", - "termios", - "test", - "textwrap", - "threading", - "time", - "timeit", - "tkinter", - "token", - "tokenize", - "tomllib", - "trace", - "traceback", - "tracemalloc", - "tty", - "turtle", - "turtledemo", - "types", - "typing", - "unicodedata", - "unittest", - "urllib", - "uu", - "uuid", - "venv", - "warnings", - "wave", - "weakref", - "webbrowser", - "winreg", - "winsound", - "wsgiref", - "xdrlib", - "xml", - "xmlrpc", - "zipapp", - "zipfile", - "zipimport", - "zlib", - "zoneinfo", - ]), - ), - ]) - }); +pub fn is_known_standard_library(minor_version: u32, module: &str) -> bool { + matches!( + (minor_version, module), + ( + _, + "_ast" + | "_thread" + | "abc" + | "aifc" + | "argparse" + | "array" + | "ast" + | "asyncio" + | "atexit" + | "audioop" + | "base64" + | "bdb" + | "binascii" + | "bisect" + | "builtins" + | "bz2" + | "cProfile" + | "calendar" + | "cgi" + | "cgitb" + | "chunk" + | "cmath" + | "cmd" + | "code" + | "codecs" + | "codeop" + | "collections" + | "colorsys" + | "compileall" + | "concurrent" + | "configparser" + | "contextlib" + | "contextvars" + | "copy" + | "copyreg" + | "crypt" + | "csv" + | "ctypes" + | "curses" + | "dataclasses" + | "datetime" + | "dbm" + | "decimal" + | "difflib" + | "dis" + | "doctest" + | "email" + | "encodings" + | "ensurepip" + | "enum" + | "errno" + | "faulthandler" + | "fcntl" + | "filecmp" + | "fileinput" + | "fnmatch" + | "fractions" + | "ftplib" + | "functools" + | "gc" + | "getopt" + | "getpass" + | "gettext" + | "glob" + | "grp" + | "gzip" + | "hashlib" + | "heapq" + | "hmac" + | "html" + | "http" + | "imaplib" + | "imghdr" + | "importlib" + | "inspect" + | "io" + | "ipaddress" + | "itertools" + | "json" + | "keyword" + | "lib2to3" + | "linecache" + | "locale" + | "logging" + | "lzma" + | "mailbox" + | "mailcap" + | "marshal" + | "math" + | "mimetypes" + | "mmap" + | "modulefinder" + | "msilib" + | "msvcrt" + | "multiprocessing" + | "netrc" + | "nis" + | "nntplib" + | "ntpath" + | "numbers" + | "operator" + | "optparse" + | "os" + | "ossaudiodev" + | "pathlib" + | "pdb" + | "pickle" + | "pickletools" + | "pipes" + | "pkgutil" + | "platform" + | "plistlib" + | "poplib" + | "posix" + | "posixpath" + | "pprint" + | "profile" + | "pstats" + | "pty" + | "pwd" + | "py_compile" + | "pyclbr" + | "pydoc" + | "queue" + | "quopri" + | "random" + | "re" + | "readline" + | "reprlib" + | "resource" + | "rlcompleter" + | "runpy" + | "sched" + | "secrets" + | "select" + | "selectors" + | "shelve" + | "shlex" + | "shutil" + | "signal" + | "site" + | "smtplib" + | "sndhdr" + | "socket" + | "socketserver" + | "spwd" + | "sqlite3" + | "sre" + | "sre_compile" + | "sre_constants" + | "sre_parse" + | "ssl" + | "stat" + | "statistics" + | "string" + | "stringprep" + | "struct" + | "subprocess" + | "sunau" + | "symtable" + | "sys" + | "sysconfig" + | "syslog" + | "tabnanny" + | "tarfile" + | "telnetlib" + | "tempfile" + | "termios" + | "test" + | "textwrap" + | "threading" + | "time" + | "timeit" + | "tkinter" + | "token" + | "tokenize" + | "trace" + | "traceback" + | "tracemalloc" + | "tty" + | "turtle" + | "turtledemo" + | "types" + | "typing" + | "unicodedata" + | "unittest" + | "urllib" + | "uu" + | "uuid" + | "venv" + | "warnings" + | "wave" + | "weakref" + | "webbrowser" + | "winreg" + | "winsound" + | "wsgiref" + | "xdrlib" + | "xml" + | "xmlrpc" + | "zipapp" + | "zipfile" + | "zipimport" + | "zlib" + ) | ( + 7, + "_dummy_thread" + | "asynchat" + | "asyncore" + | "binhex" + | "distutils" + | "dummy_threading" + | "formatter" + | "imp" + | "macpath" + | "parser" + | "smtpd" + | "symbol" + ) | ( + 8, + "_dummy_thread" + | "asynchat" + | "asyncore" + | "binhex" + | "distutils" + | "dummy_threading" + | "formatter" + | "imp" + | "parser" + | "smtpd" + | "symbol" + ) | ( + 9, + "asynchat" + | "asyncore" + | "binhex" + | "distutils" + | "formatter" + | "graphlib" + | "imp" + | "parser" + | "smtpd" + | "symbol" + | "zoneinfo" + ) | ( + 10, + "asynchat" + | "asyncore" + | "binhex" + | "distutils" + | "graphlib" + | "idlelib" + | "imp" + | "smtpd" + | "zoneinfo" + ) | ( + 11, + "asynchat" + | "asyncore" + | "distutils" + | "graphlib" + | "idlelib" + | "imp" + | "smtpd" + | "tomllib" + | "zoneinfo" + ) | (12, "graphlib" | "idlelib" | "tomllib" | "zoneinfo") + ) +} diff --git a/scripts/generate_known_standard_library.py b/scripts/generate_known_standard_library.py index 5d9c66c58b..f35a714d0e 100644 --- a/scripts/generate_known_standard_library.py +++ b/scripts/generate_known_standard_library.py @@ -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>> = - 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 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( """\ - ]) - }); -""", + ) +} + """, )