# HG changeset patch # User Matt Harbison # Date 2022-12-15 20:41:59 # Node ID 7a4143428db785ab2c5dff9b8d58a257103d532c # Parent c5a06cc37401fac90835274848490283f5924d1b typing: add type hints to the platform specific scm modules Surprisingly, pytype struggled to figure out the return types in the posix functions. diff --git a/mercurial/scmposix.py b/mercurial/scmposix.py --- a/mercurial/scmposix.py +++ b/mercurial/scmposix.py @@ -4,6 +4,11 @@ import fcntl import os import sys +from typing import ( + List, + Tuple, +) + from .pycompat import getattr from . import ( encoding, @@ -11,6 +16,9 @@ from . import ( util, ) +if pycompat.TYPE_CHECKING: + from . import ui as uimod + # BSD 'more' escapes ANSI color sequences by default. This can be disabled by # $MORE variable, but there's no compatible option with Linux 'more'. Given # OS X is widely used and most modern Unix systems would have 'less', setting @@ -18,7 +26,7 @@ from . import ( fallbackpager = b'less' -def _rcfiles(path): +def _rcfiles(path: bytes) -> List[bytes]: rcs = [os.path.join(path, b'hgrc')] rcdir = os.path.join(path, b'hgrc.d') try: @@ -34,7 +42,7 @@ def _rcfiles(path): return rcs -def systemrcpath(): +def systemrcpath() -> List[bytes]: path = [] if pycompat.sysplatform == b'plan9': root = b'lib/mercurial' @@ -49,7 +57,7 @@ def systemrcpath(): return path -def userrcpath(): +def userrcpath() -> List[bytes]: if pycompat.sysplatform == b'plan9': return [encoding.environ[b'home'] + b'/lib/hgrc'] elif pycompat.isdarwin: @@ -65,7 +73,7 @@ def userrcpath(): ] -def termsize(ui): +def termsize(ui: "uimod.ui") -> Tuple[int, int]: try: import termios diff --git a/mercurial/scmwindows.py b/mercurial/scmwindows.py --- a/mercurial/scmwindows.py +++ b/mercurial/scmwindows.py @@ -1,5 +1,10 @@ import os +from typing import ( + List, + Tuple, +) + from . import ( encoding, pycompat, @@ -7,6 +12,9 @@ from . import ( win32, ) +if pycompat.TYPE_CHECKING: + from . import ui as uimod + try: import _winreg as winreg # pytype: disable=import-error @@ -19,7 +27,7 @@ except ImportError: fallbackpager = b'more' -def systemrcpath(): +def systemrcpath() -> List[bytes]: '''return default os-specific hgrc search path''' rcpath = [] filename = win32.executablepath() @@ -27,7 +35,7 @@ def systemrcpath(): progrc = os.path.join(os.path.dirname(filename), b'mercurial.ini') rcpath.append(progrc) - def _processdir(progrcd): + def _processdir(progrcd: bytes) -> None: if os.path.isdir(progrcd): for f, kind in sorted(util.listdir(progrcd)): if f.endswith(b'.rc'): @@ -68,7 +76,7 @@ def systemrcpath(): return rcpath -def userrcpath(): +def userrcpath() -> List[bytes]: '''return os-specific hgrc search path to the user dir''' home = _legacy_expanduser(b'~') path = [os.path.join(home, b'mercurial.ini'), os.path.join(home, b'.hgrc')] @@ -79,7 +87,7 @@ def userrcpath(): return path -def _legacy_expanduser(path): +def _legacy_expanduser(path: bytes) -> bytes: """Expand ~ and ~user constructs in the pre 3.8 style""" # Python 3.8+ changed the expansion of '~' from HOME to USERPROFILE. See @@ -111,5 +119,5 @@ def _legacy_expanduser(path): return userhome + path[i:] -def termsize(ui): +def termsize(ui: "uimod.ui") -> Tuple[int, int]: return win32.termsize()