Skip to content

fix(rules): detect unrequired Python packages

When using PEP517 to build a Python package a RECORD file is included containing all the dependencies of a package. For backports (like importlib_metadata) projects usually specify until which Python version it is required combined with an optional import usually by checking sys.version_info and comparing the Python version.

As using the AST to figure out if a package is required is tricky and a lot of work and finding it out via shadowed imports of builtins for example with ExceptionGroup can work as shown below, but does not hold up for importlib.metadata as the backport is called importlib_metadata.

{ 'builtins.ExceptionGroup': {'usr/bin/optional.py'}, 'exceptiongroup.ExceptionGroup': {'usr/bin/optional.py'} }

Instead of these methods, rely on the project to provide a marker for example python_version < 3.11 for required packages.

Merge request reports