-
Notifications
You must be signed in to change notification settings - Fork 49
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
Support for type hints in function signatures #136
Comments
|
In many cases, type annotations can indeed replace class C:
a: int
b = True
c = 1 + 1 + 1
d = ':'.join(['x', 'y', 'z'])In this example:
As you can see in the example, even in type annotated code, types aren't always trivial. I don't think pydoctor should attempt to do full type inference, since that is a whole complex project of its own. Ideally, we'd be able to get type inference from a library, but I'm not aware of a good library that does this. The most advanced type inference for Python I've seen is in mypy, but mypy doesn't have a library or service interface at the moment that fits pydoctor's needs. The Astroid library used by PyLint should be easier to integrate, but it doesn't have PEP 484 support yet, so it doesn't fit our needs either, at least not in the near future. Still, even if we support only explicit and trivially inferred types, that would already avoid a lot of duplicate type information, so I think that's something we should absolutely do. |
|
Another thing to consider is whether we want to support type hints when running on Python versions before 3.6, or support the use of Note that the Personally, I'm not in favor of supporting type hints on Python versions below 3.6. It would require additional effort that I think is better spent elsewhere, not only to implement it but also in having to maintain and test support for two parsers. Additionally, I think it would be very useful to adopt type annotations in the pydoctor source itself, which would require pydoctor to run on Python 3.6 or higher, eliminating one of the advantages |
|
Support for 2.7-compatible type hints is clearly not worth the bother. I don't think that requiring
|
|
I'm working on an implementation of type hints support that uses annotations from nodes produced by the Since #170 class C:
def __init__(self, secret: int) -> None:
self.secret = secret
"""Code to open my luggage."""
self.merchandise: List[str] = []
"""Where the real money from the movie is made."""In the latest release the mypy daemon got experimental support for returning inferred types for functions. So this might be a way to get type inference in the future. |
|
Based on https://github.com/twisted/pydoctor/pull/175/files#diff-1e6d0f0013af649be87039388d3b105dR682 I think this ticket can be marked "closed"? Or is there something that still needs to be done (in which case I would like to understand what)? |
|
The main thing that is still missing is support for annotations and type comments on function signatures. The PR you linked implements support for variables only. I had a look at implementing it for functions, but the code isn't structured well for that: we'd have to merge type information discovered at different times (when processing AST nodes and when parsing docstrings), but the I'd prefer to get a release out before doing major refactoring, for multiple reasons:
While the branch coverage of our unit tests is starting to approach acceptable levels, this may be deceptive, since a lot of data structures are used in ad-hoc ways and the unit tests don't cover a whole lot of state space. I'd feel a lot more confident making invasive changes if I could use mypy in addition to unit and system tests. In theory mypy can run on Python 2.7 code, but the type comments syntax is just too painful to work with in my opinion, as I'm used to Python 3.6 syntax now. |
|
Thanks for the explanation! This makes sense. Two questions: [1] Can we retitle this issue to "support for type hints in function signatures" to make it clearer? |
|
PR #199 and #200 are ready to be reviewed; I'd like to get those in before a release. The README could use an update, since I think pydoctor is useful for projects that are looking for a successor to Epydoc, so its potential audience is not only Twisted. People from other projects have already inquired about a release, see #96. I also think that every release should include some kind of release notes or change log, even if it's minimal. It seems pydoctor didn't do this in the past, but if we want to broaden its use beyond Twisted, we should communicate to users why they'd want to update. All in all, it shouldn't be a lot of work, but I can't review my own PRs, so I will need some help. @twm and @glyph together reviewed seven PRs this week, so I'm not complaining, but I'd really like to get the two remaining ones in as well. |
|
Only PR #200 is remaining now. I'd really like to get that one in, since it fixes errors that are visible in the output. It also eliminates the last errors that the |
|
Posted a review. Let me know if there are any other blockers (for example, if you do decide to split the PR, or need any reviews for the release process! Feel free to e-mail me directly. |
Address #136: Add support for function annotations
|
The next release of pydoctor will have support for type annotations on functions. There is some simple type inference done for variables. If this is insufficient, users can work around that by explicitly adding annotations or So I'm closing this issue now. If we ever want to improve type inference in the future, that can be tracked in a new issue. |
Amazing. This is fantastic news. How long until the next release? :) |
A few days at most. See #290 for details. |
If one has type hints in code, adding
@typeand@rtypeinfo in doc strings is redundant.The text was updated successfully, but these errors were encountered: