Migration of code from 2.x to 3.x

03-19-2021 11:42 AM
Occasional Contributor II

In trying to migrate from 2.x to 3.x, I am trying to update a segment of code and my troubleshoot has ran its limits with Google. I am trying to remove domains from fields in various feature classes below. My error, in particular, comes from the image below where the error I get is the 'NoneType' object is not callable. 


Full Code:

		# Comments
		arcpy.AddMessage("Removing All Domains..." +'\n')
		txtFile.write("Removing All Domains..." +'\n')

		#Set workspace environment to geodatabase
		arcpy.env.workspace = "D:\\WorkSpace\\WebReporter\\Workspace.gdb"
		myGDB = "D:\\WorkSpace\\WebReporter\\Workspace.gdb"

		#Get list of feature classes in geodatabase
		FCs = arcpy.ListFeatureClasses()

		#Loop through feature classes in list
		for FC in FCs:

			#List fields in feature class
			fields = arcpy.ListFields(FC)

			#Loop through fields
			for field in fields:

				#Check if field has domain
				if (field.domain != "" and field.domain != 'EnabledDomain' and field.domain != 'AnnotationStatus'):

					#Print feature class, field, domain name
					print (FC, field.name, field.domain)
					arcpy.RemoveDomainFromField_management(FC, field.name)

				# Get domains that are assigned to a field
		domains_used = []
		for dirpath, dirnames, filenames in arcpy.da.Walk(myGDB, datatype=["FeatureClass", "Table"]):
			for filename in filenames:
				print ("Checking {}".format(os.path.join(dirpath, filename)))
				## Check for normal field domains
				for field in arcpy.ListFields(os.path.join(dirpath, filename)):
					if field.domain:
						## Check for domains used in a subtype field
								# Comments
				arcpy.AddMessage("I got to here..." +'\n')
				txtFile.write("I got to here..." +'\n')
				subtypes = arcpy.da.ListSubtypes(os.path.join(dirpath, filename))
				for stcode, stdict in subtypes.items():
					if stdict.get('keys')()()("SubtypeField") != u'':
						for field, fieldvals in stdict.get('keys')("FieldValues").items():
							if not fieldvals[1] is None:

							# Get domains that exist in the geodatabase
		domains_existing = [dom.name for dom in arcpy.da.ListDomains(myGDB)]

		# Find existing domains that are not assigned to a field  
		domains_unused = set(domains_existing) ^ set(domains_used)
		print ("{} unused domains in {}".format(len(domains_unused), myGDB))
		for domain in domains_unused:
			arcpy.DeleteDomain_management(myGDB, domain)
			print ("{} deleted".format(domain)  )

		# Comments
		arcpy.AddMessage("Removing All Domains Complete..." +'\n')
		txtFile.write("Removing All Domains Complete..." +'\n')


0 Kudos
1 Reply
by MVP Regular Contributor
MVP Regular Contributor

If you set a breakpoint on the line above (or a few lines above and step over to check your values) and debug, what values get assigned to the variables field and fieldvals?  That should tell you if fieldvals[1] is null or not and possibly give you a clue where something may be broken.

0 Kudos