comparison mercurial/windows.py @ 49818: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 18c8c18993f0
comparison
equal deleted inserted replaced
49817:2b1476714849 49818:3fd5824f1177
12 import os 12 import os
13 import re 13 import re
14 import stat 14 import stat
15 import string 15 import string
16 import sys 16 import sys
17 import typing
17 import winreg # pytype: disable=import-error 18 import winreg # pytype: disable=import-error
18 19
19 from typing import ( 20 from typing import (
20 AnyStr, 21 AnyStr,
21 BinaryIO, 22 BinaryIO,
25 Mapping, 26 Mapping,
26 NoReturn, 27 NoReturn,
27 Optional, 28 Optional,
28 Pattern, 29 Pattern,
29 Sequence, 30 Sequence,
31 Tuple,
30 Union, 32 Union,
31 ) 33 )
32 34
33 from .i18n import _ 35 from .i18n import _
34 from .pycompat import getattr 36 from .pycompat import getattr
56 setsignalhandler = win32.setsignalhandler 58 setsignalhandler = win32.setsignalhandler
57 spawndetached = win32.spawndetached 59 spawndetached = win32.spawndetached
58 split = os.path.split 60 split = os.path.split
59 testpid = win32.testpid 61 testpid = win32.testpid
60 unlink = win32.unlink 62 unlink = win32.unlink
63
64 if typing.TYPE_CHECKING:
65 # Replace the various overloads that come along with aliasing stdlib methods
66 # with the narrow definition that we care about in the type checking phase
67 # only. This ensures that both Windows and POSIX see only the definition
68 # that is actually available.
69 #
70 # Note that if we check pycompat.TYPE_CHECKING here, it is always False, and
71 # the methods aren't replaced.
72 def split(p: bytes) -> Tuple[bytes, bytes]:
73 raise NotImplementedError
74
61 75
62 umask: int = 0o022 76 umask: int = 0o022
63 77
64 78
65 class mixedfilemodewrapper: 79 class mixedfilemodewrapper: