Select to view content in your preferred language

Better CIM Type Hinting

263
0
11-20-2024 09:10 AM
Status: Open
Labels (1)
HaydenWelch
MVP Regular Contributor

The CIM module is incredibly powerful, but navigating the objects in it is a pain whenever you go more than one level deep. To fix this you can pretty simply just add the stored object type string as a hint in all files. The result is a developer experience like this:

HaydenWelch_0-1732122145552.png

As opposed to one where objects not immediately in the base object attributes are typed as strings:

HaydenWelch_1-1732122323192.png

This allows me to use linking to CTRL+click into an object to see what attributes it has as well as start typing attribute names and getting autocompletion. With names as complex as the ones for CIM and with how dangerous CIM can be, I think making this process as painless and foolproof as possible will prevent people from making terrible mistakes.

Ideally this would be extended to include typed sequences (e.g.  CIMFeatureLayer.featureTable.fieldDescriptions would be typed as list[CIMFieldDescription]  instead of list[Any]), I have implemented the easy version of this and will be willing to share the repo with someone on the ESRI development team if they want to see, but because of the licensing I don't feel comfortable sharing it publicly.

 

UPDATE:

I am currently working on another re-factor of the CIM module that switches from regular classes to dataclasses. This will allow developers to immediately see the attributes and defaults for each CIM class in their IDE along with their expected type. This change shouldn't effect how the CIM class objects are parsed by arcpy as they still have the required attributes and a vars() or __dict__ call will return the same values. This will only allow for static type checkers and tools like Pylance to immediately check your work without putting your project at risk of a bad CIM write.

Tags (2)