Skip to content

Commit

Permalink
Better document branch priorities.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlatr committed Sep 20, 2024
1 parent 8f82e87 commit 0650c35
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions docs/source/codedoc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -338,41 +338,48 @@ Branch priorities
-----------------

When pydoctor deals with try/except/else or if/else block, it makes sure that the names defined in
the "principal" flow do not get overridden by names defined in the except handlers or ifs' else block.
New names defined in these block will be processed normally.
the main flow has precedence over the definitions in ``except`` handlers or ``else`` blocks.

Meaning that in the context of the code below, ``ssl`` would resolve to ``twisted.internet.ssl``:

.. code:: python
try:
# main flow
from twisted.internet import ssl as _ssl
except ImportError:
ssl = None
# exceptional flow
ssl = None # ignored since 'ssl' is defined in the main flow below.
var = True # not ignored since 'var' is not defined anywhere else.
else:
# main flow
ssl = _ssl
Similarly, in the context of the code below, the first ``CapSys`` class will be
documented and the second one will be ignored.
Similarly, in the context of the code below, the ``CapSys`` protocol under the ``TYPE_CHECKING`` block will be
documented and the runtime version will be ignored.

.. code:: python
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# main flow
from typing import Protocol
class CapSys(Protocol):
def readouterr() -> Any:
...
else:
class CapSys(object): # ignored
# secondary flow
class CapSys(object): # ignored since 'CapSys' is defined in the main flow above.
...
.. But sometimes pydoctor can be better off analysing the ``TYPE_CHECKING`` blocks and should
.. stick to the runtime version of the code instead.
.. You can instruct pydoctor do to such things with a custom system class.
.. .. code:: python
.. class MySystem(model.System):
.. eval_if = {'my_mod':{'TYPE_CHECKING':False}}
But sometimes pydoctor can be better off analysing the ``TYPE_CHECKING`` blocks and should
stick to the runtime version of the code instead.
For these case, you might want to inverse the condition of if statement:

.. code:: python
if not TYPE_CHECKING:
# main flow
from ._implementation import Thing
else:
# secondary flow
from ._typing import Thing # ignored since 'Thing' is defined in the main flow above.

0 comments on commit 0650c35

Please sign in to comment.