Skip to content

Commit 86a3246

Browse files
Unify path helpers in _compat module
- Move PathT type alias from _types.py to _compat.py - Move norm_real() function from _file_finders/_pathtools.py to _compat.py - Update all file finder imports to use _compat directly - Delete _file_finders/_pathtools.py (no longer needed) - Update _types.py to re-export PathT for backward compatibility This consolidates all path-related compatibility utilities in a single location, eliminating unnecessary module indirection and making the codebase more maintainable.
1 parent 0e89304 commit 86a3246

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

vcs-versioning/src/vcs_versioning/_compat.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
from __future__ import annotations
44

5+
import os
6+
from typing import TypeAlias
7+
8+
# Path type for accepting both strings and PathLike objects
9+
PathT: TypeAlias = os.PathLike[str] | str
10+
511

612
def normalize_path_for_assertion(path: str) -> str:
713
"""Normalize path separators for cross-platform assertions.
@@ -63,3 +69,23 @@ def assert_path_endswith(
6369
def compute_path_prefix(full_path: str, suffix_path: str) -> str:
6470
"""Legacy alias - use strip_path_suffix instead."""
6571
return strip_path_suffix(full_path, suffix_path)
72+
73+
74+
def norm_real(path: PathT) -> str:
75+
"""Normalize and resolve a path (combining normcase and realpath).
76+
77+
This combines os.path.normcase() and os.path.realpath() to produce
78+
a canonical path string that is normalized for the platform and has
79+
all symbolic links resolved.
80+
81+
Args:
82+
path: The path to normalize and resolve
83+
84+
Returns:
85+
The normalized, resolved absolute path
86+
87+
Examples:
88+
>>> norm_real("/path/to/../to/file.txt") # doctest: +SKIP
89+
'/path/to/file.txt'
90+
"""
91+
return os.path.normcase(os.path.realpath(path))

vcs-versioning/src/vcs_versioning/_file_finders/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from typing import TypeGuard
77

88
from .. import _types as _t
9+
from .._compat import norm_real
910
from .._entrypoints import entry_points
10-
from ._pathtools import norm_real
1111

1212
log = logging.getLogger("vcs_versioning.file_finder")
1313

vcs-versioning/src/vcs_versioning/_file_finders/_git.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
from typing import IO
88

99
from .. import _types as _t
10-
from .._compat import strip_path_suffix
10+
from .._compat import norm_real, strip_path_suffix
1111
from .._integration import data_from_mime
1212
from .._run_cmd import run as _run
1313
from . import is_toplevel_acceptable, scm_find_files
14-
from ._pathtools import norm_real
1514

1615
log = logging.getLogger(__name__)
1716

vcs-versioning/src/vcs_versioning/_file_finders/_hg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
from .. import _types as _t
88
from .._backends._hg import run_hg
9+
from .._compat import norm_real
910
from .._integration import data_from_mime
1011
from . import is_toplevel_acceptable, scm_find_files
11-
from ._pathtools import norm_real
1212

1313
log = logging.getLogger(__name__)
1414

vcs-versioning/src/vcs_versioning/_file_finders/_pathtools.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

vcs-versioning/src/vcs_versioning/_types.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
from __future__ import annotations
22

3-
import os
43
from collections.abc import Callable, Sequence
54
from typing import TYPE_CHECKING, TypeAlias
65

76
if TYPE_CHECKING:
87
from . import _version_schemes as version
98

10-
PathT: TypeAlias = os.PathLike[str] | str
9+
# Re-export from _compat for backward compatibility
10+
from ._compat import PathT as PathT # noqa: PLC0414
11+
12+
__all__ = [
13+
"PathT",
14+
"CMD_TYPE",
15+
"VERSION_SCHEME",
16+
"VERSION_SCHEMES",
17+
"SCMVERSION",
18+
"GIT_PRE_PARSE",
19+
]
1120

1221
CMD_TYPE: TypeAlias = Sequence[PathT] | str
1322

0 commit comments

Comments
 (0)