# HG changeset patch # User Pierre-Yves David # Date 2023-12-20 11:51:20 # Node ID 9d3721552b6cbf613f0089e9dd9fa87e88919a22 # Parent 58d39c7865e57df1efd2369b0fb955aed7162b11 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. diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py --- a/mercurial/branchmap.py +++ b/mercurial/branchmap.py @@ -13,47 +13,50 @@ from .node import ( 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 diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -18,6 +18,7 @@ from typing import ( Dict, Iterable, Optional, + TYPE_CHECKING, cast, ) @@ -71,7 +72,7 @@ from .revlogutils import ( constants as revlog_constants, ) -if pycompat.TYPE_CHECKING: +if TYPE_CHECKING: from . import ( ui as uimod, ) diff --git a/mercurial/encoding.py b/mercurial/encoding.py --- a/mercurial/encoding.py +++ b/mercurial/encoding.py @@ -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 . import ( 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 @@ class localstr(bytes): 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 diff --git a/mercurial/error.py b/mercurial/error.py --- a/mercurial/error.py +++ b/mercurial/error.py @@ -14,19 +14,30 @@ imports. 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): diff --git a/mercurial/i18n.py b/mercurial/i18n.py --- a/mercurial/i18n.py +++ b/mercurial/i18n.py @@ -11,18 +11,22 @@ import locale 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: diff --git a/mercurial/logcmdutil.py b/mercurial/logcmdutil.py --- a/mercurial/logcmdutil.py +++ b/mercurial/logcmdutil.py @@ -10,6 +10,15 @@ import itertools import os import posixpath +from typing import ( + Any, + Callable, + Dict, + Optional, + Sequence, + Tuple, +) + from .i18n import _ from .node import wdirrev @@ -39,19 +48,19 @@ from .utils import ( 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): diff --git a/mercurial/mail.py b/mercurial/mail.py --- a/mercurial/mail.py +++ b/mercurial/mail.py @@ -18,6 +18,13 @@ import smtplib import socket import time +from typing import ( + Any, + List, + Tuple, + Union, +) + from .i18n import _ from .pycompat import ( open, @@ -35,11 +42,14 @@ from .utils import ( 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): diff --git a/mercurial/pathutil.py b/mercurial/pathutil.py --- a/mercurial/pathutil.py +++ b/mercurial/pathutil.py @@ -23,6 +23,14 @@ from . import ( rustdirs = policy.importrust('dirstate', 'Dirs') parsers = policy.importmod('parsers') +# keeps pyflakes happy +assert [ + Any, + Callable, + Iterator, + Optional, +] + def _lowerclean(s): # type: (bytes) -> bytes diff --git a/mercurial/phases.py b/mercurial/phases.py --- a/mercurial/phases.py +++ b/mercurial/phases.py @@ -102,6 +102,18 @@ Note: old client behave as a publishing 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 @@ from . import ( 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] ] diff --git a/mercurial/posix.py b/mercurial/posix.py --- a/mercurial/posix.py +++ b/mercurial/posix.py @@ -70,13 +70,6 @@ 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 diff --git a/mercurial/scmposix.py b/mercurial/scmposix.py --- a/mercurial/scmposix.py +++ b/mercurial/scmposix.py @@ -3,6 +3,7 @@ import errno import fcntl import os import sys +import typing from typing import ( List, @@ -15,7 +16,7 @@ from . import ( 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 diff --git a/mercurial/scmwindows.py b/mercurial/scmwindows.py --- a/mercurial/scmwindows.py +++ b/mercurial/scmwindows.py @@ -3,6 +3,7 @@ import winreg # pytype: disable=import- from typing import ( List, + TYPE_CHECKING, Tuple, ) @@ -13,7 +14,7 @@ from . import ( 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. diff --git a/mercurial/state.py b/mercurial/state.py --- a/mercurial/state.py +++ b/mercurial/state.py @@ -20,23 +20,22 @@ the data. 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: diff --git a/mercurial/subrepoutil.py b/mercurial/subrepoutil.py --- a/mercurial/subrepoutil.py +++ b/mercurial/subrepoutil.py @@ -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 @@ from . import ( filemerge, pathutil, phases, - pycompat, util, ) from .utils import ( @@ -25,17 +34,19 @@ from .utils import ( 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 @@ if pycompat.TYPE_CHECKING: 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): diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -18,6 +18,7 @@ import socket import subprocess import sys import traceback +import typing from typing import ( Any, @@ -1766,7 +1767,7 @@ class ui: return line - if pycompat.TYPE_CHECKING: + if typing.TYPE_CHECKING: @overload def prompt(self, msg: bytes, default: bytes) -> bytes: @@ -1782,7 +1783,7 @@ class ui: """ return self._prompt(msg, default=default) - if pycompat.TYPE_CHECKING: + if typing.TYPE_CHECKING: @overload def _prompt( diff --git a/mercurial/upgrade_utils/actions.py b/mercurial/upgrade_utils/actions.py --- a/mercurial/upgrade_utils/actions.py +++ b/mercurial/upgrade_utils/actions.py @@ -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 .. import ( 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 = { diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -34,6 +34,14 @@ import time 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 @@ from .utils import ( 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') diff --git a/mercurial/utils/dateutil.py b/mercurial/utils/dateutil.py --- a/mercurial/utils/dateutil.py +++ b/mercurial/utils/dateutil.py @@ -10,6 +10,15 @@ import calendar import datetime import time +from typing import ( + Callable, + Dict, + Iterable, + Optional, + Tuple, + Union, +) + from ..i18n import _ from .. import ( encoding, @@ -17,17 +26,17 @@ from .. import ( 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 = ( diff --git a/mercurial/utils/urlutil.py b/mercurial/utils/urlutil.py --- a/mercurial/utils/urlutil.py +++ b/mercurial/utils/urlutil.py @@ -8,6 +8,10 @@ import os import re as remod import socket +from typing import ( + Union, +) + from ..i18n import _ from .. import ( encoding, @@ -24,11 +28,8 @@ from ..revlogutils import ( constants as revlog_constants, ) - -if pycompat.TYPE_CHECKING: - from typing import ( - Union, - ) +# keeps pyflakes happy +assert [Union] urlreq = urllibcompat.urlreq diff --git a/mercurial/windows.py b/mercurial/windows.py --- a/mercurial/windows.py +++ b/mercurial/windows.py @@ -61,13 +61,7 @@ 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