Skip to content

Commit fc4a7c6

Browse files
committed
Fix allcaptures()/allspans() return type in regex stubs
Both were typed as fixed one-element tuples, tuple[list[AnyStr]] and tuple[list[tuple[int, int]]], even though the runtime always returns one entry per capture group, a count that varies with the pattern. A type checker refuses to unpack or index past the first element of a fixed-length tuple, so any code doing that against the real, variable return value failed to type-check even though it ran correctly. Changed both to the variable-length tuple form, matching what the neighboring captures() method already uses for the same shape. Fixes #566
1 parent dcc366e commit fc4a7c6

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__ import annotations
2+
3+
from typing import List, Tuple
4+
from typing_extensions import assert_type
5+
6+
import regex
7+
8+
# Regression test for #566: allcaptures()/allspans() were typed as fixed
9+
# 1-tuples, which made a type checker refuse to unpack or index past the
10+
# first element even though the runtime always returns one entry per group,
11+
# a count that depends on the pattern rather than being fixed at one.
12+
m = regex.match(r"(\w+) (\w+)", "hello world")
13+
assert m is not None
14+
assert_type(m.allcaptures(), Tuple[List[str], ...])
15+
assert_type(m.allspans(), Tuple[List[Tuple[int, int]], ...])

‎stubs/regex/regex/_main.pyi‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,8 +746,8 @@ class Match(Generic[AnyStr]):
746746

747747
def capturesdict(self) -> dict[str, list[AnyStr]]: ...
748748
def detach_string(self) -> None: ...
749-
def allcaptures(self) -> tuple[list[AnyStr]]: ...
750-
def allspans(self) -> tuple[list[tuple[int, int]]]: ...
749+
def allcaptures(self) -> tuple[list[AnyStr], ...]: ...
750+
def allspans(self) -> tuple[list[tuple[int, int]], ...]: ...
751751

752752
@overload
753753
def __getitem__(self, key: Literal[0], /) -> AnyStr: ...

0 commit comments

Comments
 (0)