Replies: 1 comment 4 replies
-
This is an interesting idea. It's worth considering. Pyright, the static type analyzer that underlies pyright, does already perform some advanced type inference for unannotated functions. In particular, it performs call-site inference in an attempt to infer return types. That functionality is intended primarily for users who are interested in more accurate completion suggestions but are not interested in bug detection. It sounds like you are interested in pylance helping you find bugs in your code. Do you have type checking diagnostics enabled? That is, have you set "python.analysis.typeCheckingMode" to "basic"? Pyright does not currently infer the types of unannotated parameters except for override methods where the base class provides type annotations. What you're suggesting would go beyond that and infer protocol types based on clues within the function's implementation. I'm not aware of other static type checking tools that do this. I'm quite certain that mypy, pyre, and pytype do not do this. Nor does TypeScript do this for unannotated javascript code. You mentioned that PyCharm's type checker does this. Could you point me to documentation for this feature? I'm skeptical that PyCharm is doing what you're suggesting, but it is possible that they're doing something different that is able to catch certain classes of bugs that pyright does not currently. Does this feature in PyCharm catch the bug in your specific code sample? One challenge I see with your proposed solution is that it would be difficult or impossible to generate intelligible and actionable error messages at the call site. If PyCharm is able to detect the bug in the code above, I'd be interested in what error message it outputs and what code location it reports the bug against. A big challenge with this solution is generating an accurate protocol definition. Python has many behaviors that are difficult to discern from looking at a code snippet. For example, you said "we can infer that the argument 'vector' must have an attribute called values". That's not strictly true. The argument So my concern with your proposed approach is that it would produce a lot of false positive errors, and the resulting error messages would be difficult to understand leaving the user with no idea how to address them. The proposal would also be very computationally expensive, so it would significantly increase analysis times — the time for providing completion suggestions during editing. |
Beta Was this translation helpful? Give feedback.
-
One point where VSCode lacks behind in python development when compared to other code editors/IDEs is in the ability to detect more errors by statically analyzing the code.
Take this example of code:
The last line is incorrect as we are passing a list to the function, but it is not compatible with how the function works.
Because the function is unnanotated, VSCode does not report any error. However other IDEs (e.g. pycharm) can detect that something is wrong without having to run the code.
Could VS Code have something like this? It would really take the editor experience with python to the next level, even when writing unnanotated code.
Here there is how it could be done (and how I think other IDEs work). The main idea is to infer a Protocol for each unnanotated function argument:
In this case we can infer that the argument 'vector' must have an attribute called
values
and must define the function__len__
.The function definition would be equivalent to:
Now, VSCode could give very useful errors when calling
compute_mean([1, 2, 3])
, as it would detect that the passed argument is incorrect because a list does not have any attributevalues
.TL;DR:
When defining an unnanotated function like the following:
VSCode could automatically detect it as the following piece of code behind the scenes:
This would help users catch errors earlier even if the code is not annotated. This functionality is present in other IDEs such as pycharm.
Beta Was this translation helpful? Give feedback.
All reactions