comparison mercurial/posix.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 ae93ada06454
children 07792fd1837f
comparison
equal deleted inserted replaced
49817:2b1476714849 49818:3fd5824f1177
15 import re 15 import re
16 import select 16 import select
17 import stat 17 import stat
18 import sys 18 import sys
19 import tempfile 19 import tempfile
20 import typing
20 import unicodedata 21 import unicodedata
21 22
22 from typing import ( 23 from typing import (
23 Any, 24 Any,
25 AnyStr,
24 Iterable, 26 Iterable,
25 Iterator, 27 Iterator,
26 List, 28 List,
27 Match, 29 Match,
28 NoReturn, 30 NoReturn,
65 67
66 readlink = os.readlink 68 readlink = os.readlink
67 unlink = os.unlink 69 unlink = os.unlink
68 rename = os.rename 70 rename = os.rename
69 removedirs = os.removedirs 71 removedirs = os.removedirs
72
73 if typing.TYPE_CHECKING:
74 # Replace the various overloads that come along with aliasing stdlib methods
75 # with the narrow definition that we care about in the type checking phase
76 # only. This ensures that both Windows and POSIX see only the definition
77 # that is actually available.
78 #
79 # Note that if we check pycompat.TYPE_CHECKING here, it is always False, and
80 # the methods aren't replaced.
81
82 def normpath(path: bytes) -> bytes:
83 raise NotImplementedError
84
85 def abspath(path: AnyStr) -> AnyStr:
86 raise NotImplementedError
87
88 def oslink(src: bytes, dst: bytes) -> None:
89 raise NotImplementedError
90
91 def readlink(path: bytes) -> bytes:
92 raise NotImplementedError
93
94 def unlink(path: bytes) -> None:
95 raise NotImplementedError
96
97 def rename(src: bytes, dst: bytes) -> None:
98 raise NotImplementedError
99
100 def removedirs(name: bytes) -> None:
101 raise NotImplementedError
102
103
70 expandglobs: bool = False 104 expandglobs: bool = False
71 105
72 umask: int = os.umask(0) 106 umask: int = os.umask(0)
73 os.umask(umask) 107 os.umask(umask)
74 108