Skip to content

Commit 4dcd33f

Browse files
daiyippyglove authors
authored andcommitted
Enable dataclasses_transform (PEP 681) on pg.ObjectMeta for better static typing support.
PiperOrigin-RevId: 791418828
1 parent c080ff4 commit 4dcd33f

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

pyglove/core/symbolic/contextual_object.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class A(pg.ContextualObject):
4646
"""
4747

4848
import threading
49-
from typing import Annotated, Any, ContextManager, Dict, Optional, Type
49+
from typing import Annotated, Any, ClassVar, ContextManager, Dict, Optional, Type
5050
from pyglove.core import utils as pg_utils
5151
from pyglove.core.symbolic import base
5252
from pyglove.core.symbolic import inferred as pg_inferred
@@ -109,13 +109,13 @@ def foo(a):
109109
"""
110110

111111
# Override __repr__ format to use inferred values when available.
112-
__repr_format_kwargs__ = dict(
112+
__repr_format_kwargs__: ClassVar[Dict[str, Any]] = dict(
113113
compact=True,
114114
use_inferred=True,
115115
)
116116

117117
# Override __str__ format to use inferred values when available.
118-
__str_format_kwargs__ = dict(
118+
__str_format_kwargs__: ClassVar[Dict[str, Any]] = dict(
119119
compact=False,
120120
verbose=False,
121121
use_inferred=True,

pyglove/core/symbolic/object.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
from pyglove.core.symbolic import flags
2929

3030

31+
if sys.version_info >= (3, 11):
32+
_dataclass_transform = typing.dataclass_transform
33+
else:
34+
def _dataclass_transform():
35+
return lambda cls: cls
36+
37+
38+
@_dataclass_transform()
3139
class ObjectMeta(abc.ABCMeta):
3240
"""Meta class for pg.Object."""
3341

@@ -153,6 +161,8 @@ def _infer_fields_from_annotations(cls) -> List[pg_typing.Field]:
153161
if attr_name == '__kwargs__':
154162
# __kwargs__ is speical annotation for enabling keyword arguments.
155163
key = pg_typing.StrKey()
164+
if typing.get_origin(attr_annotation) is typing.ClassVar:
165+
attr_annotation = typing.get_args(attr_annotation)[0]
156166
elif not attr_name.isupper() and not attr_name.startswith('_'):
157167
key = pg_typing.ConstStrKey(attr_name)
158168
else:

pyglove/core/utils/formatting.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import enum
1818
import io
1919
import sys
20-
from typing import Any, Callable, ContextManager, Dict, List, Optional, Sequence, Set, Tuple
20+
from typing import Any, Callable, ClassVar, ContextManager, Dict, List, Optional, Sequence, Set, Tuple
2121
from pyglove.core.utils import thread_local
2222

2323

@@ -55,10 +55,12 @@ class Formattable(metaclass=abc.ABCMeta):
5555
"""
5656

5757
# Additional format keyword arguments for `__str__`.
58-
__str_format_kwargs__ = dict(compact=False, verbose=True)
58+
__str_format_kwargs__: ClassVar[Dict[str, Any]] = dict(
59+
compact=False, verbose=True
60+
)
5961

6062
# Additional format keyword arguments for `__repr__`.
61-
__repr_format_kwargs__ = dict(compact=True)
63+
__repr_format_kwargs__: ClassVar[Dict[str, Any]] = dict(compact=True)
6264

6365
@abc.abstractmethod
6466
def format(self,

0 commit comments

Comments
 (0)