Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning typed arrays created using the Array constructor fails with a typing error at runtime for inner classes #100096

Closed
phkeese opened this issue Dec 6, 2024 · 2 comments

Comments

@phkeese
Copy link

phkeese commented Dec 6, 2024

Tested versions

  • Reproducible in v4.3.stable.official [77dcf97]
    No other versions were tested.

System information

macOS Sonoma 14.4, M3 Processor

Issue description

When returning typed arrays for inner classes from a function by way of the Array Array(base: Array, type: int, class_name: StringName, script: Variant) constructor, a runtime error occurs. The example only contains the case for inner classes, but in my testing it also seemed to fail when trying to return an array of typed nodes (class_name at start of file).

I came across this behaviour when I tried filtering child nodes (get_children()) and returning only those of a specific class.
The result of the filtering step is an untyped array I tried turning into a typed array using the above constructor.

Example Code:

class_name MyNode
extends Node


class Data:
	var value
	pass


func _ready() -> void:
	var typed_array := create_array()


func create_array() -> Array[Data]:
	var untyped_array := [Data.new()]
	var data := untyped_array[0] as Data
	# Fails with error:
	# Trying to return an array of type "Array[RefCounted]" where expected return type is "Array[Data]".
	#return Array(untyped_array, TYPE_OBJECT, StringName(data.get_class()), null)
	
	# Even weirder: 
	# Trying to return an array of type "Array[Data]" where expected return type is "Array[Data]".
	#return Array(untyped_array, TYPE_OBJECT, "Data", null)
	
	# Also fails:
	# Trying to return an array of type "Array[MyNode.Data]" where expected return type is "Array[Data]".
	#return Array(untyped_array, TYPE_OBJECT, "MyNode.Data", null)
	
	var typed_array : Array[Data] = []
	for val in untyped_array:
		typed_array.push_back(val)
		
	# This prints "RefCounted" which is weird because type hints actually work and suggest `value`
	# when accessing an element of this array.
	print(typed_array.get_typed_class_name())
	return typed_array

Steps to reproduce

  1. Write a typed function returning a typed array
  2. Try to use the typed array constructor with an inner class or a node class (from class_name)
  3. Run project, fail when the function tries to return

Minimal reproduction project (MRP)

array_type_error.zip

@AThousandShips
Copy link
Member

AThousandShips commented Dec 6, 2024

Class names of custom types are not returned as such, see:

In all the construction cases you are missing the script argument, I think this is necessary for this, so try:

return Array(untyped_array, TYPE_OBJECT, "RefCounted", Data)

@phkeese
Copy link
Author

phkeese commented Dec 6, 2024

Oops, it is even right there in the documentation for the constructor.
User error 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants