Skip to content

Commit

Permalink
Migrate sys.rs generation to stdlibs
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul Modpur <rmodpur2@gmail.com>
  • Loading branch information
rmodpur committed May 12, 2024
1 parent 4b330b1 commit 985a61b
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 41 deletions.
188 changes: 177 additions & 11 deletions crates/ruff_python_stdlib/src/sys.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,100 @@
//! This file is generated by `scripts/generate_known_standard_library.py`

pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
pub fn is_known_standard_library(minor_version: u32, module: &str) -> bool {
matches!(
(minor_version, module),
(
_,
"_ast"
"__hello__"
| "__phello__"
| "_abc"
| "_ast"
| "_asyncio"
| "_bisect"
| "_blake2"
| "_bz2"
| "_codecs"
| "_codecs_cn"
| "_codecs_hk"
| "_codecs_iso2022"
| "_codecs_jp"
| "_codecs_kr"
| "_codecs_tw"
| "_collections"
| "_collections_abc"
| "_compat_pickle"
| "_compression"
| "_contextvars"
| "_crypt"
| "_csv"
| "_ctypes"
| "_ctypes_test"
| "_curses"
| "_curses_panel"
| "_datetime"
| "_dbm"
| "_decimal"
| "_elementtree"
| "_frozen_importlib"
| "_frozen_importlib_external"
| "_functools"
| "_gdbm"
| "_hashlib"
| "_heapq"
| "_imp"
| "_io"
| "_json"
| "_locale"
| "_lsprof"
| "_lzma"
| "_markupbase"
| "_md5"
| "_msi"
| "_multibytecodec"
| "_multiprocessing"
| "_opcode"
| "_operator"
| "_osx_support"
| "_overlapped"
| "_pickle"
| "_posixsubprocess"
| "_py_abc"
| "_pydecimal"
| "_pyio"
| "_queue"
| "_random"
| "_scproxy"
| "_sha1"
| "_sha3"
| "_signal"
| "_sitebuiltins"
| "_socket"
| "_sqlite3"
| "_sre"
| "_ssl"
| "_stat"
| "_string"
| "_strptime"
| "_struct"
| "_symtable"
| "_testbuffer"
| "_testcapi"
| "_testconsole"
| "_testimportmultiple"
| "_testmultiphase"
| "_thread"
| "_threading_local"
| "_tkinter"
| "_tracemalloc"
| "_uuid"
| "_warnings"
| "_weakref"
| "_weakrefset"
| "_winapi"
| "_xxtestfuzz"
| "abc"
| "aifc"
| "antigravity"
| "argparse"
| "array"
| "ast"
Expand Down Expand Up @@ -65,6 +151,7 @@ pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
| "ftplib"
| "functools"
| "gc"
| "genericpath"
| "getopt"
| "getpass"
| "gettext"
Expand All @@ -76,6 +163,7 @@ pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
| "hmac"
| "html"
| "http"
| "idlelib"
| "imaplib"
| "imghdr"
| "importlib"
Expand Down Expand Up @@ -103,8 +191,11 @@ pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
| "netrc"
| "nis"
| "nntplib"
| "nt"
| "ntpath"
| "nturl2path"
| "numbers"
| "opcode"
| "operator"
| "optparse"
| "os"
Expand All @@ -128,6 +219,8 @@ pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
| "py_compile"
| "pyclbr"
| "pydoc"
| "pydoc_data"
| "pyexpat"
| "queue"
| "quopri"
| "random"
Expand Down Expand Up @@ -173,8 +266,8 @@ pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
| "telnetlib"
| "tempfile"
| "termios"
| "test"
| "textwrap"
| "this"
| "threading"
| "time"
| "timeit"
Expand Down Expand Up @@ -205,13 +298,19 @@ pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
| "xdrlib"
| "xml"
| "xmlrpc"
| "xx"
| "xxlimited"
| "xxsubtype"
| "zipapp"
| "zipfile"
| "zipimport"
| "zlib"
) | (
7,
"_dummy_thread"
"_bootlocale"
| "_dummy_thread"
| "_sha256"
| "_sha512"
| "asynchat"
| "asyncore"
| "binhex"
Expand All @@ -225,7 +324,14 @@ pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
| "symbol"
) | (
8,
"_dummy_thread"
"_bootlocale"
| "_dummy_thread"
| "_posixshmem"
| "_sha256"
| "_sha512"
| "_statistics"
| "_testinternalcapi"
| "_xxsubinterpreters"
| "asynchat"
| "asyncore"
| "binhex"
Expand All @@ -238,39 +344,99 @@ pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
| "symbol"
) | (
9,
"asynchat"
"_aix_support"
| "_bootlocale"
| "_bootsubprocess"
| "_peg_parser"
| "_posixshmem"
| "_sha256"
| "_sha512"
| "_statistics"
| "_testinternalcapi"
| "_xxsubinterpreters"
| "_zoneinfo"
| "asynchat"
| "asyncore"
| "binhex"
| "distutils"
| "formatter"
| "graphlib"
| "imp"
| "parser"
| "peg_parser"
| "smtpd"
| "symbol"
| "zoneinfo"
) | (
10,
"asynchat"
"_aix_support"
| "_bootsubprocess"
| "_posixshmem"
| "_sha256"
| "_sha512"
| "_statistics"
| "_testclinic"
| "_testinternalcapi"
| "_xxsubinterpreters"
| "_zoneinfo"
| "asynchat"
| "asyncore"
| "binhex"
| "distutils"
| "graphlib"
| "idlelib"
| "imp"
| "smtpd"
| "xxlimited_35"
| "zoneinfo"
) | (
11,
"asynchat"
"__hello_alias__"
| "__hello_only__"
| "__phello_alias__"
| "_aix_support"
| "_bootsubprocess"
| "_posixshmem"
| "_sha256"
| "_sha512"
| "_statistics"
| "_testclinic"
| "_testinternalcapi"
| "_tokenize"
| "_typing"
| "_xxsubinterpreters"
| "_zoneinfo"
| "asynchat"
| "asyncore"
| "distutils"
| "graphlib"
| "idlelib"
| "imp"
| "smtpd"
| "tomllib"
| "xxlimited_35"
| "zoneinfo"
) | (
12,
"__hello_alias__"
| "__hello_only__"
| "__phello_alias__"
| "_aix_support"
| "_posixshmem"
| "_pydatetime"
| "_pylong"
| "_sha2"
| "_statistics"
| "_testclinic"
| "_testinternalcapi"
| "_testsinglephase"
| "_tokenize"
| "_typing"
| "_xxinterpchannels"
| "_xxsubinterpreters"
| "_zoneinfo"
| "graphlib"
| "tomllib"
| "xxlimited_35"
| "zoneinfo"
) | (12, "graphlib" | "idlelib" | "tomllib" | "zoneinfo")
)
)
}
33 changes: 4 additions & 29 deletions scripts/generate_known_standard_library.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
"""Vendored from scripts/mkstdlibs.py in PyCQA/isort.
Source:
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 __future__ import annotations

from pathlib import Path

from sphinx.ext.intersphinx import fetch_inventory
from stdlibs import stdlib_module_names

URL = "https://docs.python.org/{}/objects.inv"
PATH = Path("crates") / "ruff_python_stdlib" / "src" / "sys.rs"
VERSIONS: list[tuple[int, int]] = [
(3, 7),
Expand All @@ -23,18 +14,6 @@
(3, 12),
]


class FakeConfig:
intersphinx_timeout = None
tls_verify = True
user_agent = ""


class FakeApp:
srcdir = ""
config = FakeConfig()


with PATH.open("w") as f:
f.write(
"""\
Expand All @@ -48,9 +27,6 @@ class FakeApp:
modules_by_version = {}

for major_version, minor_version in VERSIONS:
url = URL.format(f"{major_version}.{minor_version}")
invdata = fetch_inventory(FakeApp(), "", url)

modules = {
"_ast",
"posixpath",
Expand All @@ -61,10 +37,9 @@ class FakeApp:
"sre",
}

for module in invdata["py:module"]:
root, *_ = module.split(".")
if root not in ["__future__", "__main__"]:
modules.add(root)
for module in stdlib_module_names(f"{major_version}.{minor_version}"):
if module not in ["__future__", "__main__"]:
modules.add(module)

modules_by_version[minor_version] = modules

Expand Down
2 changes: 1 addition & 1 deletion scripts/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "scripts"
version = "0.0.1"
dependencies = ["sphinx"]
dependencies = ["stdlibs"]
requires-python = ">=3.8"

[tool.black]
Expand Down

0 comments on commit 985a61b

Please sign in to comment.