# HG changeset patch # User Matt Harbison # Date 1671247445 18000 # Node ID 3fd5824f1177e2d8ebb77cc2dde628c83a47e94c # Parent 2b14767148492175fce62f80eb11407f1ace7444 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. diff -r 2b1476714849 -r 3fd5824f1177 mercurial/posix.py --- 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) diff -r 2b1476714849 -r 3fd5824f1177 mercurial/windows.py --- 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