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
Activity
- Resolved by David Runge
- Resolved by David Runge
- Resolved by David Runge
added 1 commit
- a93742bd - fix(rules): detect unrequired Python packages
reset approvals from @dvzrv by pushing to the branch
123 def get_unrequired_deps(metadata): 124 try: 125 raw, _ = parse_email(metadata.read()) 126 parsed = Metadata.from_raw(raw) 127 except Exception: 128 return [] 129 130 pyversion = f"{sys.version_info.major}.{sys.version_info.minor}" 131 environment = {"python_version": pyversion} 132 unrequired_dep = [] 133 for dep in parsed.requires_dist: 134 if dep.marker is None: 135 continue 136 if "python_version" in str(dep.marker) and not dep.marker.evaluate(environment): 137 # Some modules on PyPi are typing-extensions while upstream calls them typing_extensions 138 unrequired_dep.append(f"python-{dep.name.replace('-', '_')}") 127 148 modules = defaultdict(set) 128 149 gir_modules = defaultdict(set) 129 150 gir_versions = defaultdict(str) 151 unrequired_packages = [] 130 152 131 153 for entry in tar: 132 154 if not entry.isfile(): 133 155 continue 134 156 f = tar.extractfile(entry) 157 if entry.name.endswith("METADATA"): changed this line in version 3 of the diff
By the way, I assum this will fix #76 ?not really, see #76 (comment 188847)Edited by Chih-Hsuan Yenadded 15 commits
-
a93742bd...bc9dd002 - 14 commits from branch
pacman:master
- 95ff01c0 - fix(rules): detect unrequired Python packages
-
a93742bd...bc9dd002 - 14 commits from branch
requested review from @dvzrv
mentioned in issue #76
mentioned in issue #97