changeset 49913:3fd5824f1177

typing: attempt to remove @overloads in the platform module for stdlib methods This is mostly successful, as examining util.pyi, posix.pyi, and windows.pyi after a pytype run shows that the type overloads for `oslink`, `readlink`, `removedirs`, `rename`, `split`, and `unlink` have been removed. (Some of these still have an @overload, but the differences are the variable names, not the types.) However, @overloads remain for `abspath` and `normpath` for some reason. It's useful to redefine these methods for the type checking phase because in addition to excluding str and PathLike variants, some of these functions have optional args in stdlib that aren't implemented in the custom implementation on Windows, and we want the type checking to flag that instead of assuming it's an allowable overload everywhere. One last quirk I noticed that I can't explain- `pycompat.TYPE_CHECKING` is always False, so the conditionals need to check `typing.TYPE_CHECKING` directly. I tried dropping the custom code for assigning `pycompat.TYPE_CHECKING` and simply did `from typing import TYPE_CHECKING` directly in pycompat.py, and used `pycompat.TYPE_CHECKING` for the conditional here... and pytype complained that `pycompat` doesn't have the `TYPE_CHECKING` variable.
author Matt Harbison <matt_harbison@yahoo.com>
date Fri, 16 Dec 2022 22:24:05 -0500
parents 2b1476714849
children b1e4c74beb6f
files mercurial/posix.py mercurial/windows.py
diffstat 2 files changed, 48 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/posix.py	Fri Dec 16 22:07:02 2022 -0500
+++ b/mercurial/posix.py	Fri Dec 16 22:24:05 2022 -0500
@@ -17,10 +17,12 @@
 import stat
 import sys
 import tempfile
+import typing
 import unicodedata
 
 from typing import (
     Any,
+    AnyStr,
     Iterable,
     Iterator,
     List,
@@ -67,6 +69,38 @@
 unlink = os.unlink
 rename = os.rename
 removedirs = os.removedirs
+
+if typing.TYPE_CHECKING:
+    # Replace the various overloads that come along with aliasing stdlib methods
+    # with the narrow definition that we care about in the type checking phase
+    # only.  This ensures that both Windows and POSIX see only the definition
+    # that is actually available.
+    #
+    # Note that if we check pycompat.TYPE_CHECKING here, it is always False, and
+    # the methods aren't replaced.
+
+    def normpath(path: bytes) -> bytes:
+        raise NotImplementedError
+
+    def abspath(path: AnyStr) -> AnyStr:
+        raise NotImplementedError
+
+    def oslink(src: bytes, dst: bytes) -> None:
+        raise NotImplementedError
+
+    def readlink(path: bytes) -> bytes:
+        raise NotImplementedError
+
+    def unlink(path: bytes) -> None:
+        raise NotImplementedError
+
+    def rename(src: bytes, dst: bytes) -> None:
+        raise NotImplementedError
+
+    def removedirs(name: bytes) -> None:
+        raise NotImplementedError
+
+
 expandglobs: bool = False
 
 umask: int = os.umask(0)
--- a/mercurial/windows.py	Fri Dec 16 22:07:02 2022 -0500
+++ b/mercurial/windows.py	Fri Dec 16 22:24:05 2022 -0500
@@ -14,6 +14,7 @@
 import stat
 import string
 import sys
+import typing
 import winreg  # pytype: disable=import-error
 
 from typing import (
@@ -27,6 +28,7 @@
     Optional,
     Pattern,
     Sequence,
+    Tuple,
     Union,
 )
 
@@ -59,6 +61,18 @@
 testpid = win32.testpid
 unlink = win32.unlink
 
+if typing.TYPE_CHECKING:
+    # Replace the various overloads that come along with aliasing stdlib methods
+    # with the narrow definition that we care about in the type checking phase
+    # only.  This ensures that both Windows and POSIX see only the definition
+    # that is actually available.
+    #
+    # Note that if we check pycompat.TYPE_CHECKING here, it is always False, and
+    # the methods aren't replaced.
+    def split(p: bytes) -> Tuple[bytes, bytes]:
+        raise NotImplementedError
+
+
 umask: int = 0o022