Skip to content

feat(module) use sys.stdlib_module_names to get stdlibs in Python >= 3.10#275

Merged
mkniewallner merged 5 commits intofpgmaas:mainfrom
mkniewallner:feat/use-sys-for-stdlibs-python-3.10-plus
Jan 23, 2023
Merged

feat(module) use sys.stdlib_module_names to get stdlibs in Python >= 3.10#275
mkniewallner merged 5 commits intofpgmaas:mainfrom
mkniewallner:feat/use-sys-for-stdlibs-python-3.10-plus

Conversation

@mkniewallner
Copy link
Collaborator

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

Python 3.10 introduced sys.stdlib_module_names to retrieve the list of modules that belong to the stdlib.

This PR updates the stdlib modules retrieval in order to use this method on Python >= 3.10, while older versions would still depend on the generated list of modules that we get from the official documentation.

The main advantage of using sys.stdlib_module_names is that we will not need a new release of deptry that generates stdlib modules for a new Python version whenever a new one is available.

For instance, when using the latest alpha of Python 3.12 this would now work out of the box (while it would just crash without those changes):

$ poetry run python --version
Python 3.12.0a4

$ poetry run deptry .
Warning: Package 'importlib-metadata' (conditional) not found in current environment. Assuming its corresponding module name is 'importlib_metadata'.
Warning: Package 'tomli' (conditional) not found in current environment. Assuming its corresponding module name is 'tomli'.
Scanning 31 files...
Success! No dependency issues found.

Although we would not need to re-generate the stdlibs files for previous Python versions ever again, I still wanted to rework a bit the generation of stdlibs files by using an AST, which feels cleaner than custom string indentations, since we can them leverage black for the formatting afterwards.

It now also outputs the modules to a single location, to only have a single import and simplify the logic for Python < 3.10.

@codecov-commenter
Copy link

codecov-commenter commented Jan 22, 2023

Codecov Report

Merging #275 (c2873db) into main (1a9e1fa) will decrease coverage by 0.1%.
The diff coverage is 100.0%.

@@           Coverage Diff           @@
##            main    #275     +/-   ##
=======================================
- Coverage   95.7%   95.7%   -0.1%     
=======================================
  Files         32      28      -4     
  Lines        915     907      -8     
  Branches     191     186      -5     
=======================================
- Hits         876     868      -8     
  Misses        26      26             
  Partials      13      13             
Impacted Files Coverage Δ
deptry/core.py 97.9% <100.0%> (+0.2%) ⬆️
deptry/module.py 98.3% <100.0%> (-0.4%) ⬇️
deptry/stdlibs.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 mkniewallner force-pushed the feat/use-sys-for-stdlibs-python-3.10-plus branch 2 times, most recently from 291564b to ba319fb Compare January 22, 2023 17:07
@mkniewallner mkniewallner marked this pull request as ready for review January 22, 2023 17:11
@mkniewallner mkniewallner requested a review from fpgmaas January 22, 2023 17:11

[tool.mypy]
files = ["deptry", "scripts", "tests"]
explicit_package_bases = true
Copy link
Collaborator Author

@mkniewallner mkniewallner Jan 22, 2023

Choose a reason for hiding this comment

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

scripts directory doesn't have an __init__.py file, so this makes mypy understand that scripts is a Python module (https://mypy.readthedocs.io/en/stable/config_file.html#confval-explicit_package_bases).

@mkniewallner mkniewallner force-pushed the feat/use-sys-for-stdlibs-python-3.10-plus branch from ba319fb to c2873db Compare January 23, 2023 13:13
@mkniewallner mkniewallner merged commit 4c72b74 into fpgmaas:main Jan 23, 2023
@mkniewallner mkniewallner deleted the feat/use-sys-for-stdlibs-python-3.10-plus branch January 23, 2023 15:38
@mkniewallner mkniewallner mentioned this pull request Jan 24, 2023
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.

3 participants