changeset 51926:70fe33bdab54

typing: add type hints for the overloads of `matchmod.readpatternfile()` The return type is conditional on an argument passed, and it very much confused both pytype and PyCharm inside `dirstate._ignorefileandline()` after adding type hints for the return value there.
author Matt Harbison <matt_harbison@yahoo.com>
date Fri, 27 Sep 2024 11:57:42 -0400
parents 3a90a6fd710d
children e99c007030da
files mercurial/match.py
diffstat 1 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/match.py	Thu Sep 26 18:52:46 2024 -0400
+++ b/mercurial/match.py	Fri Sep 27 11:57:42 2024 -0400
@@ -12,6 +12,16 @@
 import itertools
 import os
 import re
+import typing
+
+from typing import (
+    Any,
+    Callable,
+    List,
+    Tuple,
+    Union,
+    overload,
+)
 
 from .i18n import _
 from .pycompat import open
@@ -1664,6 +1674,33 @@
 
 _commentre = None
 
+if typing.TYPE_CHECKING:
+    from typing_extensions import (
+        Literal,
+    )
+
+    @overload
+    def readpatternfile(
+        filepath: bytes, warn: Callable[[bytes], Any], sourceinfo: Literal[True]
+    ) -> List[Tuple[bytes, int, bytes]]:
+        ...
+
+    @overload
+    def readpatternfile(
+        filepath: bytes,
+        warn: Callable[[bytes], Any],
+        sourceinfo: Literal[False],
+    ) -> List[bytes]:
+        ...
+
+    @overload
+    def readpatternfile(
+        filepath: bytes,
+        warn: Callable[[bytes], Any],
+        sourceinfo: bool = False,
+    ) -> List[Union[Tuple[bytes, int, bytes], bytes]]:
+        ...
+
 
 def readpatternfile(filepath, warn, sourceinfo=False):
     """parse a pattern file, returning a list of