Skip to content

Commit f13c298

Browse files
authored
Clarify version split/join usage (#725)
This adds a docstring on _version_join to clarify its expectation on the input value. Since this is a strictly internal function, I also removed the assert statement so it does not fire at runtime and break user code. If it ever does fire, it would be due to a bug in packaging and we should fix it instead. Both _version_split and _version_join also received slight refactoring.
1 parent c52d2b3 commit f13c298

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/packaging/specifiers.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -644,14 +644,17 @@ def filter(
644644

645645

646646
def _version_split(version: str) -> List[str]:
647-
result: List[str] = []
647+
"""Split version into components.
648648
649-
epoch, sep, rest = version.rpartition("!")
649+
The split components are intended for version comparison. The logic does
650+
not attempt to retain the original version string, so joining the
651+
components back with :func:`_version_join` may not produce the original
652+
version string.
653+
"""
654+
result: List[str] = []
650655

651-
if sep:
652-
result.append(epoch)
653-
else:
654-
result.append("0")
656+
epoch, _, rest = version.rpartition("!")
657+
result.append(epoch or "0")
655658

656659
for item in rest.split("."):
657660
match = _prefix_regex.search(item)
@@ -663,12 +666,14 @@ def _version_split(version: str) -> List[str]:
663666

664667

665668
def _version_join(components: List[str]) -> str:
666-
# This function only works with numeric components.
667-
assert all(c.isdigit() for c in components)
669+
"""Join split version components into a version string.
668670
671+
This function assumes the input came from :func:`_version_split`, where the
672+
first component must be the epoch (either empty or numeric), and all other
673+
components numeric.
674+
"""
669675
epoch, *rest = components
670-
671-
return epoch + "!" + ".".join(rest)
676+
return f"{epoch}!{'.'.join(rest)}"
672677

673678

674679
def _is_not_suffix(segment: str) -> bool:

0 commit comments

Comments
 (0)