Dynamic runtime type inference in PyCharm 2.7
PyCharm’s debugger is now capable of collecting type information at runtime and using this information for improving code completion suggestions, code insight and quick documentation popups. This needs to be enabled in the debugger settings.
You debug your code all the time, and now in the course of debugging you can also collect type information. Afterwards, it will be used in auto-completion, and you will be able to put the collected type information into docstrings as well. Please note that enabling this feature may affect debugger performance, so use it with care.
For example, let’s consider the following code snippet:
First, make sure that “Collect run-time types information for code insight” is enabled as shown above. Then run the debugger on your code. It doesn’t matter if you use breakpoints or other debugger tools. After the debugger steps into the “foo” method, it will record the types used in the call.
As you develop your code further, you will get suggestions based on this runtime experience. Please note that this will work even during the same debugger session, so you can get suggestions right after the debugger enters any function.
Dynamically collected type information is in use not only inside a function body:
Since PyCharm also makes static type inference, it will guess that “res” will be of the type returned by “foo”:
If different calls to the same function use different argument types, PyCharm will remember all the variants of the types encountered. As always, you can use Quick Documentation Lookup to get quick information on any symbol or just method signature information. Just place the caret on a symbol and press Ctrl+Q:
As you can see, dynamically inferred types are already shown in this popup.
There is also another usage of dynamic type inference: generating docstrings based on information collected by the debugger. The basic steps are the same: enable “Collect run-time types information for code insight” and then debug your method/function. After this, place the caret anywhere within the function body and press Alt+Enter. This displays the “Generate docstrings with types” option. Select it to automatically generate a docstring for your method. For more details please see “Using Docstrings to Specify Types” in Help.
Take advantage of this new feature to better control your source code and improve your user experience with PyCharm.
Develop with pleasure!
JetBrains PyCharm Team
Subscribe to Blog updates
Thanks, we've got you!
Smart execution of R code
R plugin is announcing some helpful features to track execution of your R code: 1. Execute your R file as a runnable process, job. Jobs are shown in a separate tab in the R console. You can preview the job status (succeeded or failed), the duration of the execution, and the time you launched the …
New Database Features in PyCharm 2020.1
PyCharm 2020.1 comes with new Database export features that make exporting data easier. You can now see previews of the data that you want to export, letting you have a quick glance of what the data will look like. Furthermore, as many of our users prefer to export to Excel files, we've also adde…
R 4.0.0 and better UX in the R Plugin
Although it has not been a while since our last update release, we’re ready to announce some important enhancements and new features: Long-awaited support of R 4.0.0 . Download it and try with the R plugin for PyCharm. In response to your feedback, we improved user interface and workflows for…
New VCS Features in PyCharm 2020.1
The Version Control System support in PyCharm has been revamped with a new and improved commit workflow as well as an interactive rebasing option. This means, that it gives you more control over the way you commit your changes. New Commit Tool Window There are a couple of changes that are notewo…