Skip to content

Commit

Permalink
pg.Object: Fix an issue that overridden property is not correctly i…
Browse files Browse the repository at this point in the history
…nherited by subclasses.

Example:

```
import pyglove as pg

class A(pg.Object):
  x: int = 1

class B(A):
  @Property
  def x(self) -> int:
    return 2

class C(B):
  pass
```

Previously `C.x` will be 1 (the value of symbolic field x), after this change C.x will be 2 (the value of overriden property x inherited from B).

PiperOrigin-RevId: 615494844
  • Loading branch information
daiyip authored and pyglove authors committed Mar 13, 2024
1 parent cc456b1 commit aad25de
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyglove/core/symbolic/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def _generate_sym_attributes(cls):
for key, field in cls.__schema__.fields.items():
if isinstance(key, pg_typing.ConstStrKey):
attr_name = str(key)
attr_value = cls.__dict__.get(attr_name, pg_typing.MISSING_VALUE)
attr_value = getattr(cls, attr_name, pg_typing.MISSING_VALUE)
if attr_value == pg_typing.MISSING_VALUE or (
not inspect.isfunction(attr_value)
and not isinstance(attr_value, property)
Expand Down
8 changes: 8 additions & 0 deletions pyglove/core/symbolic/object_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ def z(self):
self.assertEqual(a.z(), 5)
self.assertEqual(a.sym_init_args.z, 3)

# Make sure overridden property is inherited by subclass correctly.
class B(A):
pass

b = B(1, 2, 3)
self.assertEqual(b.x, 2)
self.assertEqual(b.sym_init_args.x, 1)

def test_runtime_type_check(self):

@pg_members([
Expand Down

0 comments on commit aad25de

Please sign in to comment.