Skip to content

Commit 5a28828

Browse files
lagruOriolAbril
andcommitted
Only allow literals on top-level
which avoids potentially confusing constructs like `dict of {{"a", "b"}: int}` Co-authored-by: Oriol Abril-Pla <oriol.abril.pla@gmail.com>
1 parent 3908f3f commit 5a28828

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/docstub/_docstrings.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,20 @@ def transform(self, tree):
173173
finally:
174174
self._collected_imports = None
175175

176-
def doctype(self, tree):
176+
def annotation(self, tree):
177177
out = " | ".join(tree.children)
178178
return out
179179

180-
def type_or(self, tree):
180+
def types_or(self, tree):
181181
out = " | ".join(tree.children)
182182
return out
183183

184184
def optional(self, tree):
185185
out = "None"
186186
literal = [child for child in tree.children if child.type == "LITERAL"]
187187
assert len(literal) <= 1
188-
if len(literal) == 1:
189-
out = lark.Discard # Should be covered by doctype
188+
if literal:
189+
out = lark.Discard # Type should cover the default
190190
return out
191191

192192
def extra_info(self, tree):

src/docstub/doctype.lark

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
?start : doctype
1+
?start : annotation
22

3-
doctype : type_or ("," optional)? ("," extra_info)?
3+
annotation : (literals | types_or) ("," optional)? ("," extra_info)?
44

5-
type_or : type (("or" | "|") type)*
5+
literals : "{" literal ("," literal)* "}"
6+
7+
types_or : type (("or" | "|") type)*
68

79
?type : qualname
810
| sphinx_ref
9-
| "{" literal ("," literal)* "}" -> literals
1011
| shape_n_dtype
1112

1213
optional : "optional"
@@ -19,11 +20,11 @@ sphinx_ref : ":" (NAME ":")? NAME ":`" qualname "`"
1920
// Name with leading dot separated path
2021
qualname : (/~/ ".")? (NAME ".")* NAME contains?
2122

22-
contains: "[" type_or ("," type_or)* "]"
23-
| "[" type_or "," PY_ELLIPSES "]"
23+
contains: "[" types_or ("," types_or)* "]"
24+
| "[" types_or "," PY_ELLIPSES "]"
2425
| "of" type
25-
| "of" "(" type_or ("," type_or)* ")"
26-
| "of" "{" type_or ":" type_or "}"
26+
| "of" "(" types_or ("," types_or)* ")"
27+
| "of" "{" types_or ":" types_or "}"
2728

2829
// Array-like form with dtype or shape information
2930
shape_n_dtype : shape? ARRAY_NAME ("of" dtype)?

0 commit comments

Comments
 (0)