|
| 1 | +import dataclasses |
1 | 2 | import inspect |
2 | 3 | import re |
3 | 4 |
|
4 | 5 | # Get the comment for a specific field. |
5 | | -from typing import Callable, Type |
| 6 | +from typing import Callable, ClassVar, Type, get_origin, get_type_hints |
6 | 7 |
|
7 | 8 | import reflex as rx |
8 | 9 | from pcweb.templates.docpage import h1_comp, h2_comp |
@@ -54,9 +55,11 @@ def get_class_fields(self) -> list[dict]: |
54 | 55 | return out |
55 | 56 |
|
56 | 57 | def get_fields(self) -> list[dict]: |
57 | | - if not issubclass(self.module, rx.Base): |
58 | | - return [] |
59 | | - return self.get_annotations(self.module.__fields__) |
| 58 | + if dataclasses.is_dataclass(self.module): |
| 59 | + return self.get_annotations({f.name: f for f in dataclasses.fields(self.module)}) |
| 60 | + elif isinstance(self.module, type) and issubclass(self.module, rx.Base): |
| 61 | + return self.get_annotations(self.module.__fields__) |
| 62 | + return [] |
60 | 63 |
|
61 | 64 | def get_methods(self): |
62 | 65 | return [ |
@@ -170,8 +173,16 @@ def get_annotations(self, props) -> list[dict]: |
170 | 173 |
|
171 | 174 |
|
172 | 175 | def format_field(field): |
173 | | - type_ = field["prop"].type_ |
| 176 | + prop = field["prop"] |
| 177 | + try: |
| 178 | + type_ = prop.type_ |
| 179 | + except AttributeError: |
| 180 | + type_ = prop.type |
174 | 181 | default = field["prop"].default |
| 182 | + if default is dataclasses.MISSING: |
| 183 | + default = None |
| 184 | + else: |
| 185 | + default = repr(default) |
175 | 186 | type_str = type_.__name__ if hasattr(type_, "__name__") else str(type_) |
176 | 187 | if default: |
177 | 188 | type_str += f" = {default}" |
@@ -217,8 +228,10 @@ def format_fields(headers, fields): |
217 | 228 | ) |
218 | 229 |
|
219 | 230 |
|
220 | | -def generate_docs(title: str, s: Source): |
| 231 | +def generate_docs(title: str, s: Source, extra_fields: list[dict] | None = None): |
221 | 232 | fields = s.get_fields() |
| 233 | + if extra_fields: |
| 234 | + fields.extend(extra_fields) |
222 | 235 | class_fields = s.get_class_fields() |
223 | 236 | return rx.box( |
224 | 237 | h1_comp(text=title.title()), |
|
0 commit comments