-
Notifications
You must be signed in to change notification settings - Fork 199
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
Fix #1186: list-methods of type-annotated class #1188
Conversation
Turn annotations into a string which PYON can encode.
Test pc_rpc can correctly process an annotated function. Also check pyon can encode & decode the annotated function. Signed-off-by: Drew Risinger <drewrisinger@users.noreply.github.com>
artiq/protocols/pc_rpc.py
Outdated
@@ -487,6 +487,17 @@ def __init__(self, targets, description=None, builtin_terminate=False, | |||
else: | |||
self._noparallel = asyncio.Lock() | |||
|
|||
@staticmethod | |||
def _document_function(function: typing.Callable) -> typing.Tuple[dict, str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the type annotations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain why the type annotations should be removed? Other than not being used elsewhere in ARTIQ (#1181 (comment))?
There is no downside to using type annotations, it makes the code more understandable, and it helps IDEs with autocomplete. Guido van Rossum supports them, and they are making their way into more projects as Python2 support is dropped. Some projects that use them:
- Pylint: Annotate pylint code with type annotations pylint-dev/pylint#2079
- Black (formatter): https://github.com/ambv/black/blob/master/black.py
Overview of type hints and their benefits; https://stackoverflow.com/questions/32557920/what-are-type-hints-in-python-3-5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are not used anywhere else in ARTIQ, they are not used by most other Python projects, they are difficult to write, some functions may return different types depending on the context, and annotations are ignored by the interpreter and are (currently at least) not checked automatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussion continued below in #1188 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Apart from the one style thing this LGTM
@jordens FYI, I found a few other instances of that same return style in that file. artiq/artiq/protocols/pc_rpc.py Line 130 in abad916
artiq/artiq/protocols/pc_rpc.py Line 239 in abad916
|
@drewrisinger touché. Then either style is fine. |
Just wanted to let you know since fixing them would be out of scope for this PR |
artiq/test/test_pc_rpc.py
Outdated
return arg1 | ||
|
||
argspec, docstring = pc_rpc.Server._document_function(_annotated_function) | ||
print(argspec) # for debug |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prints in tests are generally "for debug", this comment doesn't provide much info.
@drewrisinger To clarify my position: while i am opposed to using type annotations inside ARTIQ, merging this and having |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comments above.
Continuing the discussion on type annotations:
My understanding is that this is due to the nature of ARTIQ. ARTIQ has received contributions from people with varying levels of programming experience, who might be unaware that type annotations exist.
This is mostly due to them being a new/less-documented feature in Python 3. Their usage is spreading as more projects transition to Python 3. Similar features like generators & async code are not widely used in beginner projects, but have their usage in large & advanced projects. And projects that do not use them, like
I disagree: no more difficult than a comment once you know how to use them. Proper usage can be learned in about 10-15 mins for 95% of use cases.
Not an issue, and easily handled. Returns can be of the form
This is one of the main benefits. Type annotations can be statically checked to catch obvious mistakes (e.g. In summary, I strongly believe that type annotations are best practice and very useful for documentation and code understandability. Especially in Python when it is nearly impossible to tell what data type a function expects, combined with a general lack of function documentation in ARTIQ. As such, all my code does and will continue to use type annotations, because to my cost-benefit analysis their marginal cost comes with a large comprehensibility benefit for users and future maintainability. |
I started writing ARTIQ and wrote large parts of it, and I am obviously aware of type annotations since I've been using them in the compiler. I just don't want them elsewhere.
Generators add useful features and are not similar to useless type annotations.
Then write or fund such documentation instead of adding those cryptic type annotations.
As such, your code will continue to get rejected. |
If those tools were good, I'd be all for using them. This is why we switched the firmware from C to Rust, for example. But with Python they are half-baked hacks that are not really worth the effort. |
@drewrisinger Thanks for keeping the conversation on this going. @sbourdeauducq said
Type annotations were supported in Python 3.5 which was released in autumn 2015. By that time @sbourdeauducq already had spent two years on ARTIQ -- v1 was released May 2016. So it wasn't really an option for most of the early ARTIQ infrastructure.
There's a nice medium post discussing additional benefits of type annotations.
Automatic code completion is another great IDE feature that works better with type annotations. The same Medium post provides an example illustrating this. This could really make learning and writing in ARTIQ Python much easier. |
When they work! Which isn't always the case with the Python tools unlike e.g. Rust. |
Removed comment per @sbourdeauducq. Clarify variable names b/c they didn't make sense when re-reading months later.
Type annotations removed per @sbourdeauducq, so no longer used.
@drewrisinger Thanks! |
@drewrisinger Please run tests before submitting PRs. You ticked the box but your unit test fails. |
@drewrisinger Please submit a fix. |
Fixes bug from 5108ed8. Truth value of multi-element numpy array is not defined. Completes m-labs#1186 and fixes/amends m-labs#1188. Signed-off-by: Drew Risinger <drewrisinger@users.noreply.github.com>
#1239. Had tested originally, forgot why I couldn't add extra element to array. |
Fixes bug from 5108ed8. Truth value of multi-element numpy array is not defined. Completes m-labs#1186 and fixes/amends m-labs#1188. Signed-off-by: Drew Risinger <drewrisinger@users.noreply.github.com>
ARTIQ Pull Request
Description of Changes
Change how
pc_rpc
process annotations. Essentiallyannotations -> str(annotations)
which can be serialized bypyon
.Related Issue
Closes #1186
Type of Changes
Steps (Choose relevant, delete irrelevant before submitting)
All Pull Requests
git commit --signoff
, see copyright).Code Changes
flake8
to check code style (follow PEP-8 style).flake8
has issues with parsing Migen/gateware code, ignore as necessary.artiq_rpctool SERVER list-methods
Documentation Changes
None
Git Logistics
git rebase --interactive
). Merge/squash/fixup commits that just fix or amend previous commits. Remove unintended changes & cleanup. See tutorial.git show
). Format: