-
Notifications
You must be signed in to change notification settings - Fork 23
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
Removed dependency on isort #140
Conversation
Codecov Report
@@ Coverage Diff @@
## main #140 +/- ##
=======================================
- Coverage 95.0% 94.9% -0.1%
=======================================
Files 35 36 +1
Lines 1180 1176 -4
=======================================
- Hits 1121 1117 -4
Misses 59 59
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Since the script from https://docs.python.org/3/py-modindex.html also lists all the stdlib modules, so based on that, we could write the following script (that I also renamed to #!/usr/bin/env python3
# This script is inspired by isort: https://github.com/PyCQA/isort/blob/4ccbd1eddf564d2c9e79c59d59c1fc06a7e35f94/scripts/mkstdlibs.py.
import urllib.request
from html.parser import HTMLParser
from typing import List
from typing import Optional
from typing import Tuple
OUTPUT_PATH = "deptry/stdlibs/py{}.py"
STDLIB_MODULES_URL = "https://docs.python.org/{}/py-modindex.html"
PYTHON_VERSIONS = (("3", "7"), ("3", "8"), ("3", "9"), ("3", "10"))
# Modules that are in stdlib, but undocumented.
EXTRA_STDLIBS_MODULES = ("_ast", "ntpath", "posixpath", "sre", "sre_constants", "sre_compile", "sre_parse")
DOCSTRING_GENERATED_FILES = """
DO NOT EDIT THIS FILE MANUALLY.
It is generated from `scripts/generate_stdlibs.py` script and contains the stdlib modules for Python {}.
You can generate it again using `poetry run scripts/generate_stdlibs.py`.
"""
class PythonStdlibHTMLParser(HTMLParser):
def __init__(self) -> None:
super().__init__()
self._is_in_code_tag = False
self.modules: List[str] = []
def handle_starttag(self, tag: str, attrs: List[Tuple[str, Optional[str]]]) -> None:
if tag == "code":
self._is_in_code_tag = True
def handle_endtag(self, tag: str) -> None:
if tag == "code":
self._is_in_code_tag = False
def handle_data(self, data: str) -> None:
if self._is_in_code_tag:
self.modules.append(data)
for python_version in PYTHON_VERSIONS:
dotted_python_version = ".".join(python_version)
with urllib.request.urlopen(STDLIB_MODULES_URL.format(dotted_python_version)) as response:
html_content = response.read().decode()
parser = PythonStdlibHTMLParser()
parser.feed(html_content)
modules = {module.split(".")[0] for module in parser.modules}.union(EXTRA_STDLIBS_MODULES)
modules.remove("__main__")
with open(OUTPUT_PATH.format("".join(python_version)), "w") as python_stdlib_file:
python_stdlib_file.write(f'"""{DOCSTRING_GENERATED_FILES.format(dotted_python_version)}"""\n\n')
python_stdlib_file.write("stdlib = {\n")
for module in sorted(modules):
python_stdlib_file.write(f' "{module}",\n')
python_stdlib_file.write("}\n") The slight difference is that this will add |
We could have also added P.S. You might want to open a PR in |
Co-authored-by: Mathieu Kniewallner <mathieu.kniewallner@gmail.com>
* Removed dependency on isort * updated script to generate stdlibs Co-authored-by: Mathieu Kniewallner <mathieu.kniewallner@gmail.com>
This PR removes the dependency on
isort
to get the standard library. The code to create the standard library files is borrowed fromisort
, which I think is permitted by their license. I did at a disclaimer to credit the project to themkstdlibs.py
file.I have chosen to not use the built-in function for Python 3.10, but to eep the approach consistent for the various supported Python versions.
PR Checklist
docs
is updatedDescription of changes