changeset 52709:279e217d6041

typing: lock in the new type annotations detected with the pyupgrade changes After the changes culminating in 70a75d379daf, pytype was able to detect these types better (typically changing from something like `Generator[Any, Any, None]` to `Generator[bytes, Any, None]`). Let's make these explicit so they don't disappear because of other changes.
author Matt Harbison <matt_harbison@yahoo.com>
date Mon, 06 Jan 2025 20:02:17 -0500
parents efac197c6cff
children 45dc0f874b8c
files hgext/git/manifest.py mercurial/bundle2.py mercurial/patch.py mercurial/streamclone.py mercurial/utils/cborutil.py mercurial/utils/stringutil.py
diffstat 6 files changed, 28 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/git/manifest.py	Wed Jan 15 06:56:44 2025 +0100
+++ b/hgext/git/manifest.py	Mon Jan 06 20:02:17 2025 -0500
@@ -277,7 +277,7 @@
     ) -> tuple[ByteString, ByteString]:
         raise NotImplementedError  # TODO: implement this
 
-    def _walkonetree(self, tree, match, subdir):
+    def _walkonetree(self, tree, match, subdir) -> Iterator[bytes]:
         for te in tree:
             # TODO: can we prune dir walks with the matcher?
             realname = subdir + pycompat.fsencode(te.name)
--- a/mercurial/bundle2.py	Wed Jan 15 06:56:44 2025 +0100
+++ b/mercurial/bundle2.py	Mon Jan 06 20:02:17 2025 -0500
@@ -186,6 +186,7 @@
 if typing.TYPE_CHECKING:
     from typing import (
         Dict,
+        Iterator,
         List,
         Optional,
         Tuple,
@@ -739,7 +740,7 @@
         return part
 
     # methods used to generate the bundle2 stream
-    def getchunks(self):
+    def getchunks(self) -> Iterator[bytes]:
         if self.ui.debugflag:
             msg = [b'bundle2-output-bundle: "%s",' % self._magicstring]
             if self._params:
--- a/mercurial/patch.py	Wed Jan 15 06:56:44 2025 +0100
+++ b/mercurial/patch.py	Mon Jan 06 20:02:17 2025 -0500
@@ -14,6 +14,7 @@
 import os
 import re
 import shutil
+import typing
 import zlib
 
 from .i18n import _
@@ -44,6 +45,12 @@
     stringutil,
 )
 
+if typing.TYPE_CHECKING:
+    from typing import (
+        Any,
+        Iterator,
+    )
+
 stringio = util.stringio
 
 gitre = re.compile(br'diff --git a/(.*) b/(.*)')
@@ -2792,7 +2799,9 @@
                 nextisnewline = True
 
 
-def difflabel(func, *args, **kw):
+# TODO: first tuple element is likely bytes, but was being detected as bytes|int
+#  so it needs investigation/more typing here.
+def difflabel(func, *args, **kw) -> Iterator[tuple[Any, bytes]]:
     '''yields 2-tuples of (output, label) based on the output of func()'''
     if kw.get('opts') and kw['opts'].worddiff:
         dodiffhunk = diffsinglehunkinline
--- a/mercurial/streamclone.py	Wed Jan 15 06:56:44 2025 +0100
+++ b/mercurial/streamclone.py	Mon Jan 06 20:02:17 2025 -0500
@@ -12,7 +12,7 @@
 import os
 import struct
 
-from typing import Optional
+from typing import Iterator, Optional
 
 from .i18n import _
 from .interfaces import repository
@@ -308,7 +308,7 @@
     return len(entries), total_bytes, emitrevlogdata()
 
 
-def generatev1wireproto(repo):
+def generatev1wireproto(repo) -> Iterator[bytes]:
     """Emit content for version 1 of streaming clone suitable for the wire.
 
     This is the data output from ``generatev1()`` with 2 header lines. The
--- a/mercurial/utils/cborutil.py	Wed Jan 15 06:56:44 2025 +0100
+++ b/mercurial/utils/cborutil.py	Mon Jan 06 20:02:17 2025 -0500
@@ -8,7 +8,12 @@
 from __future__ import annotations
 
 import struct
+import typing
 
+if typing.TYPE_CHECKING:
+    from typing import (
+        Iterator,
+    )
 
 # Very short very of RFC 7049...
 #
@@ -131,7 +136,7 @@
         yield encodelength(MAJOR_TYPE_NEGINT, abs(v) - 1)
 
 
-def streamencodearray(l):
+def streamencodearray(l) -> Iterator[bytes]:
     """Encode a known size iterable to an array."""
 
     yield encodelength(MAJOR_TYPE_ARRAY, len(l))
@@ -140,7 +145,7 @@
         yield from streamencode(i)
 
 
-def streamencodearrayfromiter(it):
+def streamencodearrayfromiter(it) -> Iterator[bytes]:
     """Encode an iterator of items to an indefinite length array."""
 
     yield BEGIN_INDEFINITE_ARRAY
@@ -163,7 +168,7 @@
     yield from streamencodearray(sorted(s, key=_mixedtypesortkey))
 
 
-def streamencodemap(d):
+def streamencodemap(d) -> Iterator[bytes]:
     """Encode dictionary to a generator.
 
     Does not supporting indefinite length dictionaries.
@@ -175,7 +180,7 @@
         yield from streamencode(value)
 
 
-def streamencodemapfromiter(it):
+def streamencodemapfromiter(it) -> Iterator[bytes]:
     """Given an iterable of (key, value), encode to an indefinite length map."""
     yield BEGIN_INDEFINITE_MAP
 
--- a/mercurial/utils/stringutil.py	Wed Jan 15 06:56:44 2025 +0100
+++ b/mercurial/utils/stringutil.py	Mon Jan 06 20:02:17 2025 -0500
@@ -17,6 +17,7 @@
 import typing
 
 from typing import (
+    Iterator,
     Optional,
     overload,
 )
@@ -72,7 +73,9 @@
     return b''.join(pprintgen(o, bprefix=bprefix, indent=indent, level=level))
 
 
-def pprintgen(o, bprefix: bool = False, indent: int = 0, level: int = 0):
+def pprintgen(
+    o, bprefix: bool = False, indent: int = 0, level: int = 0
+) -> Iterator[bytes]:
     """Pretty print an object to a generator of atoms.
 
     ``bprefix`` is a flag influencing whether bytestrings are preferred with