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

Enforce Signal parameter types in Connect #71952

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jordi-star
Copy link
Contributor

@jordi-star jordi-star commented Jan 24, 2023

Beginning work for Typed signals.
Adds get_method_info() to Callable (not exposed to Godot) for retrieving Callable arg info.

Object's connect method now checks if the correct amount of non-initialized arguments exist in the callable, and if the types are equal (todo: Check if types are compatible instead). Currently only implemented in connect

Task list:

  • Check if types are compatible instead of doing explicit ==

  • Add checks in the analyzer to error ahead of time when connect uses a non-compatible Callable

  • Rewrite messages for readability

  • Clean-up code

  • Write tests

Example

extends Node2D

signal typed(arg1:bool, arg2:int);

func incorrect_arg_amount(arg1:Vector2):
	pass

func incorrect_arg_types(arg1:Node, arg2:String):
	pass

func _ready():
	push_warning("Incorrect arg amount");
	typed.connect(incorrect_arg_amount);
	push_warning("Incorrect arg types");
	typed.connect(incorrect_arg_types);
	
	push_warning("Lambdas are also checked, of course.");
	typed.connect(func(arg1:String):
		pass
	);

image

Bugsquad edit: This closes godotengine/godot-proposals#2557 and closes #54333.

@Calinou Calinou added this to the 4.x milestone Jan 24, 2023
@jordi-star jordi-star force-pushed the typed_sig branch 2 times, most recently from 45893a2 to a7349f9 Compare January 24, 2023 18:11
@jordi-star jordi-star changed the title Enforce Signal parameter types in Connect & Emit Enforce Signal parameter types in Connect Jan 24, 2023
@jordi-star
Copy link
Contributor Author

jordi-star commented Jan 24, 2023

Parameter types do not need to be enforced in emit because all connections are forced to have the correct arguments anyways. Callable already implements an error when the wrong arguments are provided when called.

@jordi-star jordi-star force-pushed the typed_sig branch 3 times, most recently from 59c15df to 4563e7b Compare January 26, 2023 23:11
@jordi-star
Copy link
Contributor Author

Analyzer now checks if the provided callable contains the correct arguments for the provided signal. I'm gonna go over the code I have right now, see if any changes are needed, then write tests.

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

Successfully merging this pull request may close these issues.

GDScript 2.0: Static typing on signals isn't enforced Add type hints for signal parameters
2 participants