Skip to content

Commit 532d833

Browse files
committed
Generate argument/return types from type hints #10
I added the sphinx-autodoc-typehints module, which uses typehint information to label argument types and return types, plus hyperlinks those types. The result is much more readable and usable documentation. There is unfortunately a bug with Python 3.7.5 dataclasses and type annotations that produces some warnings during builds, but this should be resolved in 3.7.6 (as well as 3.8.1).
1 parent 934a424 commit 532d833

6 files changed

Lines changed: 57 additions & 13 deletions

File tree

docs/changelog.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Changelog
2+
=========
3+
4+
0.3.0
5+
-----
6+
7+
- **Backwards compatibility break:** The CDP API avoids shadowing Python
8+
built-ins. For example, an argument called ``id`` in CDP will be renamed to
9+
``id_`` in this library. Other common names include ``type_`` and
10+
``format_``.
11+
- The documentation has been improved to be faster to load and navigate.
12+
- Better handling of the "deprecated" and "experimental" flags.

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# ones.
3030
extensions = [
3131
'sphinx.ext.autodoc',
32+
'sphinx_autodoc_typehints',
3233
'sphinx_rtd_theme',
3334
]
3435

docs/getting_started.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ types. These types can improve autocompletion and also allow you to type check
185185
your own code that uses PyCDP.
186186

187187

188+
.. _getting-started-commands:
189+
188190
Commands
189191
--------
190192

@@ -303,10 +305,10 @@ convention transparently for you.
303305
Events
304306
------
305307

306-
While each command elicits a single response, the CDP protocol provides _events_
308+
While each command elicits a single response, the CDP protocol provides *events*
307309
as a mechanism for the browser to send information to the client that is not
308-
necessarily tied to a single command/response pair. Here's an example of a CDP
309-
event definition:
310+
tied to a single command/response pair. Here's an example of a CDP event
311+
definition:
310312

311313
.. code-block:: json
312314

docs/index.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ Python wrappers for Chrome DevTools Protocol (CDP).
1313
getting_started
1414
api
1515
develop
16+
changelog
1617

17-
Indices and tables
18-
==================
18+
19+
Indices
20+
=======
1921

2022
* :ref:`genindex`
2123
* :ref:`modindex`

generator/generate.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -848,22 +848,48 @@ def generate_sphinx(self) -> str:
848848
docs += f'.. module:: cdp.{self.module}\n\n'
849849
docs += '* Types_\n* Commands_\n* Events_\n\n'
850850

851-
docs += 'Types\n-----\n'
852-
docs += '\nGenerally you do not need to instantiate CDP types ' \
853-
'yourself. Instead, the API creates objects for you as return ' \
854-
'values from commands, and then you can use those objects as ' \
855-
'arguments to other commands.\n'
851+
docs += 'Types\n-----\n\n'
852+
if self.types:
853+
docs += dedent('''\
854+
Generally, you do not need to instantiate CDP types
855+
yourself. Instead, the API creates objects for you as return
856+
values from commands, and then you can use those objects as
857+
arguments to other commands.
858+
''')
859+
else:
860+
docs += '*There are no types in this module.*\n'
856861
for type in self.types:
857862
docs += f'\n.. autoclass:: {type.id}\n'
858863
docs += ' :members:\n'
859864
docs += ' :undoc-members:\n'
860865
docs += ' :exclude-members: from_json, to_json\n'
861866

862-
docs += '\nCommands\n--------\n'
867+
docs += '\nCommands\n--------\n\n'
868+
if self.commands:
869+
docs += dedent('''\
870+
Each command is a generator function. The return
871+
type ``Generator[x, y, z]`` indicates that the generator
872+
*yields* arguments of type ``x``, it must be resumed with
873+
an argument of type ``y``, and it returns type ``z``. In
874+
this library, types ``x`` and ``y`` are the same for all
875+
commands, and ``z`` is the return type you should pay attention
876+
to. For more information, see
877+
:ref:`Getting Started: Commands <getting-started-commands>`.
878+
''')
879+
else:
880+
docs += '*There are no types in this module.*\n'
863881
for command in sorted(self.commands, key=operator.attrgetter('py_name')):
864882
docs += f'\n.. autofunction:: {command.py_name}\n'
865883

866-
docs += '\nEvents\n------\n'
884+
docs += '\nEvents\n------\n\n'
885+
if self.events:
886+
docs += dedent('''\
887+
Generally, you do not need to instantiate CDP events
888+
yourself. Instead, the API creates events for you and then
889+
you use the event\'s attributes.
890+
''')
891+
else:
892+
docs += '*There are no events in this module.*\n'
867893
for event in self.events:
868894
docs += f'\n.. autoclass:: {event.py_name}\n'
869895
docs += ' :members:\n'

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ inflection
44
mypy
55
pytest
66
sphinx
7+
sphinx-autodoc-typehints
78
sphinx-rtd-theme
8-
twine
9+
twine

0 commit comments

Comments
 (0)