You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I like the new setting (from #81355) to warn for untyped declarations, but I am not liking it for for loops because it gets rid of the convenience of just having it inferred. Or at least it seems it can't be inferred for Dictionaries. Not only that, but adding @warning_ignore above the offending line of code does not make the warning go away.
So without a separate option to not warn for for loops, I'm tempted to just disable the whole thing.
Steps to reproduce
Enable warnings for untyped declarations in the project settings.
Create a script named mrp.gd with the following code:
extends Node
var alice = 5
var bob: Dictionary = {}
func _ready() -> void:
for something in bob:
pass
Observe the following warnings:
[Ignore] Line 3 (UNTYPED_DECLARATION): Variable "alice" has no static type.
[Ignore] Line 7 (UNTYPED_DECLARATION): "for" iterator variable "something" has no static type.
Click the [Ignore] link for both of the warnings to result in this code:
extends Node
@warning_ignore("untyped_declaration")
var alice = 5
var bob: Dictionary = {}
func _ready() -> void:
@warning_ignore("untyped_declaration")
for something in bob:
pass
Observe the following warning still persists:
[Ignore] Line 9 (UNTYPED_DECLARATION): "for" iterator variable "something" has no static type.
Clicking [Ignore] repeatedly just adds another copy of "untyped_declaration" to the same line each time.
While writing this up I discovered a similar issue for Arrays:
extends Node
@warning_ignore("untyped_declaration")
var alice = 5
var bob: Dictionary = {}
var carlos := []
func _ready() -> void:
@warning_ignore("untyped_declaration")
for something in bob:
pass
@warning_ignore("untyped_declaration")
for other in carlos:
pass
[Ignore]Line 10 (UNTYPED_DECLARATION):"for" iterator variable "something" has no static type.
[Ignore]Line 13 (UNTYPED_DECLARATION):"for" iterator variable "other" has no static type.
And just to be clear, the warning obviously goes away for the Array when I strongly type it and its elements, e.g., var carlos: Array[int] = [], but at that point the @warning_ignore is not needed to make it go away.
Minimal reproduction project
Shown above.
CC @ryanabx since I believe you implemented this feature, this issue may be of interest to you.
The text was updated successfully, but these errors were encountered:
I really wish there was a separate toggle for this feature in regard to for loops, as I can't picture myself ever wanting to explicitly declare the types for those variables rather than letting them just be inferred (or fallback to Variant for types that can't be strictly inferred, such as with Dictionaries). If the type can't be inferred from a Dictionary because a Dictionary can't be strongly typed then of course it's going to be Variant. So why not just automatically infer that and not nag about it?
I suppose the real solution to this problem is to add strongly typed Dictionaries so the type can be inferred in a for loop.
But if strongly typed Dictionaries or a separate option specifically for for loops are not going to happen (at least not anytime soon) then maybe I'll re-enable this feature once #83037 is merged so I can tell the compiler to ignore the warnings.
We discussed this when implementing the warning. The problem is that for loop variable is different from other variables; you cannot tell it to infer its type or not, as you can for other variables (:= vs =). The loop variable always tries to infer the hard type if possible, and leaves the weak Varuant type if this is not possible. Accordingly, a warning for a loop variable is generated in the case of a weak type.
In the case of dictionaries, I think the bug is that keys and values are not inferred as hard Variants. If we change this, you can avoid specifying the type explicitly for dictionary keys without changing how the UNTYPED_DECLARATION warning works (but it still be generated if you're iterating over a value of an unknown type).
Godot version
v4.2.beta2.official [f8818f8]
System information
Windows 10
Issue description
I like the new setting (from #81355) to warn for untyped declarations, but I am not liking it for
for
loops because it gets rid of the convenience of just having it inferred. Or at least it seems it can't be inferred for Dictionaries. Not only that, but adding@warning_ignore
above the offending line of code does not make the warning go away.So without a separate option to not warn for
for
loops, I'm tempted to just disable the whole thing.Steps to reproduce
mrp.gd
with the following code:[Ignore]
link for both of the warnings to result in this code:[Ignore]
repeatedly just adds another copy of"untyped_declaration"
to the same line each time.While writing this up I discovered a similar issue for Arrays:
And just to be clear, the warning obviously goes away for the Array when I strongly type it and its elements, e.g.,
var carlos: Array[int] = []
, but at that point the@warning_ignore
is not needed to make it go away.Minimal reproduction project
Shown above.
CC @ryanabx since I believe you implemented this feature, this issue may be of interest to you.
The text was updated successfully, but these errors were encountered: