Type hints! #1635
Replies: 7 comments 14 replies
-
One additional point for type hints + type checkers (and is a big plus, IMO). You get the ability to add type checking to CI. |
Beta Was this translation helpful? Give feedback.
-
@drewj-usnctech ... def add(a: float, b: float) -> float:
return a + b This is not sufficient. Nuclear code is complicated, we aren't building website; we need docstrings that describe things in words. I'm afraid I couldn't get something like this past QA or management. So, we should change this example to also include some sort of docstring. |
Beta Was this translation helpful? Give feedback.
-
Casting my vote for preferring type hints in signatures. As someone who builds off of ARMI heavily, having my IDE know the type of thing returned from a function/method is immensely valuable. Otherwise I need to find the function in the documentation and follow that rabbit trail. But, as someone who struggles with balancing type hints and docstring type information, I feel the pain of the duplicity. Especially when you want to add commentary to a input parameter, you're going to want to document that somehow. |
Beta Was this translation helpful? Give feedback.
-
I voted numpy for v1.0.0 After that, y'all do whatcha wanna...I'll review the PRs! |
Beta Was this translation helpful? Give feedback.
-
@jakehader has proposed another option: beartype. |
Beta Was this translation helpful? Give feedback.
-
My vote is for a combination of docs & hints for this phase of development. Encourage developers to add type hinting, but don't force it. It's been my experience that type hinting can be a powerful tool for documentation. It can also help guide good code design, and improve the development experience. Type hinting can also be a rabbit hole where the benefits of a duck typed language vanish (i.e., rapid prototyping and quick development). In many cases, it makes more sense to commit to a compiled language with strong typing after a certain point. Especially when performance is a valuable design objective. Echoing what @john-science said, I've heard automatic stub generation isn't a silver bullet. There will be additional work to get 100% typing. Not to mention, the continued effort to maintain it. Also, it'll add more work to integrate type checking into the CI. None of these things are necessarily bad, they're just things to consider. |
Beta Was this translation helpful? Give feedback.
-
Well, we have a reality check coming. I have tried to implement type hinting in ARMI. Unfortunately, this will require a near total rewrite of the ARMI API. The problem is...
The ARMI API was written without type hints, so adding them means re-writing the API to prevent circular imports. Upon my first try, I found that adding type hints to 1000 Python methods resulted in ~100 separate circular imports that need to be fixed. This would mean reorganizing the entire codebase, and rewriting a large chunk of it. This would be the mother of all API-breaking changes for our downstream users.
If I'm going through the exercise of rewriting the API to that level, I would prefer to just rewrite ARMI in Rust. |
Beta Was this translation helpful? Give feedback.
-
Pulling out a thread from ARMI 1.0.0 pathway to talk about type hints in Python!
Not intending for this to force the dev team one way or another, but felt like a good way to get a sense for what devs and users want
Full numpydocs
No use of type annotations in signatures. Putting all the type information in the docstring, e.g.,
Pro: Tools like sphinx for documentation pick this up super well. You can add descriptive information about parameters and returned objects in your docstring. And in most cases you're probably writing a docstring already
Con: IDEs and type checkers do not parse docstrings so you don't get any nice typed information when developing. In cases like VSCode, you can hover over a function to see the docstring, but the returned object usually has a type of
Any
because the type-checker doesn't know what was returnedFull type annotations
Put type hints right in the signature, e.g.,
Pro: IDEs and type checkers parse this so you'll get typed information when developing. Sphinx appears to handle signature annotations with the autodoc extension
Con: You're probably going to still want to write a docstring for some or most cases (e.g., purpose of the class, class attributes, exceptions raised, notes, etc.) so then do you exclude documenting parameters and return values? What if you want to add context to a variable?
Both?
Eat the duplicity because type checkers in IDEs are useful but also types and explanations in documentation is also useful. But which is more useful?
Stub files?
Generate and ship some stub files with the repo? #531 used
monkeytype
on a small set of the code base, so there's a precedence?6 votes ·
Beta Was this translation helpful? Give feedback.
All reactions