Array with dictionaries doesn't get recognized as Array[Dictionary]

Godot Version

4.2.2

Question

I’m trying to make my code more explicitly typed when possible. This part deals with HTTP requests which return data in JSON format, but at the end of the day the minimal reproduction boils down to this test function:

func test_request() -> Array[Dictionary]:
 	var test_response : Array = JSON.parse_string(
 		'[{"key1": 1, "key2": 2},{"key1": 3, "key2": 4},{"key1": 5, "key2": 6}]'
 		)
 	#Logger.info(test_response)
 	return test_response

As you can see, stack trace sees the response as an array of dictionaries, but the function only recognizes it as an untyped array. I also have to mention that typing var test_response as Array isn’t the cause: if I type it as Array[Dictionary] the same error just happens earlier (I only changed that so that the value would show in stack trace); if I leave it untyped, it still ascribes the “Array” type to itself.

So, what’s the deal with the way the JSON parser works - do I have to just accept that and leave untyped arrays, or is there a way to make the function recognize those dictionaries?

Just return Array for now, dont nest anything inside

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.