Skip to content

Commit 3908f3f

Browse files
lagruOriolAbril
andcommitted
Remove container_of and support "of {key: value}" syntax
This syntax is used by Pandas [1]. I'm not entirely sold yet, but let's add it for now. To avoid confusion, literals should probably be made to only be accepted on the top-level. Otherwise something like `dict of {{"a", "b"}: int}` becomes possible. Co-authored-by: Oriol Abril-Pla <oriol.abril.pla@gmail.com>
1 parent 5c1e629 commit 3908f3f

File tree

4 files changed

+12
-24
lines changed

4 files changed

+12
-24
lines changed

examples/example_pkg-stubs/_basic.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ __all__ = [
1414

1515
def func_empty(a1, a2, a3) -> None: ...
1616
def func_contains(
17-
self,
1817
a1: list[float],
1918
a2: dict[str, Union[int, str]],
2019
a3: Sequence[int | float],
2120
a4: frozenset[bytes],
22-
) -> tuple[tuple[int, ...], list[int]]: ...
21+
a5: tuple[int],
22+
a6: list[int, str],
23+
a7: dict[str, int],
24+
) -> None: ...
2325
def func_literals(
2426
a1: Literal[1, 3, "foo"], a2: Literal["uno", 2, "drei", "four"] = ...
2527
) -> None: ...

examples/example_pkg/_basic.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def func_empty(a1, a2, a3):
2626
"""
2727

2828

29-
def func_contains(self, a1, a2, a3, a4):
29+
def func_contains(a1, a2, a3, a4, a5, a6, a7):
3030
"""Dummy.
3131
3232
Parameters
@@ -35,11 +35,9 @@ def func_contains(self, a1, a2, a3, a4):
3535
a2 : dict[str, Union[int, str]]
3636
a3 : Sequence[int | float]
3737
a4 : frozenset[bytes]
38-
39-
Returns
40-
-------
41-
r1 : tuple of int
42-
r2 : list of int
38+
a5 : tuple of int
39+
a6 : list of (int, str)
40+
a7 : dict of {str: int}
4341
"""
4442

4543

src/docstub/_docstrings.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,6 @@ def shape_n_dtype(self, tree):
239239
name = f"{name}[{', '.join(children)}]"
240240
return name
241241

242-
def container_of(self, tree):
243-
assert len(tree.children) == 2
244-
container_name, item_type = tree.children
245-
if container_name == "tuple":
246-
item_type += ", ..."
247-
out = f"{container_name}[{item_type}]"
248-
return out
249-
250242
def contains(self, tree):
251243
out = ", ".join(tree.children)
252244
out = f"[{out}]"

src/docstub/doctype.lark

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ type_or : type (("or" | "|") type)*
77
?type : qualname
88
| sphinx_ref
99
| "{" literal ("," literal)* "}" -> literals
10-
| container_of
1110
| shape_n_dtype
1211

1312
optional : "optional"
@@ -20,14 +19,11 @@ sphinx_ref : ":" (NAME ":")? NAME ":`" qualname "`"
2019
// Name with leading dot separated path
2120
qualname : (/~/ ".")? (NAME ".")* NAME contains?
2221

23-
2422
contains: "[" type_or ("," type_or)* "]"
2523
| "[" type_or "," PY_ELLIPSES "]"
26-
27-
28-
// Container-of
29-
container_of : NAME "of" type_or
30-
24+
| "of" type
25+
| "of" "(" type_or ("," type_or)* ")"
26+
| "of" "{" type_or ":" type_or "}"
3127

3228
// Array-like form with dtype or shape information
3329
shape_n_dtype : shape? ARRAY_NAME ("of" dtype)?
@@ -40,7 +36,7 @@ ARRAY_NAME : "array"
4036
| "ndarray"
4137
| "array-like"
4238
| "array_like"
43-
dtype : NAME
39+
dtype : qualname
4440
shape : "(" dim ",)"
4541
| "(" leading_optional? dim (("," dim | insert_optional))* ")"
4642
| NUMBER "-"? "D"

0 commit comments

Comments
 (0)