Skip to content

Commit 34dfc3b

Browse files
committed
Fix builtins raising TypeError when compared against arbitrary type
1 parent 85841a1 commit 34dfc3b

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/godot/builtins_pyx/operator.pyx.j2

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,23 @@ def __repr__(self):
2323
gdapi.gd_string_del(&gdstr)
2424
{% endif %}
2525

26-
def __eq__(self, {{ builtin.cy_type }} other):
26+
def __eq__(self, object other):
27+
cdef {{ builtin.c_type }}* other_gd_data
28+
try:
29+
other_gd_data = &(<{{ builtin.cy_type }}?>other)._gd_data
30+
except TypeError:
31+
return False
2732
{% if builtin.is_transparent_builtin %}
2833
return (
2934
{% for field_path in builtin.all_nested_scalar_members %}
3035
{% if not loop.first%}
3136
and
3237
{% endif %}
33-
self._gd_data.{{ field_path }} == other._gd_data.{{ field_path }}
38+
self._gd_data.{{ field_path }} == other_gd_data.{{ field_path }}
3439
{% endfor %}
3540
)
3641
{% else %}
37-
return gdapi.gd_{{ builtin.snake_name }}_op_equal_{{ builtin.snake_name }}(&self._gd_data, &other._gd_data)
42+
return gdapi.gd_{{ builtin.snake_name }}_op_equal_{{ builtin.snake_name }}(&self._gd_data, other_gd_data)
3843
{% endif %}
3944

4045
{% if builtin.original_name in ("Array", "Dictionary") or builtin.is_packed_array %}

tests/4-use-godot-from-python/tests/test_builtins.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ def test_scalars_and_nil_not_exposed(type: str):
5454
"Dictionary",
5555
"Array",
5656
"PackedArray",
57+
# TODO: Callable (with conversion from a regular python function ?)
58+
# TODO: Signal
5759
],
5860
)
5961
def test_constructor(kind: str):
@@ -432,9 +434,13 @@ def test_operator(kind: str):
432434
case "equal":
433435
assert_eq(godot.GDString("foo") == godot.GDString("foo"), True)
434436
assert_eq(godot.GDString("foo") == godot.GDString("bar"), False)
437+
assert_eq(godot.GDString("foo") == None, False) # noqa: E711
438+
assert_eq(godot.GDString("foo") == 42, False)
435439

436440
assert_eq(godot.Vector2i(1, 2) == godot.Vector2i(1, 2), True)
437441
assert_eq(godot.Vector2i(1, 0) == godot.Vector2i(1, 2), False)
442+
assert_eq(godot.Vector2i(1, 0) == None, False) # noqa: E711
443+
assert_eq(godot.Vector2i(1, 0) == 42, False)
438444

439445
assert_eq(godot.GDArray() == godot.GDArray(), True)
440446
assert_eq(
@@ -443,6 +449,8 @@ def test_operator(kind: str):
443449
True,
444450
)
445451
assert_eq(godot.GDArray() == godot.GDArray((1,)), False)
452+
assert_eq(godot.GDArray() == None, False) # noqa: E711
453+
assert_eq(godot.GDArray() == 42, False)
446454

447455
assert_eq(godot.GDDictionary() == godot.GDDictionary(), True)
448456
assert_eq(
@@ -451,6 +459,8 @@ def test_operator(kind: str):
451459
True,
452460
)
453461
assert_eq(godot.GDDictionary() == godot.GDDictionary([(1, 2)]), False)
462+
assert_eq(godot.GDDictionary() == None, False) # noqa: E711
463+
assert_eq(godot.GDDictionary() == 42, False)
454464

455465
case "not_equal":
456466
assert_eq(godot.GDString("foo") != godot.GDString("bar"), True)

0 commit comments

Comments
 (0)