Introducing type-checking #2749
Replies: 5 comments 3 replies
-
|
@pplantinga @mravanelli @asumagic @Adel-Moumen inputs may be welcome here. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks @rogiervd for this, I think we will all agree that this is important. A few comments:
|
Beta Was this translation helpful? Give feedback.
-
|
I have historically been a little skeptical of Python type hints, but I'll try to keep an open mind about it for the purposes of this discussion. Just to explain, I find that often they obscure rather than clarify function definitions, without providing any guarantees. def function(a, b=None, c=None):
"""
Arguments
---------
a : torch.Tensor
b : dict, default None
c : list of lists of ints, default None
Returns
-------
int or None
"""is just much clearer to me than: def function(a : torch.Tensor, b : Optional[Mapping[str, Any]] = None, c : Optional[Iterable[Iterable[int]]] = None) -> Optional[int]:
...My second impression is that the type system can sometimes be too restrictive, marking perfectly fine code as violating type safety, making it an annoying burden for developers. The risk is that we overuse However, I'm certainly willing to admit that as the toolkit grows and is depended on by more users, perhaps the verbosity and extra developer burden is worth the trouble in order to get people to think more carefully about the structure of the arugments. In addition, perhaps this could encourage us to use more data classes that encapsulate the arguments we send between functions. This is sort of a parallel issue but seems related to me: it seems we ought to have a data class for As for specific tools, I'm not familiar with these but a brief investigation made it seem like |
Beta Was this translation helpful? Give feedback.
-
|
I would be in favor of this. In addition to other comments, I would tend towards one that integrates well with IDEs and/or that is fast to execute. (It's also big enough that it may be required to find a way to approach the problem incrementally) Something I long wished for would be a way to add tensor shape information and checking to static type information, but last I checked tooling around this looked fairly immature. One thing where I think we will always fall short is linting in training scripts, since most typing information is gone when you go through hyperpyyaml-loaded namespaces. It would be neat if the chosen linter would allow executing arbitrary code to infer those types from a sample yaml, but that seems tricky.
This might increase the amount of workarounds in the codebase for each linter. I would stick to one. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks all for the comments. Picking out a subset of points:
I think it would be but still, it's a matter of taste.
This is specifically why I mentioned the Brain class, which uses
Good to know I'm not the only one who feels this. I believe
I think this is out of scope for this proposal, essentially because no one has written a type checker for hyperpyyaml yet. I think the closest thing would be to use pydantic (again, at run time), and a type-annotated dataclass that the hyperyaml can be loaded into. (I would prefer |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to propose introducing type checking as an additional check in CI and possibly a pre-commit hook. I would propose leaving type annotations optional, but reviewers can encourage this where it makes sense.
Type checking provides an extra check for correctness. Type checking also encourages type annotations, which (1) are documentation for users and (2) can be used by IDEs to give hints.
Four type checkers are in general use for Python.
Three use a similar paradigm:
mypy. It is used by default by VS Code.One type checker is different:
Personally, I like pytype, but on my indicative test it takes four minutes to run, which may become a pain.
I don't think you'd want to put pytype in a pre-commit check, whereas pyright takes 30 seconds and pyre 10 seconds.
It may be possible to run multiple type checkers to check for the union of the problems they detect.
I imagine the process of adding type checks would involve quite a few PRs over time:
I am volunteering to drive this, though I would need some help where I don't understand the code well enough.
A few types of changes that would be required to make type checking pass, judging from running a few type checkers over the current tree:
# type: ignoretoimportstatements for packages that are not inrequirements.txt, likematplotlib.# type: ignoreto code that edits attributes of objects at run time. E.g. the Brain class, which is particularly dynamic.assert argument_name is not Nonein places where this assumption is currently implicit.__init__.pyfiles if necessary, maybe adding__all__ = [...].ArrayLikecan be used.In the long term, I would envision these changes:
# type: ignorethat it will have a harder time getting through code review.Beta Was this translation helpful? Give feedback.
All reactions