Type hinting for ArcPy has been a frequently requested feature for IDEs outside ArcGIS Pro such as Visual Studio Code and PyCharm. It is now available as of ArcGIS Pro 3.3.
Type hinting is a popular Python 3.5+ feature that makes code easier to write. Type hints indicate the types of variables, function parameters, and return values.
To get the benefits of this functionality in Visual Studio Code, you must have the Python extension installed. For both Visual Studio Code and PyCharm, you must set the IDE’s interpreter to a Python environment containing arcpy
such as arcgispro-py3 or a clone.
ArcPy Data Access Module
The largest area where type hinting was added with this release is for the Data Access module known as arcpy.da
in Python. Previously, da
would not come up as a suggestion when typing arcpy.
but now it does as well as all of arcpy.da
‘s functions and classes presented with their arguments and doc strings.
Geoprocessing Tools
In ArcGIS Pro 3.2 you could see type hints for function parameters when the function was typed in the form of arcpy.toolName_toolBox
, but in ArcGIS Pro 3.3 type hints were added for using functions in the format arcpy.toolBox.toolName
for most tools.
Functions with string literals
Some functions in arcpy
have a defined list of strings for the parameter, called string literals. These string literals will now be displayed through type hinting as options you can choose from. This will save you time from having to refer back to the documentation to find the exact case you needed. For example, arcpy.ListDatasets
has a parameter called feature_type
that has a set list of strings to represent the feature type of the data you want to list with the function.
ArcPy Dictionary Objects
Similar to string literals, some arcpy
dictionary objects will now display the list of dictionary keys for you when you type the left square bracket. You will no longer have to refer to the documentation or guess at which keys are available on objects such as arcpy.GetInstallInfo()
.
Geoprocessing Result Objects
Running most geoprocessing tools with Python will return a Result object. Previously, you would need to refer to the documentation to find the particular properties and methods on the result object, but now with type hints, you can see them as you type your code.
Some geoprocessing tools return another type of object such as a raster instead of the Result object. These do not yet have type hints enabled.
Summary
All of these type hints and more were added to ArcPy API at ArcGIS Pro 3.3 and can help you improve your code. You can write code to use a tool and fill out its parameters without having to refer back to the documentation.
The effort started at 3.3 was limited but will expand to more of the ArcPy API over the next few releases.
Article Discussion: