Select to view content in your preferred language

TypeError: 'geoprocessing messages object' object is not subscriptable

362
8
Jump to solution
07-03-2024 06:09 AM
Glasnoct
New Contributor III
def getParameterInfo(self) -> list:
        """Define the tool parameters."""
        project_id = \
            arcpy.Parameter(
                name="project ID",
                displayName="project ID",
                datatype="GPString",
                parameterType="Required",  # Required|Optional|Derived
                direction="Input",  # Input|Output
        )
        project_id.filter.type = "ValueList"
        project_id.filter.list = self.project_ids
        return [project_id]
project_id = parameters[0].value
                 ~~~~~~~~~~^^^
TypeError: 'geoprocessing messages object' object is not subscriptable

GetParameterInfo is returning [project_id] so I'm not sure why I can't subscript it . 'Parameters' without an index doesn't work either, so I'm not sure what the value method is expecting. This is pretty typical python tool code. Why is it a messages object to begin with? Is that just how the parameters function spits out the parameters?

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

I'd try changing "messages=None" to just "messages" as per all the official arcpy examples. Shouldn't make a difference, but ArcGIS does do some weird things with python toolboxes.

And make sure you are running it as a tool (from the toolbox or in code via arcpy.ImportToolbox) and not treating it as a regular python class and trying to instantiate it yourself.

I also see from your other post you were using an "@staticmethod" decorator for the execute method. Again, probably better to stick to the syntax shown by Esri in the examples (undecorated methods).

View solution in original post

8 Replies
Luke_Pinner
MVP Regular Contributor

Can you include more of the code where you call "project_id = parameters[0].value".

If it's in the execute method, you may have your arguments in the wrong order and are accessing the messages argument instead of the parameters argument.

Glasnoct
New Contributor III
# db.project_ids is just a list of strings

    def getParameterInfo(self) -> list:
        """Define the tool parameters."""
        project_id = \
            arcpy.Parameter(
                name="project ID",
                displayName="project ID",
                datatype="GPString",
                parameterType="Required",  # Required|Optional|Derived
                direction="Input",  # Input|Output
        )
        project_ids = db.project_ids
        project_id.filter.type = "ValueList"
        project_id.filter.list = project_ids
        return [project_id]
0 Kudos
Luke_Pinner
MVP Regular Contributor

You've already shown how you define the "project_id" parameter.  That wasn't what I asked to see.  Please include the code where you use/access that parameter.

0 Kudos
Glasnoct
New Contributor III

 

    def execute(self, parameters, messages=None):
        """The source code of the tool."""
        project_id = parameters[0].value
        all_project_features_to_new_filtered_lyrs(project_id)
        return

 

Right, Monday mornings am I right? The access is in the error message in the original post. The full function isn't much longer. 

This is more or less modeled directly off the suggested code I received in my last thread about parameter issues. https://community.esri.com/t5/python-questions/struggling-with-python-toolbox-tools-and-parameter/m-...

0 Kudos
Luke_Pinner
MVP Regular Contributor

I'd try changing "messages=None" to just "messages" as per all the official arcpy examples. Shouldn't make a difference, but ArcGIS does do some weird things with python toolboxes.

And make sure you are running it as a tool (from the toolbox or in code via arcpy.ImportToolbox) and not treating it as a regular python class and trying to instantiate it yourself.

I also see from your other post you were using an "@staticmethod" decorator for the execute method. Again, probably better to stick to the syntax shown by Esri in the examples (undecorated methods).

Glasnoct
New Contributor III

It was the @staticmethod decorator. It's been too long to remember why it was there; might've been an artifact from the github example I originally was imitating. Messages=None caused no issues.

0 Kudos
Luke_Pinner
MVP Regular Contributor

For next time, I suggest you include all relevant code in your post to start with. You're more likely to get an answer at all/quicker.

0 Kudos
Luke_Pinner
MVP Regular Contributor

Yes the "@staticmethod" decorator is definitely the problem.

The reason you got a TypeError: 'geoprocessing messages object' object is not subscriptable exception is that staticmethods don't get passed a reference to the instance of the class (i.e. "self")

So because you put a "self" argument in the definition of static method, when the method was called by ArcGIS, that first "self" argument got the parameters list, the 2nd "parameters" argument got the messages object and the "method" argument didn't get passed anything at all.  And because you had "messages" default to "None", you didn't get a more obvious error earlier - something like TypeError: Tool.execute() missing 1 required positional argument: 'messages'

Here's a simple tutorial:

Tutorials Teacher - staticmethod decorator 

 

0 Kudos