diff --git a/contrib/check-pytype.sh b/contrib/check-pytype.sh --- a/contrib/check-pytype.sh +++ b/contrib/check-pytype.sh @@ -31,7 +31,6 @@ cd `hg root` # mercurial/pure/parsers.py # [attribute-error] # mercurial/repoview.py # [attribute-error] # mercurial/testing/storage.py # tons of [attribute-error] -# mercurial/ui.py # [attribute-error], [wrong-arg-types] # mercurial/unionrepo.py # ui, svfs, unfiltered [attribute-error] # mercurial/win32.py # [not-callable] # mercurial/wireprotoframing.py # [unsupported-operands], [attribute-error], [import-error] @@ -64,7 +63,6 @@ pytype -V 3.7 --keep-going --jobs auto m -x mercurial/repoview.py \ -x mercurial/testing/storage.py \ -x mercurial/thirdparty \ - -x mercurial/ui.py \ -x mercurial/unionrepo.py \ -x mercurial/win32.py \ -x mercurial/wireprotoframing.py \ diff --git a/mercurial/typelib.py b/mercurial/typelib.py new file mode 100644 --- /dev/null +++ b/mercurial/typelib.py @@ -0,0 +1,28 @@ +# typelib.py - type hint aliases and support +# +# Copyright 2022 Matt Harbison +# +# This software may be used and distributed according to the terms of the +# GNU General Public License version 2 or any later version. + +import typing + +# Note: this is slightly different from pycompat.TYPE_CHECKING, as using +# pycompat causes the BinaryIO_Proxy type to be resolved to ``object`` when +# used as the base class during a pytype run. +TYPE_CHECKING = typing.TYPE_CHECKING + + +# The BinaryIO class provides empty methods, which at runtime means that +# ``__getattr__`` on the proxy classes won't get called for the methods that +# should delegate to the internal object. So to avoid runtime changes because +# of the required typing inheritance, just use BinaryIO when typechecking, and +# ``object`` otherwise. +if TYPE_CHECKING: + from typing import ( + BinaryIO, + ) + + BinaryIO_Proxy = BinaryIO +else: + BinaryIO_Proxy = object diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -1795,6 +1795,9 @@ class ui: # choices containing spaces, ASCII, or basically anything # except an ampersand followed by a character. m = re.match(br'(?s)(.+?)\$\$([^$]*&[^ $].*)', prompt) + + assert m is not None # help pytype + msg = m.group(1) choices = [p.strip(b' ') for p in m.group(2).split(b'$$')] diff --git a/mercurial/utils/procutil.py b/mercurial/utils/procutil.py --- a/mercurial/utils/procutil.py +++ b/mercurial/utils/procutil.py @@ -18,6 +18,10 @@ import sys import threading import time +from typing import ( + BinaryIO, +) + from ..i18n import _ from ..pycompat import ( getattr, @@ -29,6 +33,7 @@ from .. import ( error, policy, pycompat, + typelib, ) # Import like this to keep import-checker happy @@ -118,8 +123,8 @@ def unwrap_line_buffered(stream): return stream -class WriteAllWrapper: - def __init__(self, orig): +class WriteAllWrapper(typelib.BinaryIO_Proxy): + def __init__(self, orig: BinaryIO): self.orig = orig def __getattr__(self, attr): diff --git a/mercurial/windows.py b/mercurial/windows.py --- a/mercurial/windows.py +++ b/mercurial/windows.py @@ -16,6 +16,10 @@ import string import sys import winreg # pytype: disable=import-error +from typing import ( + BinaryIO, +) + from .i18n import _ from .pycompat import getattr from . import ( @@ -23,6 +27,7 @@ from . import ( error, policy, pycompat, + typelib, win32, ) @@ -208,7 +213,7 @@ def get_password(): return encoding.unitolocal(pw) -class winstdout: +class winstdout(typelib.BinaryIO_Proxy): """Some files on Windows misbehave. When writing to a broken pipe, EINVAL instead of EPIPE may be raised. @@ -217,7 +222,7 @@ class winstdout: error may happen. Python 3 already works around that. """ - def __init__(self, fp): + def __init__(self, fp: BinaryIO): self.fp = fp def __getattr__(self, key):