@@ -21,15 +21,10 @@ def englishFromList(items: t.Iterable[str], conjunction: str = "or") -> str:
2121 return "{0}, {2} {1}" .format (", " .join (items [:- 1 ]), items [- 1 ], conjunction )
2222
2323
24- if t .TYPE_CHECKING :
25- IntersperseU = t .TypeVar ("IntersperseU" )
26- IntersperseV = t .TypeVar ("IntersperseV" )
27-
28-
29- def intersperse (
30- iterable : t .Iterable [IntersperseU ],
31- delimiter : IntersperseV ,
32- ) -> t .Generator [IntersperseU | IntersperseV , None , None ]:
24+ def intersperse [IterT , DelimT ](
25+ iterable : t .Iterable [IterT ],
26+ delimiter : DelimT ,
27+ ) -> t .Iterator [IterT | DelimT ]:
3328 first = True
3429 for x in iterable :
3530 if not first :
@@ -38,13 +33,13 @@ def intersperse(
3833 yield x
3934
4035
41- if t . TYPE_CHECKING :
42- ProcessTextNodesU = t . TypeVar ( "ProcessTextNodesU" )
43-
44-
45- def processTextNodes ( nodes : list [ t . NodeT ], regex : re . Pattern , replacer : t . Callable ) -> list [t .NodeT ]:
36+ def processTextNodes (
37+ nodes : t . Iterable [ t . NodeT ],
38+ regex : t . Pattern ,
39+ replacer : t . Callable [[ t . Match ], t . NodeT ],
40+ ) -> list [t .NodeT ]:
4641 """
47- Takes an array of text/objects ,
42+ Takes an array of nodes ,
4843 and flatmaps reSubObject over the text parts.
4944 """
5045 ret : list [t .NodeT ] = []
@@ -56,35 +51,35 @@ def processTextNodes(nodes: list[t.NodeT], regex: re.Pattern, replacer: t.Callab
5651 return ret
5752
5853
59- if t .TYPE_CHECKING :
60- ReSubObjectU = t .TypeVar ("ReSubObjectU" )
61-
62-
6354@t .overload
64- def reSubObject (pattern : re .Pattern , string : str , repl : None ) -> list [str | re .Match ]: ...
55+ def reSubObject (
56+ pattern : t .Pattern ,
57+ string : str ,
58+ repl : None = None ,
59+ ) -> list [str | t .Match ]: ...
6560
6661
6762@t .overload
68- def reSubObject (
69- pattern : re .Pattern ,
63+ def reSubObject [ SubT ] (
64+ pattern : t .Pattern ,
7065 string : str ,
71- repl : t .Callable [[re .Match ], ReSubObjectU ],
72- ) -> list [str | ReSubObjectU ]: ...
66+ repl : t .Callable [[t .Match ], SubT ],
67+ ) -> list [str | SubT ]: ...
7368
7469
75- def reSubObject (
76- pattern : re .Pattern ,
70+ def reSubObject [ SubT ] (
71+ pattern : t .Pattern ,
7772 string : str ,
78- repl : t .Callable [[re .Match ], ReSubObjectU ] | None = None ,
79- ) -> list [str | re .Match ] | list [str | ReSubObjectU ] | list [str | re .Match | ReSubObjectU ]:
73+ repl : t .Callable [[t .Match ], SubT ] | None = None ,
74+ ) -> list [str | t .Match ] | list [str | SubT ] | list [str | t .Match | SubT ]:
8075 """
8176 like re.sub, but replacements don't have to be text;
8277 returns an array of alternating unmatched text and match objects instead.
8378 If repl is specified, it's called with each match object,
8479 and the result then shows up in the array instead.
8580 """
8681 lastEnd = 0
87- pieces : list [str | re .Match | ReSubObjectU ] = []
82+ pieces : list [str | t .Match | SubT ] = []
8883 for match in pattern .finditer (string ):
8984 pieces .append (string [lastEnd : match .start ()])
9085 if repl :
@@ -150,14 +145,6 @@ def groupFromKey(key: str, length: int = 2) -> str:
150145_groupFromKeyCache = {}
151146
152147
153- def flatten (arr : t .Iterable ) -> t .Generator :
154- for el in arr :
155- if isinstance (el , collections .abc .Iterable ) and not isinstance (el , str ) and not lxml .etree .iselement (el ):
156- yield from flatten (el )
157- else :
158- yield el
159-
160-
161148def scriptPath (* pathSegs : str ) -> str :
162149 startPath = os .path .dirname (os .path .dirname (os .path .realpath (__file__ )))
163150 path = os .path .join (startPath , * pathSegs )
@@ -183,7 +170,7 @@ def chrootPath(rootPath: str, path: str) -> str:
183170 return path
184171
185172
186- def doEvery (s : float , action : t .Callable , lastTime : float | None = None ) -> float :
173+ def doEvery (s : float , action : t .Callable [[], t . Any ] , lastTime : float | None = None ) -> float :
187174 # Takes an action every N seconds.
188175 # Pass it the duration and the last time it took the action;
189176 # it returns the time it last took the action
@@ -204,7 +191,19 @@ def doEvery(s: float, action: t.Callable, lastTime: float | None = None) -> floa
204191 SafeIndexDefaultT = t .TypeVar ("SafeIndexDefaultT" )
205192
206193
207- def safeIndex (coll : t .Sequence , needle : t .Any , default : SafeIndexDefaultT = None ) -> int | SafeIndexDefaultT : # type: ignore[assignment]
194+ @t .overload
195+ def safeIndex [ValT ](coll : t .Sequence [ValT ], needle : ValT ) -> int | None : ...
196+
197+
198+ @t .overload
199+ def safeIndex [ValT , DefaultT ](coll : t .Sequence [ValT ], needle : ValT , default : DefaultT ) -> int | DefaultT : ...
200+
201+
202+ def safeIndex [ValT , DefaultT ](
203+ coll : t .Sequence [ValT ],
204+ needle : ValT ,
205+ default : DefaultT | None = None ,
206+ ) -> int | DefaultT | None :
208207 try :
209208 return coll .index (needle )
210209 except ValueError :
0 commit comments