Skip to content

Commit f45c54a

Browse files
committed
Add informative notes for the InvalidVersion error (#3776)
2 parents cf9351d + 7976b15 commit f45c54a

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

changelog.d/3776.misc.rst

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added note about using the ``--pep-517`` flag with ``pip`` to workaround
2+
``InvalidVersion`` errors for packages that are already installed in the system.

pkg_resources/__init__.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -2675,7 +2675,14 @@ def key(self):
26752675
@property
26762676
def parsed_version(self):
26772677
if not hasattr(self, "_parsed_version"):
2678-
self._parsed_version = parse_version(self.version)
2678+
try:
2679+
self._parsed_version = parse_version(self.version)
2680+
except packaging.version.InvalidVersion as ex:
2681+
info = f"(package: {self.project_name})"
2682+
if hasattr(ex, "add_note"):
2683+
ex.add_note(info) # PEP 678
2684+
raise
2685+
raise packaging.version.InvalidVersion(f"{str(ex)} {info}") from None
26792686

26802687
return self._parsed_version
26812688

setuptools/__init__.py

+21
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,28 @@ def finalize_options(self):
7777
# Honor setup.cfg's options.
7878
dist.parse_config_files(ignore_option_errors=True)
7979
if dist.setup_requires:
80+
_fetch_build_eggs(dist)
81+
82+
83+
def _fetch_build_eggs(dist):
84+
try:
8085
dist.fetch_build_eggs(dist.setup_requires)
86+
except Exception as ex:
87+
msg = """
88+
It is possible a package already installed in your system
89+
contains an version that is invalid according to PEP 440.
90+
You can try `pip install --use-pep517` as a workaround for this problem,
91+
or rely on a new virtual environment.
92+
93+
If the problem refers to a package that is not installed yet,
94+
please contact that package's maintainers or distributors.
95+
"""
96+
if "InvalidVersion" in ex.__class__.__name__:
97+
if hasattr(ex, "add_note"):
98+
ex.add_note(msg) # PEP 678
99+
else:
100+
dist.announce(f"\n{msg}\n")
101+
raise
81102

82103

83104
def setup(**attrs):

0 commit comments

Comments
 (0)