-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling of "if TYPE_CHECKING:" blocks #174
Comments
if TYPE_CHECKING:
blocks
What about we introduce the following new option:
We would parse the string
For now, only
Which would be very useful to consider the whole project under a certain python version. But having the feature to compare as string and store only booleans is still a good step forward. It will fix this issue for sure. The question is does unparsing all |
Or maybe these configuration should only be accessible by overriding a system class variable at first. Since this will be an experimental feature that could be changed down the road. class System(model.System):
eval_if = {"**":{"typing.TYPE_CHECKING":False}} |
What about the following rationale? @mthuurne All statements in the 'orelse' block of Meaning that in the context of the code below, try:
from twisted.internet import ssl as _ssl
except ImportError:
ssl = None
else:
ssl = _ssl See #589 for tests. Then, we can teak |
The workaround, which should be clearly documented is to use |
Is this the only reason klein’s API docs are generated with Sphinx instead of pydoctor @glyph ? |
I honestly didn't even realize that Klein's API docs were being generated by Sphinx :). I didn't set that up. Happy to switch to Pydoctor if someone can do the PR. |
Correction: I’m not sure Klein’s API docs are even generated at all. Can’t find a link to the docs. @wsanchez |
OK, in that case, let's just set up pydoctor. 90% sure that Klein's docstrings are epytext. At least, the ones I've written are :) |
Klein uses epytext and the I never understood Sphinx, so I never understood how the |
I’ll open a PR to integrate pydoctor with the Klein’s Sphinx build. |
It is sometimes necessary to have code in a module (or more rarely, in a class) that is only there for the benefit of a static code analyzer and is skipped at runtime. In particular, there is the constant
typing.TYPE_CHECKING
that type checkers can evaluate toTrue
, while its runtime value is alwaysFalse
. This is supported by mypy and possibly other analyzers.A common purpose of
if TYPE_CHECKING:
blocks is to contain imports that are only necessary for the purpose of type annotations. These might be circular imports at runtime. When these types are used to annotate arguments or return values, they are relevant to pydoctor as well: either to extract type information from annotations or because ofL{name}
tags inside the docstring, where pydoctor has to be able to look up the fully qualified version ofname
.In other cases,
if TYPE_CHECKING:
blocks are used to perform trickery to make mypy accept code that is difficult to analyze. In these cases, pydoctor can be better off analysing the runtime version of the code instead. For details, read the comments in this Klein PR.A related issue is that pydoctor doesn't handle duplicate definitions particularly elegantly, see #33. For example, if both the
if
andelse
branch define the same name, one of them will have a name with␣0
(space + zero) appended and that patched name not only shows up visibly in the generated documentation, it also breaks hyperlinks to the corresponding pages, since it not only escapes the link itself (for example,klein.resource%200.html
), but it also escapes the file name: it generates a file namedklein.resource%200.html
instead ofklein.resource 0.html
.On the one hand, it would be nice to do the right thing in as many cases as possible. On the other hand, perfect code analysis is impossible and trying too hard to handle complex code will consume a lot of effort and complicate pydoctor's code. So we have to find a balance somewhere that makes pydoctor perform in a simple and predictable way that we can communicate to the end user and that they can adapt their code to.
The text was updated successfully, but these errors were encountered: