Narrow types and complete doc strings in shapes#2057
Conversation
|
I'm not sure that I agree with removing the Shape annotations. The design rule of the free func API was to support Shape inputs wherever possible. That is why there are |
|
I think I see your point - since the return type must by What I don't like is the bare
|
|
|
I want to be able to write (& teach) code that has as useful type checks as possible. The only reason those methods work with mypy is because of the plugin written specifically for that purpose. But many people (& companies) write code in VS Code where types are checked with Pylance, and they use pyright instead of mypy. A red squiggle in the editor under a wrong input type is much more helpful than an exception at runtime (especially from the beginner's perspective). But I see the problem now that I just pushed a change that (apart from removing my type narrowing) adds What do you think? This would make most of the mypy plugin redundant, actually. |
|
OK, I still don't get why you want to touch/use/teach those internals and why they would cause issues in user code. Regarding VSCode et al, it might be good to expose students to tools that are more open source oriented. Here is an experimental PR that gets rid of the plugin in a way that is somewhat acceptable (i.e. hardcodes max arity), I'm not sure yet if it is time well spent and the benefit justifies the cost: #2060 |
|
My motivation was of course not to teach internals (heaven forbid) but to make public methods more reliably typed for static type checkers (= less surprises at runtime). |
|
Thanks, clear. Could confirm that #2060 results in the desired behavior with your toolchain? |
This PR narrows some of the type hints in the free function API (shapes module). Rather than formally accepting
Shape, I added a union of the actually supported types.Compoundis something that static type checking can do nothing about, so we just accept it. One unit test is added because omittingCompoundfrom the type hints would have lead to a change in runtime behavior in some of the multidispatched functions which wouldn't have been caught by the existing test.I also took the liberty to expand the doc strings of the modified functions, using the same Sphinx style for the parameter docs.
Claude Code used to prepare this PR.
Draft status as I expect black failures, will fix afterwards 😊