hasattr(layer, "dataSource") gives error

482
4
Jump to solution
06-23-2021 04:26 AM
NABAZGHARIBMOHAMMED
New Contributor III

Hi,

I am trying to iterate over all layers in a map to see the datasources, and as it is recommended it is good to see

 

 

if hasattr(layer, "dataSource"):
     pass

 

 

if the layer has attribute dataSource, but the problem is on basemap, it givers an error which is:

 

 

NameError: The attribute 'dataSource' is not supported on this instance of Layer.

 

 

 I do not understand this errors, because it should skip the basemap because it does not have dataSource.

 

Is anyone has any explanation for this issue?

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

I assume you are talking about Pro and not ArcMap here.  If so, the explanation is, it's a defect.  I logged a defect over a year ago for likely what is the same issue you are dealing with, but they have set it low priority, so I am not expecting them to address it any time soon.

#BUG-000128367 Using the Python method hasattr() produces different error messages when used in ArcMap (10.7.1) versus ArcGIS Pro (2.5 Beta)

View solution in original post

4 Replies
VictorTey
Esri Contributor

Hi, how is your layer derived? Can you do a describe on the layer then use hasattr?

0 Kudos
NABAZGHARIBMOHAMMED
New Contributor III

I tried to use Describe, but it says 

 

data_source_list=[]
'''
I have several maps, I want to iterate over all layers.
'''
for map_ in this_project.listMaps(): 
    for layer in map_.listLayers():
        desc=arcpy.Describe(layer)
        if hasattr(desc, "dataSource"):
            if not desc.dataSource in data_source_list:
                pass
                '''data_spurce_list.append(desc)'''

'''out put'''
RuntimeError: Object: Describe input value is not valid type

 

It looks like describe does not accept layer. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I assume you are talking about Pro and not ArcMap here.  If so, the explanation is, it's a defect.  I logged a defect over a year ago for likely what is the same issue you are dealing with, but they have set it low priority, so I am not expecting them to address it any time soon.

#BUG-000128367 Using the Python method hasattr() produces different error messages when used in ArcMap (10.7.1) versus ArcGIS Pro (2.5 Beta)

View solution in original post

NABAZGHARIBMOHAMMED
New Contributor III

Yes I got the error with ArcPro version 2.8.0.

I agree with you, I think it is a bug with the function.

0 Kudos