Skip to content

Commit e133018

Browse files
committed
Broaden cygpath parameter annotation
To fix a mypy error in Repo.__init__ where the epath variable, which can sometimes be a os.PathLike[str], does not match the str annotation for cygpath's path parameter, even though cygwin immediately calls str on that parameter. Pulling most of cygpath out into a new helper function is to help mypy correctly infer that the type of path is still compatible with the return type of str, when it is used in the return statement. I'm not sure this change is really the best solution at this time, because while it fixes the mypy error (without creating a new one), and cygpath is not listed in __all__, the docstring of advises to call Git.polish_url instead of cygpath. That method is public, annotates its parameter as str, and should not have its annotation broadened without considernig if it makes sense to do so or if its docstring needs to be updated. So either the cygpath docstring should be updated or this change should be undone and a different approach used to fix the type error in Repo.__init__.
1 parent f1cc1fe commit e133018

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

git/util.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,7 @@ def _cygexpath(drive: Optional[str], path: str) -> str:
404404
)
405405

406406

407-
def cygpath(path: str) -> str:
408-
"""Use :meth:`git.cmd.Git.polish_url` instead, that works on any environment."""
409-
path = str(path) # Ensure is str and not AnyPath.
407+
def _cygpath(path: str) -> str:
410408
# Fix to use Paths when 3.5 dropped. Or to be just str if only for URLs?
411409
if not path.startswith(("/cygdrive", "//", "/proc/cygdrive")):
412410
for regex, parser, recurse in _cygpath_parsers:
@@ -422,6 +420,11 @@ def cygpath(path: str) -> str:
422420
return path
423421

424422

423+
def cygpath(path: PathLike) -> str:
424+
"""Use :meth:`git.cmd.Git.polish_url` instead, that works on any environment."""
425+
return _cygpath(str(path)) # Ensure is str and not AnyPath.
426+
427+
425428
_decygpath_regex = re.compile(r"(?:/proc)?/cygdrive/(\w)(/.*)?")
426429

427430

0 commit comments

Comments
 (0)