changeset 49903:7a4143428db7

typing: add type hints to the platform specific scm modules Surprisingly, pytype struggled to figure out the return types in the posix functions.
author Matt Harbison <matt_harbison@yahoo.com>
date Thu, 15 Dec 2022 15:41:59 -0500
parents c5a06cc37401
children 7a80a614c9e5
files mercurial/scmposix.py mercurial/scmwindows.py
diffstat 2 files changed, 25 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/scmposix.py	Thu Dec 15 01:05:27 2022 -0500
+++ b/mercurial/scmposix.py	Thu Dec 15 15:41:59 2022 -0500
@@ -4,6 +4,11 @@
 import os
 import sys
 
+from typing import (
+    List,
+    Tuple,
+)
+
 from .pycompat import getattr
 from . import (
     encoding,
@@ -11,6 +16,9 @@
     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 @@
 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 @@
     return rcs
 
 
-def systemrcpath():
+def systemrcpath() -> List[bytes]:
     path = []
     if pycompat.sysplatform == b'plan9':
         root = b'lib/mercurial'
@@ -49,7 +57,7 @@
     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 termsize(ui):
+def termsize(ui: "uimod.ui") -> Tuple[int, int]:
     try:
         import termios
 
--- a/mercurial/scmwindows.py	Thu Dec 15 01:05:27 2022 -0500
+++ b/mercurial/scmwindows.py	Thu Dec 15 15:41:59 2022 -0500
@@ -1,5 +1,10 @@
 import os
 
+from typing import (
+    List,
+    Tuple,
+)
+
 from . import (
     encoding,
     pycompat,
@@ -7,6 +12,9 @@
     win32,
 )
 
+if pycompat.TYPE_CHECKING:
+    from . import ui as uimod
+
 try:
     import _winreg as winreg  # pytype: disable=import-error
 
@@ -19,7 +27,7 @@
 fallbackpager = b'more'
 
 
-def systemrcpath():
+def systemrcpath() -> List[bytes]:
     '''return default os-specific hgrc search path'''
     rcpath = []
     filename = win32.executablepath()
@@ -27,7 +35,7 @@
     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 @@
     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 @@
     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 @@
     return userhome + path[i:]
 
 
-def termsize(ui):
+def termsize(ui: "uimod.ui") -> Tuple[int, int]:
     return win32.termsize()