reflex-docgen 0.9.0a1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- reflex_docgen-0.9.0a1/.gitignore +24 -0
- reflex_docgen-0.9.0a1/LICENSE +201 -0
- reflex_docgen-0.9.0a1/PKG-INFO +19 -0
- reflex_docgen-0.9.0a1/README.md +3 -0
- reflex_docgen-0.9.0a1/pyproject.toml +30 -0
- reflex_docgen-0.9.0a1/src/reflex_docgen/__init__.py +33 -0
- reflex_docgen-0.9.0a1/src/reflex_docgen/_class.py +345 -0
- reflex_docgen-0.9.0a1/src/reflex_docgen/_component.py +151 -0
- reflex_docgen-0.9.0a1/src/reflex_docgen/markdown/__init__.py +57 -0
- reflex_docgen-0.9.0a1/src/reflex_docgen/markdown/_parser.py +403 -0
- reflex_docgen-0.9.0a1/src/reflex_docgen/markdown/_types.py +326 -0
- reflex_docgen-0.9.0a1/src/reflex_docgen/markdown/transformer/__init__.py +13 -0
- reflex_docgen-0.9.0a1/src/reflex_docgen/markdown/transformer/_base.py +414 -0
- reflex_docgen-0.9.0a1/src/reflex_docgen/markdown/transformer/_markdown.py +189 -0
- reflex_docgen-0.9.0a1/src/reflex_docgen/py.typed +0 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
**/.DS_Store
|
|
2
|
+
**/*.pyc
|
|
3
|
+
assets/external/*
|
|
4
|
+
dist/*
|
|
5
|
+
examples/
|
|
6
|
+
.web
|
|
7
|
+
.states
|
|
8
|
+
.idea
|
|
9
|
+
.vscode
|
|
10
|
+
.coverage
|
|
11
|
+
.coverage.*
|
|
12
|
+
.venv
|
|
13
|
+
venv
|
|
14
|
+
requirements.txt
|
|
15
|
+
.pyi_generator_last_run
|
|
16
|
+
.pyi_generator_diff
|
|
17
|
+
reflex.db
|
|
18
|
+
.codspeed
|
|
19
|
+
.env
|
|
20
|
+
.env.*
|
|
21
|
+
node_modules
|
|
22
|
+
package-lock.json
|
|
23
|
+
*.pyi
|
|
24
|
+
.pre-commit-config.yaml
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2023, Pynecone, Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: reflex-docgen
|
|
3
|
+
Version: 0.9.0a1
|
|
4
|
+
Summary: Generate documentation for Reflex components.
|
|
5
|
+
Author-email: Khaleel Al-Adhami <khaleel@reflex.dev>
|
|
6
|
+
Maintainer-email: Khaleel Al-Adhami <khaleel@reflex.dev>
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Requires-Dist: griffelib>=2.0.1
|
|
10
|
+
Requires-Dist: mistletoe>=1.4.0
|
|
11
|
+
Requires-Dist: pyyaml>=6.0
|
|
12
|
+
Requires-Dist: reflex
|
|
13
|
+
Requires-Dist: typing-extensions
|
|
14
|
+
Requires-Dist: typing-inspection>=0.4.2
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# reflex-docgen
|
|
18
|
+
|
|
19
|
+
Generate documentation for Reflex components.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "reflex-docgen"
|
|
3
|
+
dynamic = ["version"]
|
|
4
|
+
description = "Generate documentation for Reflex components."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [{ name = "Khaleel Al-Adhami", email = "khaleel@reflex.dev" }]
|
|
7
|
+
maintainers = [{ name = "Khaleel Al-Adhami", email = "khaleel@reflex.dev" }]
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
|
+
dependencies = [
|
|
10
|
+
"griffelib>=2.0.1",
|
|
11
|
+
"mistletoe>=1.4.0",
|
|
12
|
+
"pyyaml>=6.0",
|
|
13
|
+
"reflex",
|
|
14
|
+
"typing-extensions",
|
|
15
|
+
"typing-inspection>=0.4.2",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[tool.uv.sources]
|
|
19
|
+
reflex = { workspace = true }
|
|
20
|
+
|
|
21
|
+
[tool.hatch.version]
|
|
22
|
+
source = "uv-dynamic-versioning"
|
|
23
|
+
|
|
24
|
+
[tool.uv-dynamic-versioning]
|
|
25
|
+
pattern-prefix = "reflex-docgen-"
|
|
26
|
+
fallback-version = "0.0.0dev0"
|
|
27
|
+
|
|
28
|
+
[build-system]
|
|
29
|
+
requires = ["hatchling", "uv-dynamic-versioning"]
|
|
30
|
+
build-backend = "hatchling.build"
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Module for generating documentation for Reflex components and classes."""
|
|
2
|
+
|
|
3
|
+
from reflex_docgen import markdown as markdown
|
|
4
|
+
from reflex_docgen._class import ClassDocumentation as ClassDocumentation
|
|
5
|
+
from reflex_docgen._class import FieldDocumentation as FieldDocumentation
|
|
6
|
+
from reflex_docgen._class import MethodDocumentation as MethodDocumentation
|
|
7
|
+
from reflex_docgen._class import (
|
|
8
|
+
generate_class_documentation as generate_class_documentation,
|
|
9
|
+
)
|
|
10
|
+
from reflex_docgen._component import ComponentDocumentation as ComponentDocumentation
|
|
11
|
+
from reflex_docgen._component import (
|
|
12
|
+
EventHandlerDocumentation as EventHandlerDocumentation,
|
|
13
|
+
)
|
|
14
|
+
from reflex_docgen._component import PropDocumentation as PropDocumentation
|
|
15
|
+
from reflex_docgen._component import generate_documentation as generate_documentation
|
|
16
|
+
from reflex_docgen._component import (
|
|
17
|
+
get_component_event_handlers as get_component_event_handlers,
|
|
18
|
+
)
|
|
19
|
+
from reflex_docgen._component import get_component_props as get_component_props
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"ClassDocumentation",
|
|
23
|
+
"ComponentDocumentation",
|
|
24
|
+
"EventHandlerDocumentation",
|
|
25
|
+
"FieldDocumentation",
|
|
26
|
+
"MethodDocumentation",
|
|
27
|
+
"PropDocumentation",
|
|
28
|
+
"generate_class_documentation",
|
|
29
|
+
"generate_documentation",
|
|
30
|
+
"get_component_event_handlers",
|
|
31
|
+
"get_component_props",
|
|
32
|
+
"markdown",
|
|
33
|
+
]
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
"""Generate documentation for arbitrary Python classes."""
|
|
2
|
+
|
|
3
|
+
import dataclasses
|
|
4
|
+
import inspect
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Any, get_args, get_type_hints
|
|
7
|
+
|
|
8
|
+
from reflex_base.vars.base import BaseStateMeta
|
|
9
|
+
from typing_inspection.introspection import AnnotationSource, inspect_annotation
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
13
|
+
class FieldDocumentation:
|
|
14
|
+
"""Hold information about a class field.
|
|
15
|
+
|
|
16
|
+
Attributes:
|
|
17
|
+
name: The name of the field.
|
|
18
|
+
type: The resolved type of the field.
|
|
19
|
+
type_display: Human-readable type string (no Var wrapper). Uses __name__ for simple types, str() for generics.
|
|
20
|
+
description: The description extracted from the class docstring or field.doc.
|
|
21
|
+
default: The repr() of the default value, or None if no default.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
name: str
|
|
25
|
+
|
|
26
|
+
type: Any
|
|
27
|
+
|
|
28
|
+
type_display: str
|
|
29
|
+
|
|
30
|
+
description: str | None
|
|
31
|
+
|
|
32
|
+
default: str | None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
36
|
+
class MethodDocumentation:
|
|
37
|
+
"""Hold information about a class method.
|
|
38
|
+
|
|
39
|
+
Attributes:
|
|
40
|
+
name: The name of the method.
|
|
41
|
+
signature: The string representation of the method signature.
|
|
42
|
+
description: The docstring, truncated before 'Args:' or 'Returns:' sections.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
name: str
|
|
46
|
+
|
|
47
|
+
signature: str
|
|
48
|
+
|
|
49
|
+
description: str | None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
53
|
+
class ClassDocumentation:
|
|
54
|
+
"""Documentation for an arbitrary Python class.
|
|
55
|
+
|
|
56
|
+
Attributes:
|
|
57
|
+
name: The fully qualified name (module.qualname) of the class.
|
|
58
|
+
description: The cleaned docstring of the class.
|
|
59
|
+
fields: Instance fields (from dataclass fields or rx.State __fields__).
|
|
60
|
+
class_fields: Class variables (from __class_vars__ on rx.Base subclasses).
|
|
61
|
+
methods: Public methods that have docstrings.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
name: str
|
|
65
|
+
|
|
66
|
+
description: str | None
|
|
67
|
+
|
|
68
|
+
fields: tuple[FieldDocumentation, ...] = ()
|
|
69
|
+
|
|
70
|
+
class_fields: tuple[FieldDocumentation, ...] = ()
|
|
71
|
+
|
|
72
|
+
methods: tuple[MethodDocumentation, ...] = ()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _parse_docstring_attributes(cls: type) -> dict[str, str]:
|
|
76
|
+
"""Parse an Attributes section from a class docstring using griffe.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
cls: The class whose docstring to parse.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
A mapping from attribute name to description string.
|
|
83
|
+
"""
|
|
84
|
+
from griffe import Docstring, Parser # provided by griffelib
|
|
85
|
+
|
|
86
|
+
doc = cls.__doc__
|
|
87
|
+
if not doc:
|
|
88
|
+
return {}
|
|
89
|
+
|
|
90
|
+
parsed = Docstring(inspect.cleandoc(doc)).parse(Parser.auto)
|
|
91
|
+
return {
|
|
92
|
+
attr.name: attr.description
|
|
93
|
+
for section in parsed
|
|
94
|
+
if section.kind.value == "attributes"
|
|
95
|
+
for attr in section.value
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _type_display(type_: Any) -> str:
|
|
100
|
+
"""Return a human-readable type string.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
type_: The type to display.
|
|
104
|
+
|
|
105
|
+
Returns:
|
|
106
|
+
A human-readable type string.
|
|
107
|
+
"""
|
|
108
|
+
if get_args(type_):
|
|
109
|
+
return str(type_)
|
|
110
|
+
return getattr(type_, "__name__", str(type_))
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _extract_field_doc(hint: Any, field_doc: str | None) -> tuple[Any, str | None]:
|
|
114
|
+
"""Extract the unwrapped type and description from a type hint.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
hint: The type hint.
|
|
118
|
+
field_doc: The field.doc attribute value.
|
|
119
|
+
|
|
120
|
+
Returns:
|
|
121
|
+
A tuple of (unwrapped_type, description).
|
|
122
|
+
"""
|
|
123
|
+
inspected = inspect_annotation(hint, annotation_source=AnnotationSource.ANY)
|
|
124
|
+
return inspected.type, field_doc
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _build_field_documentation(
|
|
128
|
+
name: str,
|
|
129
|
+
hint: Any,
|
|
130
|
+
field_doc: str | None,
|
|
131
|
+
default_value: str | None,
|
|
132
|
+
docstring_desc: str | None = None,
|
|
133
|
+
) -> FieldDocumentation | None:
|
|
134
|
+
"""Build a FieldDocumentation from field info, or None if private.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
name: The field name.
|
|
138
|
+
hint: The type hint.
|
|
139
|
+
field_doc: The field.doc attribute value.
|
|
140
|
+
default_value: The repr'd default value, or None.
|
|
141
|
+
docstring_desc: Description from the class docstring Attributes section (fallback).
|
|
142
|
+
|
|
143
|
+
Returns:
|
|
144
|
+
A FieldDocumentation, or None if the field should be skipped.
|
|
145
|
+
"""
|
|
146
|
+
if name.startswith("_"):
|
|
147
|
+
return None
|
|
148
|
+
|
|
149
|
+
unwrapped_type, description = _extract_field_doc(hint, field_doc)
|
|
150
|
+
|
|
151
|
+
# Fall back to docstring Attributes section if no description found.
|
|
152
|
+
if description is None and docstring_desc is not None:
|
|
153
|
+
description = docstring_desc
|
|
154
|
+
|
|
155
|
+
if description is not None and "PRIVATE" in description:
|
|
156
|
+
return None
|
|
157
|
+
|
|
158
|
+
return FieldDocumentation(
|
|
159
|
+
name=name,
|
|
160
|
+
type=unwrapped_type,
|
|
161
|
+
type_display=_type_display(unwrapped_type),
|
|
162
|
+
description=description,
|
|
163
|
+
default=default_value,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _get_dataclass_fields(cls: type) -> tuple[FieldDocumentation, ...]:
|
|
168
|
+
"""Extract fields from a dataclass.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
cls: The dataclass to extract fields from.
|
|
172
|
+
|
|
173
|
+
Returns:
|
|
174
|
+
A tuple of FieldDocumentation objects.
|
|
175
|
+
"""
|
|
176
|
+
hints = get_type_hints(cls, include_extras=True)
|
|
177
|
+
docstring_attrs = _parse_docstring_attributes(cls)
|
|
178
|
+
result = []
|
|
179
|
+
for f in dataclasses.fields(cls):
|
|
180
|
+
hint = hints.get(f.name, f.type)
|
|
181
|
+
field_doc = getattr(f, "doc", None)
|
|
182
|
+
|
|
183
|
+
if f.default is not dataclasses.MISSING:
|
|
184
|
+
default_str = repr(f.default)
|
|
185
|
+
elif f.default_factory is not dataclasses.MISSING:
|
|
186
|
+
default_str = repr(f.default_factory)
|
|
187
|
+
else:
|
|
188
|
+
default_str = None
|
|
189
|
+
|
|
190
|
+
doc = _build_field_documentation(
|
|
191
|
+
f.name, hint, field_doc, default_str, docstring_attrs.get(f.name)
|
|
192
|
+
)
|
|
193
|
+
if doc is not None:
|
|
194
|
+
result.append(doc)
|
|
195
|
+
|
|
196
|
+
return tuple(result)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _get_state_fields(cls: BaseStateMeta) -> tuple[FieldDocumentation, ...]:
|
|
200
|
+
"""Extract instance fields from an rx.State subclass via __fields__.
|
|
201
|
+
|
|
202
|
+
Args:
|
|
203
|
+
cls: The class to extract fields from.
|
|
204
|
+
|
|
205
|
+
Returns:
|
|
206
|
+
A tuple of FieldDocumentation objects.
|
|
207
|
+
"""
|
|
208
|
+
hints = get_type_hints(cls, include_extras=True)
|
|
209
|
+
docstring_attrs = _parse_docstring_attributes(cls)
|
|
210
|
+
fields_dict = cls.__fields__
|
|
211
|
+
result = []
|
|
212
|
+
for name, field in fields_dict.items():
|
|
213
|
+
hint = hints.get(name, field.outer_type_)
|
|
214
|
+
|
|
215
|
+
if field.default is not dataclasses.MISSING:
|
|
216
|
+
default_str = repr(field.default)
|
|
217
|
+
elif field.default_factory is not None:
|
|
218
|
+
default_str = repr(field.default_factory)
|
|
219
|
+
else:
|
|
220
|
+
default_str = None
|
|
221
|
+
|
|
222
|
+
doc = _build_field_documentation(
|
|
223
|
+
name, hint, None, default_str, docstring_attrs.get(name)
|
|
224
|
+
)
|
|
225
|
+
if doc is not None:
|
|
226
|
+
result.append(doc)
|
|
227
|
+
|
|
228
|
+
return tuple(result)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _get_class_vars(cls: type) -> tuple[FieldDocumentation, ...]:
|
|
232
|
+
"""Extract class variables from __class_vars__.
|
|
233
|
+
|
|
234
|
+
Args:
|
|
235
|
+
cls: The class to extract class variables from.
|
|
236
|
+
|
|
237
|
+
Returns:
|
|
238
|
+
A tuple of FieldDocumentation objects.
|
|
239
|
+
"""
|
|
240
|
+
class_vars = getattr(cls, "__class_vars__", None)
|
|
241
|
+
if not class_vars:
|
|
242
|
+
return ()
|
|
243
|
+
|
|
244
|
+
hints = get_type_hints(cls, include_extras=True)
|
|
245
|
+
docstring_attrs = _parse_docstring_attributes(cls)
|
|
246
|
+
result = []
|
|
247
|
+
for name in class_vars:
|
|
248
|
+
hint = hints.get(name, type(None))
|
|
249
|
+
doc = _build_field_documentation(
|
|
250
|
+
name, hint, None, None, docstring_attrs.get(name)
|
|
251
|
+
)
|
|
252
|
+
if doc is not None:
|
|
253
|
+
result.append(doc)
|
|
254
|
+
|
|
255
|
+
return tuple(result)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def _get_methods(cls: type) -> tuple[MethodDocumentation, ...]:
|
|
259
|
+
"""Extract public documented methods from a class.
|
|
260
|
+
|
|
261
|
+
Args:
|
|
262
|
+
cls: The class to extract methods from.
|
|
263
|
+
|
|
264
|
+
Returns:
|
|
265
|
+
A tuple of MethodDocumentation objects.
|
|
266
|
+
"""
|
|
267
|
+
result = []
|
|
268
|
+
for name, obj in cls.__dict__.items():
|
|
269
|
+
if name.startswith("_") or name == "Config":
|
|
270
|
+
continue
|
|
271
|
+
|
|
272
|
+
fn = obj
|
|
273
|
+
if isinstance(obj, (classmethod, staticmethod)):
|
|
274
|
+
fn = obj.__func__
|
|
275
|
+
|
|
276
|
+
if not callable(fn):
|
|
277
|
+
continue
|
|
278
|
+
|
|
279
|
+
docstring = getattr(fn, "__doc__", None)
|
|
280
|
+
if not docstring:
|
|
281
|
+
continue
|
|
282
|
+
|
|
283
|
+
# Truncate docstring before Args: or Returns:
|
|
284
|
+
for marker in ("Args:", "Returns:"):
|
|
285
|
+
idx = docstring.find(marker)
|
|
286
|
+
if idx != -1:
|
|
287
|
+
docstring = docstring[:idx]
|
|
288
|
+
break
|
|
289
|
+
docstring = docstring.strip() or None
|
|
290
|
+
|
|
291
|
+
try:
|
|
292
|
+
sig = str(inspect.signature(fn))
|
|
293
|
+
except (ValueError, TypeError):
|
|
294
|
+
sig = "(...)"
|
|
295
|
+
|
|
296
|
+
result.append(
|
|
297
|
+
MethodDocumentation(
|
|
298
|
+
name=name,
|
|
299
|
+
signature=sig,
|
|
300
|
+
description=docstring,
|
|
301
|
+
)
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
return tuple(result)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def generate_class_documentation(cls: type) -> ClassDocumentation:
|
|
308
|
+
"""Generate documentation for an arbitrary Python class.
|
|
309
|
+
|
|
310
|
+
Supports dataclasses, rx.State subclasses, and other classes (methods only).
|
|
311
|
+
|
|
312
|
+
Args:
|
|
313
|
+
cls: The class to generate documentation for.
|
|
314
|
+
|
|
315
|
+
Returns:
|
|
316
|
+
The generated documentation for the class.
|
|
317
|
+
"""
|
|
318
|
+
try:
|
|
319
|
+
description = inspect.cleandoc(cls.__doc__) if cls.__doc__ else None
|
|
320
|
+
|
|
321
|
+
if dataclasses.is_dataclass(cls):
|
|
322
|
+
fields = _get_dataclass_fields(cls)
|
|
323
|
+
elif isinstance(cls, BaseStateMeta):
|
|
324
|
+
fields = _get_state_fields(cls)
|
|
325
|
+
else:
|
|
326
|
+
fields = ()
|
|
327
|
+
|
|
328
|
+
class_fields = _get_class_vars(cls)
|
|
329
|
+
methods = _get_methods(cls)
|
|
330
|
+
|
|
331
|
+
return ClassDocumentation(
|
|
332
|
+
name=f"{cls.__module__}.{cls.__qualname__}",
|
|
333
|
+
description=description,
|
|
334
|
+
fields=fields,
|
|
335
|
+
class_fields=class_fields,
|
|
336
|
+
methods=methods,
|
|
337
|
+
)
|
|
338
|
+
except Exception as e:
|
|
339
|
+
import sys
|
|
340
|
+
|
|
341
|
+
if sys.version_info >= (3, 11):
|
|
342
|
+
e.add_note(
|
|
343
|
+
f"Error generating documentation for class {cls.__module__}.{cls.__qualname__}"
|
|
344
|
+
)
|
|
345
|
+
raise
|