Skip to content

Commit cc5dc3e

Browse files
committed
Join "optional" and "extra_info" rules in grammar
Since the "optional" statement isn't used in any way right now, enforcing a specific layout for what comes after the "doctype" serves no useful purpose. I don't intend docstub to be linter so I don't think it's likely that we will ever enforce annotating that a parameter is "optional" in the docstring – while it's nice to do so, it's reasonably easy to see it from the signature. Update the docs accordingly. Also try to be more precise about differentiating "doctypes" from valid Python types.
1 parent 395c23f commit cc5dc3e

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

docs/typing_syntax.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77
> Several features are experimental and included to make adoption of docstub easier.
88
> Long-term, some of these might be discouraged or removed as docstub matures.
99
10-
Docstub defines its own [grammar](../src/docstub/doctype.lark) to parse and transform type information in docstrings into valid type annotations.
10+
Docstub defines its own [grammar](../src/docstub/doctype.lark) to parse and transform type information in docstrings (doctypes) into valid Python type expressions.
1111
This grammar fully supports [Python's conventional typing syntax](https://typing.python.org/en/latest/index.html).
12-
So any type annotation that is valid in Python, can be used in a docstrings as is.
12+
So any type expression that is valid in Python, can be used in a docstrings as is.
1313
In addition, docstub extends this syntax with several "natural language" expressions that are commonly used in the scientific Python ecosystem.
1414

15-
Docstrings are expected to follow the NumPyDoc style:
15+
Docstrings should follow a form that is inspired by the NumPyDoc style:
1616
```
1717
Section name
1818
------------
19-
name : annotation, optional, extra_info
19+
name : doctype, extra_info
2020
Description.
2121
```
2222

23-
- `name` might be the name of a parameter or attribute.
24-
Other sections like "Returns" or "Yields" are supported.
25-
- `annotation` the actual type information that will be transformed into the type annotation.
26-
- `optional` and `extra_info` can be appended to provide additional information.
27-
Their presence and content doesn't currently affect the resulting type annotation.
23+
- `name` might be the name of a parameter, attribute or similar.
24+
- `doctype` the actual type information that will be transformed into a Python type.
25+
- `extra_info` is optional and captures anything after the first comma (that is not inside a type expression).
26+
It is useful to provide additional information for readers.
27+
Its presence and content doesn't currently affect the resulting type annotation.
2828

2929

3030
## Unions

docs/user_guide.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,21 @@ def example_metric(
9393
There are several interesting things to note here:
9494
9595
- Many existing conventions that the scientific Python ecosystem uses, will work out of the box.
96-
In this case, docstub knew how to translate `array-like`, `array of dtype uint8` into a valid type annotation in the stub file.
97-
In a similar manner, `or` can be used as a "natural language" alternative to `|`.
96+
In this case, docstub knew how to translate `array-like`, `array of dtype uint8` into a valid Python type for the stub file.
97+
In a similar manner, `or` can be used as a "natural language" alternative to `|` to form unions.
9898
You can find more details in [Typing syntax in docstrings](typing_syntax.md).
9999
100-
- Optional arguments that default to `None` are recognized and a `| None` is appended automatically if the type doesn't include it already.
100+
- Optional arguments that default to `None` are recognized and a `| None` is appended automatically.
101101
The `optional` or `default = ...` part don't influence the annotation.
102102

103103
- Referencing the `float` and `Iterable` types worked out of the box.
104-
All builtin types as well as types from the standard libraries `typing` and `collections.abc` module can be used.
104+
All builtin types as well as types from the standard libraries `typing` and `collections.abc` module can be used like this.
105105
Necessary imports will be added automatically to the stub file.
106106

107107

108-
## Using types & nicknames
108+
## Referencing types & nicknames
109109

110-
To translate a type from a docstring into a valid type annotation, docstub needs to know where that type originates from and how to import it.
110+
To translate a type from a docstring into a valid type annotation, docstub needs to know where names in that type are defined from where to import them.
111111
Out of the box, docstub will know about builtin types such as `int` or `bool` that don't need an import, and types in `typing`, `collections.abc` from Python's standard library.
112112
It will source these from the Python environment it is installed in.
113113
In addition to that, docstub will collect all types in the package directory you are running it on.

src/docstub/doctype.lark

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// The basic structure of a full docstring annotation as it comes after the
1313
// `name : `. It includes additional meta information that is optional and
1414
// currently ignored.
15-
?annotation_with_meta: type ("," optional)? ("," extra_info)?
15+
?annotation_with_meta: type ("," extra_info)?
1616

1717

1818
// A type annotation. Can range from a simple qualified name to a complex
@@ -132,13 +132,8 @@ shape: "(" dim ",)"
132132
?dim: INT | ELLIPSES | NAME ("=" INT)?
133133

134134

135-
// Optional information about a parameter has a default value, added after the
136-
// docstring annotation. Currently dropped during transformation.
137-
optional: "optional" | "default" ("=" | ":")? literal_item
138-
139-
140-
// Extra meta information added after the docstring annotation.
141-
// Currently dropped during transformation.
135+
// Extra meta information added after the docstring annotation, e.g. "optional"
136+
// or "default: 3". Information is dropped and not used to generate stubs.
142137
extra_info: /[^\r\n]+/
143138

144139
// A simple name. Can start with a number or character. Can be delimited by "_"

0 commit comments

Comments
 (0)