typing: add type hints for the overloads of `matchmod.readpatternfile()`
authorMatt Harbison <matt_harbison@yahoo.com>
Fri, 27 Sep 2024 11:57:42 -0400
changeset 51926 70fe33bdab54
parent 51925 3a90a6fd710d
child 51927 e99c007030da
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.
mercurial/match.py
--- 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