Select to view content in your preferred language

Better type hinting for arcpy.env

972
4
07-23-2024 01:04 PM
Status: Implemented
HaydenWelch
MVP Regular Contributor

I pretty frequently find myself using tons of magic strings and flip flopping of environment variables in my scripts.

I have implemented something that does this as best as I can here  as an example solution. It's essentially a carbon copy of the EnvManager class but with explicitly defined environments so a static type checker can help you make sure you're feeding correct values and parameters to the GPEnvironment object.

This could also be implemented in a .pyi file for EnvManager.py to allow for forwards compatibility with changes to env parameters and type hinting updates could lag behind without causing issues.

HaydenWelch_0-1721764967861.png

 

4 Comments
JoshuaBixby

ArcPy already has a full-blown context manager for managing geoprocessing environments:  EnvManager—ArcGIS Pro | Documentation

HaydenWelch

@JoshuaBixby 

 

Well, I'm dumb. At least this is still useful for the type hinting and auto completion/literals lol.

 

Updated the title to reflect that.

HannesZiegler
Status changed to: In Product Plan

We're working on this!  This status does not guarantee that the functionality will be in the next release, but development work has begun. Release cycles vary by product so make sure to check the product life cycle information to get an idea of when to expect the next release.  

HannesZiegler
Status changed to: Implemented

This idea has been implemented in ArcGIS Pro 3.4, please see the Python section of the What's New in ArcGIS Pro 3.4 topic and scroll to ArcPy.

  • Improvements were made to type hints in arcpy and the arcpy.da and arcpy.charts modules. Type hints were added to ArcPy classes and geoprocessing tools including Result and Raster objects. Type hints provide an improved code authoring experience in modern integrated development environments (IDEs).

We have been gradually adding type hints to ArcPy, including EnvManager, and continue to progress these.