changeset 51285:9d3721552b6c

pytype: import typing directly First we no longer needs the pycompat layer, second having the types imported in all case will allow to use them more directly in type annotation, something important to upgrade the old "type comment" to proper type annotation. A lot a stupid assert are needed to keep pyflakes happy. We should be able to remove most of them once the type comment have been upgraded.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Wed, 20 Dec 2023 12:51:20 +0100
parents 58d39c7865e5
children 81224afd938d
files mercurial/branchmap.py mercurial/cmdutil.py mercurial/encoding.py mercurial/error.py mercurial/i18n.py mercurial/logcmdutil.py mercurial/mail.py mercurial/pathutil.py mercurial/phases.py mercurial/posix.py mercurial/scmposix.py mercurial/scmwindows.py mercurial/state.py mercurial/subrepoutil.py mercurial/ui.py mercurial/upgrade_utils/actions.py mercurial/util.py mercurial/utils/dateutil.py mercurial/utils/urlutil.py mercurial/windows.py
diffstat 20 files changed, 251 insertions(+), 156 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/branchmap.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/branchmap.py	Wed Dec 20 12:51:20 2023 +0100
@@ -13,47 +13,50 @@
     hex,
     nullrev,
 )
+
+from typing import (
+    Any,
+    Callable,
+    Dict,
+    Iterable,
+    List,
+    Optional,
+    Set,
+    TYPE_CHECKING,
+    Tuple,
+    Union,
+)
+
 from . import (
     encoding,
     error,
     obsolete,
-    pycompat,
     scmutil,
     util,
 )
+
 from .utils import (
     repoviewutil,
     stringutil,
 )
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Any,
-        Callable,
-        Dict,
-        Iterable,
-        List,
-        Optional,
-        Set,
-        Tuple,
-        Union,
-    )
+# keeps pyflakes happy
+assert [
+    Any,
+    Callable,
+    Dict,
+    Iterable,
+    List,
+    Optional,
+    Set,
+    Tuple,
+    Union,
+]
+
+if TYPE_CHECKING:
     from . import localrepo
 
-    assert any(
-        (
-            Any,
-            Callable,
-            Dict,
-            Iterable,
-            List,
-            Optional,
-            Set,
-            Tuple,
-            Union,
-            localrepo,
-        )
-    )
+    assert [localrepo]
 
 subsettable = repoviewutil.subsettable
 
--- a/mercurial/cmdutil.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/cmdutil.py	Wed Dec 20 12:51:20 2023 +0100
@@ -18,6 +18,7 @@
     Dict,
     Iterable,
     Optional,
+    TYPE_CHECKING,
     cast,
 )
 
@@ -71,7 +72,7 @@
     constants as revlog_constants,
 )
 
-if pycompat.TYPE_CHECKING:
+if TYPE_CHECKING:
     from . import (
         ui as uimod,
     )
--- a/mercurial/encoding.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/encoding.py	Wed Dec 20 12:51:20 2023 +0100
@@ -9,8 +9,19 @@
 import locale
 import os
 import re
+import typing
 import unicodedata
 
+from typing import (
+    Any,
+    Callable,
+    List,
+    Text,
+    Type,
+    TypeVar,
+    Union,
+)
+
 from . import (
     error,
     policy,
@@ -19,22 +30,11 @@
 
 from .pure import charencode as charencodepure
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Any,
-        Callable,
-        List,
-        Text,
-        Type,
-        TypeVar,
-        Union,
-    )
+# keep pyflakes happy
+for t in (Any, Callable, List, Text, Type, Union):
+    assert t
 
-    # keep pyflakes happy
-    for t in (Any, Callable, List, Text, Type, Union):
-        assert t
-
-    _Tlocalstr = TypeVar('_Tlocalstr', bound='localstr')
+_Tlocalstr = TypeVar('_Tlocalstr', bound='localstr')
 
 charencode = policy.importmod('charencode')
 
@@ -131,7 +131,7 @@
         s._utf8 = u
         return s
 
-    if pycompat.TYPE_CHECKING:
+    if typing.TYPE_CHECKING:
         # pseudo implementation to help pytype see localstr() constructor
         def __init__(self, u, l):
             # type: (bytes, bytes) -> None
--- a/mercurial/error.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/error.py	Wed Dec 20 12:51:20 2023 +0100
@@ -14,19 +14,30 @@
 
 import difflib
 
+from typing import (
+    Any,
+    AnyStr,
+    Iterable,
+    List,
+    Optional,
+    Sequence,
+    Union,
+)
+
 # Do not import anything but pycompat here, please
 from . import pycompat
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Any,
-        AnyStr,
-        Iterable,
-        List,
-        Optional,
-        Sequence,
-        Union,
-    )
+
+# keeps pyflakes happy
+assert [
+    Any,
+    AnyStr,
+    Iterable,
+    List,
+    Optional,
+    Sequence,
+    Union,
+]
 
 
 def _tobytes(exc):
--- a/mercurial/i18n.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/i18n.py	Wed Dec 20 12:51:20 2023 +0100
@@ -11,18 +11,22 @@
 import os
 import sys
 
+from typing import (
+    Callable,
+    List,
+)
+
 from .utils import resourceutil
 from . import (
     encoding,
     pycompat,
 )
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Callable,
-        List,
-    )
-
+# keeps pyflakes happy
+assert [
+    Callable,
+    List,
+]
 
 # modelled after templater.templatepath:
 if getattr(sys, 'frozen', None) is not None:
--- a/mercurial/logcmdutil.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/logcmdutil.py	Wed Dec 20 12:51:20 2023 +0100
@@ -10,6 +10,15 @@
 import os
 import posixpath
 
+from typing import (
+    Any,
+    Callable,
+    Dict,
+    Optional,
+    Sequence,
+    Tuple,
+)
+
 from .i18n import _
 from .node import wdirrev
 
@@ -39,19 +48,19 @@
     stringutil,
 )
 
+# keeps pyflakes happy
+assert [
+    Any,
+    Callable,
+    Dict,
+    Optional,
+    Sequence,
+    Tuple,
+]
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Any,
-        Callable,
-        Dict,
-        Optional,
-        Sequence,
-        Tuple,
-    )
-
-    for t in (Any, Callable, Dict, Optional, Tuple):
-        assert t
+# keep pyflakes happy
+for t in (Any, Callable, Dict, Optional, Tuple):
+    assert t
 
 
 def getlimit(opts):
--- a/mercurial/mail.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/mail.py	Wed Dec 20 12:51:20 2023 +0100
@@ -18,6 +18,13 @@
 import socket
 import time
 
+from typing import (
+    Any,
+    List,
+    Tuple,
+    Union,
+)
+
 from .i18n import _
 from .pycompat import (
     open,
@@ -35,11 +42,14 @@
     urlutil,
 )
 
-if pycompat.TYPE_CHECKING:
-    from typing import Any, List, Tuple, Union
 
-    # keep pyflakes happy
-    assert all((Any, List, Tuple, Union))
+# keep pyflakes happy
+assert [
+    Any,
+    List,
+    Tuple,
+    Union,
+]
 
 
 class STARTTLS(smtplib.SMTP):
--- a/mercurial/pathutil.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/pathutil.py	Wed Dec 20 12:51:20 2023 +0100
@@ -23,6 +23,14 @@
 rustdirs = policy.importrust('dirstate', 'Dirs')
 parsers = policy.importmod('parsers')
 
+# keeps pyflakes happy
+assert [
+    Any,
+    Callable,
+    Iterator,
+    Optional,
+]
+
 
 def _lowerclean(s):
     # type: (bytes) -> bytes
--- a/mercurial/phases.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/phases.py	Wed Dec 20 12:51:20 2023 +0100
@@ -102,6 +102,18 @@
 
 
 import struct
+import typing
+
+from typing import (
+    Any,
+    Callable,
+    Dict,
+    Iterable,
+    List,
+    Optional,
+    Set,
+    Tuple,
+)
 
 from .i18n import _
 from .node import (
@@ -120,23 +132,29 @@
     util,
 )
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Any,
-        Callable,
-        Dict,
-        Iterable,
-        List,
-        Optional,
-        Set,
-        Tuple,
-    )
+# keeps pyflakes happy
+assert [
+    Any,
+    Callable,
+    Dict,
+    Iterable,
+    List,
+    Optional,
+    Set,
+    Tuple,
+]
+
+Phaseroots = Dict[int, Set[bytes]]
+
+if typing.TYPE_CHECKING:
     from . import (
         localrepo,
         ui as uimod,
     )
 
-    Phaseroots = Dict[int, Set[bytes]]
+    # keeps pyflakes happy
+    assert [uimod]
+
     Phasedefaults = List[
         Callable[[localrepo.localrepository, Phaseroots], Phaseroots]
     ]
--- a/mercurial/posix.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/posix.py	Wed Dec 20 12:51:20 2023 +0100
@@ -70,13 +70,6 @@
 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
--- a/mercurial/scmposix.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/scmposix.py	Wed Dec 20 12:51:20 2023 +0100
@@ -3,6 +3,7 @@
 import fcntl
 import os
 import sys
+import typing
 
 from typing import (
     List,
@@ -15,7 +16,7 @@
     util,
 )
 
-if pycompat.TYPE_CHECKING:
+if typing.TYPE_CHECKING:
     from . import ui as uimod
 
 # BSD 'more' escapes ANSI color sequences by default. This can be disabled by
--- a/mercurial/scmwindows.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/scmwindows.py	Wed Dec 20 12:51:20 2023 +0100
@@ -3,6 +3,7 @@
 
 from typing import (
     List,
+    TYPE_CHECKING,
     Tuple,
 )
 
@@ -13,7 +14,7 @@
     win32,
 )
 
-if pycompat.TYPE_CHECKING:
+if TYPE_CHECKING:
     from . import ui as uimod
 
 # MS-DOS 'more' is the only pager available by default on Windows.
--- a/mercurial/state.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/state.py	Wed Dec 20 12:51:20 2023 +0100
@@ -20,23 +20,22 @@
 
 import contextlib
 
+from typing import (
+    Any,
+    Dict,
+)
+
 from .i18n import _
 
 from . import (
     error,
-    pycompat,
     util,
 )
 from .utils import cborutil
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Any,
-        Dict,
-    )
-
-    for t in (Any, Dict):
-        assert t
+# keeps pyflakes happy
+for t in (Any, Dict):
+    assert t
 
 
 class cmdstate:
--- a/mercurial/subrepoutil.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/subrepoutil.py	Wed Dec 20 12:51:20 2023 +0100
@@ -9,6 +9,16 @@
 import os
 import posixpath
 import re
+import typing
+
+from typing import (
+    Any,
+    Dict,
+    List,
+    Optional,
+    Set,
+    Tuple,
+)
 
 from .i18n import _
 from . import (
@@ -17,7 +27,6 @@
     filemerge,
     pathutil,
     phases,
-    pycompat,
     util,
 )
 from .utils import (
@@ -25,17 +34,19 @@
     urlutil,
 )
 
+# keeps pyflakes happy
+assert [
+    Any,
+    Dict,
+    List,
+    Optional,
+    Set,
+    Tuple,
+]
+
 nullstate = (b'', b'', b'empty')
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Any,
-        Dict,
-        List,
-        Optional,
-        Set,
-        Tuple,
-    )
+if typing.TYPE_CHECKING:
     from . import (
         context,
         localrepo,
@@ -45,7 +56,17 @@
         ui as uimod,
     )
 
-    Substate = Dict[bytes, Tuple[bytes, bytes, bytes]]
+    # keeps pyflakes happy
+    assert [
+        context,
+        localrepo,
+        matchmod,
+        scmutil,
+        subrepo,
+        uimod,
+    ]
+
+Substate = Dict[bytes, Tuple[bytes, bytes, bytes]]
 
 
 def state(ctx, ui):
--- a/mercurial/ui.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/ui.py	Wed Dec 20 12:51:20 2023 +0100
@@ -18,6 +18,7 @@
 import subprocess
 import sys
 import traceback
+import typing
 
 from typing import (
     Any,
@@ -1766,7 +1767,7 @@
 
         return line
 
-    if pycompat.TYPE_CHECKING:
+    if typing.TYPE_CHECKING:
 
         @overload
         def prompt(self, msg: bytes, default: bytes) -> bytes:
@@ -1782,7 +1783,7 @@
         """
         return self._prompt(msg, default=default)
 
-    if pycompat.TYPE_CHECKING:
+    if typing.TYPE_CHECKING:
 
         @overload
         def _prompt(
--- a/mercurial/upgrade_utils/actions.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/upgrade_utils/actions.py	Wed Dec 20 12:51:20 2023 +0100
@@ -7,11 +7,15 @@
 
 import random
 
+from typing import (
+    List,
+    Type,
+)
+
 from ..i18n import _
 from .. import (
     error,
     localrepo,
-    pycompat,
     requirements,
     revlog,
     util,
@@ -19,12 +23,11 @@
 
 from ..utils import compression
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        List,
-        Type,
-    )
-
+# keeps pyflakes happy
+assert [
+    List,
+    Type,
+]
 
 # list of requirements that request a clone of all revlog if added/removed
 RECLONES_REQUIREMENTS = {
--- a/mercurial/util.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/util.py	Wed Dec 20 12:51:20 2023 +0100
@@ -34,6 +34,14 @@
 import traceback
 import warnings
 
+from typing import (
+    Iterable,
+    Iterator,
+    List,
+    Optional,
+    Tuple,
+)
+
 from .node import hex
 from .thirdparty import attr
 from .pycompat import (
@@ -55,14 +63,14 @@
     stringutil,
 )
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Iterable,
-        Iterator,
-        List,
-        Optional,
-        Tuple,
-    )
+# keeps pyflakes happy
+assert [
+    Iterable,
+    Iterator,
+    List,
+    Optional,
+    Tuple,
+]
 
 
 base85 = policy.importmod('base85')
--- a/mercurial/utils/dateutil.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/utils/dateutil.py	Wed Dec 20 12:51:20 2023 +0100
@@ -10,6 +10,15 @@
 import datetime
 import time
 
+from typing import (
+    Callable,
+    Dict,
+    Iterable,
+    Optional,
+    Tuple,
+    Union,
+)
+
 from ..i18n import _
 from .. import (
     encoding,
@@ -17,17 +26,17 @@
     pycompat,
 )
 
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Callable,
-        Dict,
-        Iterable,
-        Optional,
-        Tuple,
-        Union,
-    )
+# keeps pyflakes happy
+assert [
+    Callable,
+    Dict,
+    Iterable,
+    Optional,
+    Tuple,
+    Union,
+]
 
-    hgdate = Tuple[float, int]  # (unixtime, offset)
+hgdate = Tuple[float, int]  # (unixtime, offset)
 
 # used by parsedate
 defaultdateformats = (
--- a/mercurial/utils/urlutil.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/utils/urlutil.py	Wed Dec 20 12:51:20 2023 +0100
@@ -8,6 +8,10 @@
 import re as remod
 import socket
 
+from typing import (
+    Union,
+)
+
 from ..i18n import _
 from .. import (
     encoding,
@@ -24,11 +28,8 @@
     constants as revlog_constants,
 )
 
-
-if pycompat.TYPE_CHECKING:
-    from typing import (
-        Union,
-    )
+# keeps pyflakes happy
+assert [Union]
 
 urlreq = urllibcompat.urlreq
 
--- a/mercurial/windows.py	Wed Nov 08 01:58:16 2023 +0100
+++ b/mercurial/windows.py	Wed Dec 20 12:51:20 2023 +0100
@@ -61,13 +61,7 @@
 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