Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate sys.rs generation to stdlibs #11374

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 176 additions & 10 deletions crates/ruff_python_stdlib/src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,96 @@ pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
(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")
)
)
}
35 changes: 5 additions & 30 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,34 +14,19 @@
(3, 12),
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdlibs also supports 3.13 already, btw: https://gist.github.com/thatch/0c7015ef2e0b6c457eb52a558f14beb1



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


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


with PATH.open("w") as f:
f.write(
"""\
//! This file is generated by `scripts/generate_known_standard_library.py`

pub fn is_known_standard_library(minor_version: u32, module: &str) -> bool {
pub fn is_known_standard_library(minor_version: u8, module: &str) -> bool {
matches!((minor_version, module),
""",
)

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",
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These hardcoded ones should probably be removed and change to modules = {} -- we haven't had sre since 2.7. I suspect these were added to work around bugs in the sphinx inventory?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think __main__ would ever be reported as stdlib.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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
Loading