godot: Incorrect type checking

Godot version

3.5

System information

Windows 10

Issue description

According to my understanding, the point of specifying variable types is to allow compiler-time checks even though Godot does not have static typing at run-time. However, when dealing with class hierarchies, Godot does not handle typed variables as expected. If we declare a variable to be an instance of class C, Godot compiler will allow the access to properties and functions that are not defined in given class C, but in its subclasses, which is incorrect.

It has to be fixed so that this code does not compile:

class_name Parent 
func parentFunction(): 
    print("parent")
...
class_name Child
extends Parent
func childFunction():
    print("child")
...

var a: Parent = Child.new()
a.childFunction()            # this should not compile, because class Parent does not have childFunction

Steps to reproduce

  1. define a class Parent
  2. define a class Child, extending Parent
  3. define a function m in class Child, such that it does not exist in Parent
  4. define variable a of type Parent
  5. initialize variable a with an instance of class Child
  6. try to call function m, which exists in Child but not in Parent
  7. this code should not compile, because it tries to call a function which is not defined in the type of variable a

Minimal reproduction project

project.zip

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (12 by maintainers)

Most upvoted comments

Static typing is optional in GDScript. In particular, the following code is allowed by default, although it is not type-safe:

var my_var: MyClass = dict.key

If you want strict checks, I wrote above how to enable them. See also this comment.

specify (restrict) type of a variable

That is not what it does.

I don’t want to be allowed to call methods and access properties that don’t belong to that type

GDScript is not statically typed. And it is interpreted. The type hints exist to allow the Editor to provide you with useful information. There is no way to prevent you from writing code that ignores the hints. You can save a script with a type error and run it just fine so long as you don’t run into an actual execution error.

If you think of GDScript as being in the same class of language as JavaScript, your expectations may be more in line with how things work. Or TypeScript since you are using the optional type hints.

Point is. It will execute without error. So the editor, is not giving you an error as it stands right now. I am plainly explaining why things work how they do, and what frame of reference allows for this to be acceptable.

It probably should error. I will reiterate.

And I will reiterate. That we (you) should investigate under which circumstances it is not providing an error, even though we believe that it should.

You can enable warnings:

Additionally, you can treat all warnings as errors:

In 4.0-dev you can set this up on a per-type basis: