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

Removed dependency on isort #140

Merged
merged 2 commits into from
Sep 30, 2022
Merged

Removed dependency on isort #140

merged 2 commits into from
Sep 30, 2022

Conversation

fpgmaas
Copy link
Owner

@fpgmaas fpgmaas commented Sep 29, 2022

This PR removes the dependency on isort to get the standard library. The code to create the standard library files is borrowed from isort, which I think is permitted by their license. I did at a disclaimer to credit the project to the mkstdlibs.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

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • Documentation in docs is updated

Description of changes

@fpgmaas fpgmaas linked an issue Sep 29, 2022 that may be closed by this pull request
@codecov-commenter
Copy link

codecov-commenter commented Sep 29, 2022

Codecov Report

Merging #140 (77c9658) into main (ce81ea3) will decrease coverage by 0.0%.
The diff coverage is 40.0%.

@@           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             
Impacted Files Coverage Δ
deptry/module.py 89.6% <25.0%> (-0.7%) ⬇️
deptry/stdlibs/py39.py 100.0% <100.0%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@mkniewallner
Copy link
Collaborator

Since the script from isort relies on sphinx that we don't use in the project, we might want to use another source of truth for the modules.

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 generate_stdlibs.py since it's IMO clearer than mkstdlibs.py):

#!/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 __future__, but since it is in the stdlib, I don't see any reason for excluding it.

@fpgmaas
Copy link
Owner Author

fpgmaas commented Sep 30, 2022

We could have also addedSphinx as a separate dependency group, e.g. under tool.poetry.group.scripts. But I agree with you that less dependencies is better, so I have changed the PR according to your suggested script.

P.S. You might want to open a PR in isort as well, since I don't think they have Sphinx in their environment either: link.

Co-authored-by: Mathieu Kniewallner <mathieu.kniewallner@gmail.com>
@fpgmaas fpgmaas merged commit b45c8f5 into main Sep 30, 2022
@fpgmaas fpgmaas deleted the 139-isort branch September 30, 2022 12:18
fpgmaas added a commit that referenced this pull request Oct 2, 2022
* Removed dependency on isort
* updated script to generate stdlibs
Co-authored-by: Mathieu Kniewallner <mathieu.kniewallner@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove dependency on isort
3 participants