wesley-python 0.1.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.
- wesley_python-0.1.0a1/LICENSE +202 -0
- wesley_python-0.1.0a1/MANIFEST.in +3 -0
- wesley_python-0.1.0a1/NOTICE +5 -0
- wesley_python-0.1.0a1/PKG-INFO +172 -0
- wesley_python-0.1.0a1/README.md +141 -0
- wesley_python-0.1.0a1/pyproject.toml +90 -0
- wesley_python-0.1.0a1/setup.cfg +4 -0
- wesley_python-0.1.0a1/src/wesley_python/__init__.py +11 -0
- wesley_python-0.1.0a1/src/wesley_python/__main__.py +9 -0
- wesley_python-0.1.0a1/src/wesley_python/cli.py +59 -0
- wesley_python-0.1.0a1/src/wesley_python/emit.py +218 -0
- wesley_python-0.1.0a1/src/wesley_python/py.typed +0 -0
- wesley_python-0.1.0a1/src/wesley_python/syntax.py +270 -0
- wesley_python-0.1.0a1/src/wesley_python/types.py +220 -0
- wesley_python-0.1.0a1/src/wesley_python.egg-info/PKG-INFO +172 -0
- wesley_python-0.1.0a1/src/wesley_python.egg-info/SOURCES.txt +17 -0
- wesley_python-0.1.0a1/src/wesley_python.egg-info/dependency_links.txt +1 -0
- wesley_python-0.1.0a1/src/wesley_python.egg-info/entry_points.txt +2 -0
- wesley_python-0.1.0a1/src/wesley_python.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wesley-python
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: A generic Wesley target that emits standard-library Python TypedDicts from Wesley IR.
|
|
5
|
+
Author: flyingrobots
|
|
6
|
+
Maintainer: flyingrobots
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
Project-URL: Homepage, https://github.com/flyingrobots/wesley-python
|
|
9
|
+
Project-URL: Repository, https://github.com/flyingrobots/wesley-python
|
|
10
|
+
Project-URL: Changelog, https://github.com/flyingrobots/wesley-python/blob/main/CHANGELOG.md
|
|
11
|
+
Project-URL: Issues, https://github.com/flyingrobots/wesley-python/issues
|
|
12
|
+
Keywords: wesley,graphql,typeddict,codegen,code-generation,typing
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Environment :: Console
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
25
|
+
Classifier: Typing :: Typed
|
|
26
|
+
Requires-Python: >=3.9
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
License-File: NOTICE
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
|
|
32
|
+
# wesley-python
|
|
33
|
+
|
|
34
|
+
A [Wesley](https://github.com/flyingrobots/wesley) target that emits stdlib-only Python: `TypedDict` models embeddable in a single-file module.
|
|
35
|
+
|
|
36
|
+
Wesley compiles GraphQL SDL into an intermediate representation (IR), and does nothing else: it attaches no meaning to any directive and runs no targets. Every output language is its own repository, a standalone program and library that reads the IR (`wesley schema lower --json`). This one is a **generic** target: it understands no directives and emits Python from plain GraphQL, and it is the language tooling other targets (such as [wesley-parley](https://github.com/flyingrobots/wesley-parley)) render through. The decisions are recorded in flyingrobots/parley#116 and flyingrobots/wesley#882.
|
|
37
|
+
|
|
38
|
+
## Status
|
|
39
|
+
|
|
40
|
+
The first release is 0.1.0a1, a pre-release: the package `wesley_python`, with a syntax API for building Python modules of `TypedDict` classes and type aliases, and a plain-GraphQL emitter on the command line. It reads IR version `1.0.0` as Wesley prints it today; the versioned IR contract is still flyingrobots/wesley#881. There is no `--fragment` mode, no marker-wrapped output and no generated header naming the schema hash yet (#1).
|
|
41
|
+
|
|
42
|
+
The package runs on Python 3.9 or later and imports nothing outside the standard library. Its generated modules do the same.
|
|
43
|
+
|
|
44
|
+
## Install
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
pip install wesley-python==0.1.0a1
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The package is `wesley-python` on PyPI and `wesley_python` to import. While every release is a pre-release, `pip install wesley-python` needs `--pre` or an exact version.
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
### The command line
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
wesley schema lower --schema schema.graphql --json | python -m wesley_python emit > models.py
|
|
58
|
+
python -m wesley_python emit --ir ir.json --out models.py
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`emit` reads Wesley IR JSON from `--ir` (standard input by default) and writes a module to `--out` (standard output by default). An IR whose `version` is not `1.0.0`, malformed IR or invalid JSON is an error: a message on standard error and exit status 1. Installing the package also installs the same command as `wesley-python`.
|
|
62
|
+
|
|
63
|
+
The emitter is generic: it ignores every directive. It maps GraphQL as follows.
|
|
64
|
+
|
|
65
|
+
| GraphQL | Python |
|
|
66
|
+
| --- | --- |
|
|
67
|
+
| `type`, `input`, `interface` | a `TypedDict`; every field is a required key |
|
|
68
|
+
| nullable field `T` | `Union[T, None]`: GraphQL null is a value, not an absent key |
|
|
69
|
+
| `[T!]`, `[T]` | `List[T]`, `List[Union[T, None]]` |
|
|
70
|
+
| `enum` | an alias of `Literal['A', 'B', ...]` of the value names |
|
|
71
|
+
| `union` | an alias of `Union[...]` of the members |
|
|
72
|
+
| `String`, `ID`, `Int`, `Float`, `Boolean` | `str`, `str`, `int`, `float`, `bool` |
|
|
73
|
+
| any other scalar | `Any` |
|
|
74
|
+
|
|
75
|
+
Types are written in IR order, except that a union alias moves to just after the last of its members: a type checker reads `A = 'B'` as a string, not as an alias, and Python 3.9 has no `typing.TypeAlias`. A reference to a type written later, inside a field or a subscript such as `List['B']`, is a string forward reference, so the module imports.
|
|
76
|
+
|
|
77
|
+
### The syntax API
|
|
78
|
+
|
|
79
|
+
Other targets build modules with the syntax API and print them with `render`. Optional keys, keys that are Python keywords and `Literal` values all work on Python 3.9.
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from wesley_python.syntax import Field, Module, TypedDictDef, render
|
|
83
|
+
from wesley_python.types import Int, Literal, Ref
|
|
84
|
+
|
|
85
|
+
module = Module(
|
|
86
|
+
[
|
|
87
|
+
TypedDictDef(
|
|
88
|
+
"Window",
|
|
89
|
+
[
|
|
90
|
+
Field("from", Int(), required=True, doc="The first line, inclusive."),
|
|
91
|
+
Field("to", Int(), required=False),
|
|
92
|
+
],
|
|
93
|
+
doc="A range of line numbers.",
|
|
94
|
+
),
|
|
95
|
+
TypedDictDef(
|
|
96
|
+
"End",
|
|
97
|
+
[
|
|
98
|
+
Field("event", Literal("end"), required=True),
|
|
99
|
+
Field("window", Ref("Window"), required=False),
|
|
100
|
+
],
|
|
101
|
+
),
|
|
102
|
+
]
|
|
103
|
+
)
|
|
104
|
+
print(render(module), end="")
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
prints, exactly as `ast.unparse` lays it out,
|
|
108
|
+
|
|
109
|
+
```text
|
|
110
|
+
from typing import Literal, TypedDict
|
|
111
|
+
_WindowRequired = TypedDict('_WindowRequired', {'from': int})
|
|
112
|
+
_WindowOptional = TypedDict('_WindowOptional', {'to': int}, total=False)
|
|
113
|
+
|
|
114
|
+
class Window(_WindowRequired, _WindowOptional):
|
|
115
|
+
"""A range of line numbers.
|
|
116
|
+
|
|
117
|
+
from: The first line, inclusive.
|
|
118
|
+
"""
|
|
119
|
+
_EndRequired = TypedDict('_EndRequired', {'event': Literal['end']})
|
|
120
|
+
_EndOptional = TypedDict('_EndOptional', {'window': Window}, total=False)
|
|
121
|
+
|
|
122
|
+
class End(_EndRequired, _EndOptional):
|
|
123
|
+
pass
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
- `wesley_python.types`: `Str()`, `Int()`, `Float()`, `Bool()`, `AnyValue()`, `NoneType()`, `Literal(value)` for a `str`, `int` or `bool`, `ListOf(item)`, `DictOf(value)` (string keys), `Ref(name)` for a type the module defines, and `UnionOf(*members)`. A union of one member prints as that member, and a union of literals prints as one `Literal[...]`.
|
|
127
|
+
- `wesley_python.syntax`: `Field(name, type, required, doc=None)`, `TypedDictDef(name, fields, doc=None)`, `AliasDef(name, type, doc=None)`, `Module(definitions, doc=None)`, `to_ast(module)` returning the `ast.Module`, and `render(module)` returning its source, ending in one newline.
|
|
128
|
+
|
|
129
|
+
How a `TypedDict` is written: `typing.NotRequired` is new in Python 3.11, and the class syntax cannot name a key such as `from`. So the required keys and the optional keys are each a functional-form `TypedDict`, the optional one `total=False`, and the class inherits from both; a side with no keys is left out, and a definition with no keys inherits from `TypedDict`. `ast.unparse` cannot print comments, so field docs go into the class docstring as a `name: doc` list, and an alias's doc is a string statement after it. The `from typing import ...` line names exactly the names used, sorted.
|
|
130
|
+
|
|
131
|
+
The caller orders the definitions; `render` keeps the given order. A reference to a later definition is a string forward reference, which is allowed inside a field type or a subscript but not as the whole type of an alias: `AliasDef('Result', Ref('Item'))` before `Item` is a `ValueError` naming both. An alias whose whole type is `None` is written `Name = Literal[None]`, because `Name = None` binds a value rather than an alias. A definition name must be a Python identifier; two definitions may not bind the same name (including the `_NameRequired` and `_NameOptional` helpers), and a definition may not shadow a `typing` or builtin name the module uses. These are `ValueError`s.
|
|
132
|
+
|
|
133
|
+
## Development
|
|
134
|
+
|
|
135
|
+
```sh
|
|
136
|
+
uv venv -p 3.9 .venv && uv pip install -p .venv -r requirements-dev.txt
|
|
137
|
+
git config --local core.hooksPath scripts/hooks
|
|
138
|
+
PYTHONPATH=src .venv/bin/python -m unittest discover -s tests -t .
|
|
139
|
+
.venv/bin/ruff check . && .venv/bin/ruff format --check . && .venv/bin/mypy
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`requirements-dev.txt` pins mypy, ruff, build and twine exactly; they are development tools, not dependencies. The pre-commit hook runs ruff and mypy, and the pre-push hook runs the test suite. CI runs all of it on Python 3.9 and 3.13. It also builds the sdist and wheel, checks them with `twine check --strict` and for `LICENSE` and `NOTICE`, and installs the wheel into fresh environments to run it (`scripts/check-dist`).
|
|
143
|
+
|
|
144
|
+
Releases are made by pushing an annotated tag; [RELEASING.md](https://github.com/flyingrobots/wesley-python/blob/main/RELEASING.md) describes the procedure.
|
|
145
|
+
|
|
146
|
+
The goldens under `tests/golden/` are compared byte for byte, compiled with `py_compile`, imported, and type-checked by `mypy --strict --python-version 3.9` together with the fixtures under `tests/typecheck/`: the good uses must pass and each `bad_*.py` must fail with its expected error (a wrong `event` literal, a keyword key read as the wrong type, a missing required key). To rewrite the goldens after an intended change, run the suite with `WESLEY_PYTHON_UPDATE_GOLDENS=1` and review the diff.
|
|
147
|
+
|
|
148
|
+
## Where it comes from
|
|
149
|
+
|
|
150
|
+
The plan is issue #1 (transferred from flyingrobots/wesley#858). Its first consumer is Parley's vendorable Python writer (flyingrobots/parley#121).
|
|
151
|
+
|
|
152
|
+
## How it generates code
|
|
153
|
+
|
|
154
|
+
The output is built as a syntax tree of the target language and printed by that language's printer. There is no string templating and no regex anywhere in the generator.
|
|
155
|
+
|
|
156
|
+
| Concern | Choice |
|
|
157
|
+
| --- | --- |
|
|
158
|
+
| Written in | Python 3.9 or later, standard library only |
|
|
159
|
+
| Syntax tree | the standard library `ast` module |
|
|
160
|
+
| Printer | `ast.unparse` |
|
|
161
|
+
| Checked by | `py_compile` and a static type checker on every golden module, on the oldest and newest supported Python |
|
|
162
|
+
|
|
163
|
+
Rules every change keeps:
|
|
164
|
+
|
|
165
|
+
- **No code as strings.** A generated identifier, literal or comment enters the output as a node of the tree, never by concatenating source text. Escaping is the printer's job.
|
|
166
|
+
- **Golden output is checked by the real toolchain.** Every golden file is compiled or type-checked in CI by the toolchain named above, not only compared byte for byte.
|
|
167
|
+
- **Deterministic to the byte.** The same IR and options give the same bytes on every machine. CI runs the generator twice and compares.
|
|
168
|
+
- **The IR is the only input.** The target reads the published IR (flyingrobots/wesley#881) and never depends on Wesley's internals.
|
|
169
|
+
|
|
170
|
+
## License
|
|
171
|
+
|
|
172
|
+
Apache 2.0. See [LICENSE](https://github.com/flyingrobots/wesley-python/blob/main/LICENSE) and [NOTICE](https://github.com/flyingrobots/wesley-python/blob/main/NOTICE).
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# wesley-python
|
|
2
|
+
|
|
3
|
+
A [Wesley](https://github.com/flyingrobots/wesley) target that emits stdlib-only Python: `TypedDict` models embeddable in a single-file module.
|
|
4
|
+
|
|
5
|
+
Wesley compiles GraphQL SDL into an intermediate representation (IR), and does nothing else: it attaches no meaning to any directive and runs no targets. Every output language is its own repository, a standalone program and library that reads the IR (`wesley schema lower --json`). This one is a **generic** target: it understands no directives and emits Python from plain GraphQL, and it is the language tooling other targets (such as [wesley-parley](https://github.com/flyingrobots/wesley-parley)) render through. The decisions are recorded in flyingrobots/parley#116 and flyingrobots/wesley#882.
|
|
6
|
+
|
|
7
|
+
## Status
|
|
8
|
+
|
|
9
|
+
The first release is 0.1.0a1, a pre-release: the package `wesley_python`, with a syntax API for building Python modules of `TypedDict` classes and type aliases, and a plain-GraphQL emitter on the command line. It reads IR version `1.0.0` as Wesley prints it today; the versioned IR contract is still flyingrobots/wesley#881. There is no `--fragment` mode, no marker-wrapped output and no generated header naming the schema hash yet (#1).
|
|
10
|
+
|
|
11
|
+
The package runs on Python 3.9 or later and imports nothing outside the standard library. Its generated modules do the same.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pip install wesley-python==0.1.0a1
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The package is `wesley-python` on PyPI and `wesley_python` to import. While every release is a pre-release, `pip install wesley-python` needs `--pre` or an exact version.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
### The command line
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
wesley schema lower --schema schema.graphql --json | python -m wesley_python emit > models.py
|
|
27
|
+
python -m wesley_python emit --ir ir.json --out models.py
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`emit` reads Wesley IR JSON from `--ir` (standard input by default) and writes a module to `--out` (standard output by default). An IR whose `version` is not `1.0.0`, malformed IR or invalid JSON is an error: a message on standard error and exit status 1. Installing the package also installs the same command as `wesley-python`.
|
|
31
|
+
|
|
32
|
+
The emitter is generic: it ignores every directive. It maps GraphQL as follows.
|
|
33
|
+
|
|
34
|
+
| GraphQL | Python |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `type`, `input`, `interface` | a `TypedDict`; every field is a required key |
|
|
37
|
+
| nullable field `T` | `Union[T, None]`: GraphQL null is a value, not an absent key |
|
|
38
|
+
| `[T!]`, `[T]` | `List[T]`, `List[Union[T, None]]` |
|
|
39
|
+
| `enum` | an alias of `Literal['A', 'B', ...]` of the value names |
|
|
40
|
+
| `union` | an alias of `Union[...]` of the members |
|
|
41
|
+
| `String`, `ID`, `Int`, `Float`, `Boolean` | `str`, `str`, `int`, `float`, `bool` |
|
|
42
|
+
| any other scalar | `Any` |
|
|
43
|
+
|
|
44
|
+
Types are written in IR order, except that a union alias moves to just after the last of its members: a type checker reads `A = 'B'` as a string, not as an alias, and Python 3.9 has no `typing.TypeAlias`. A reference to a type written later, inside a field or a subscript such as `List['B']`, is a string forward reference, so the module imports.
|
|
45
|
+
|
|
46
|
+
### The syntax API
|
|
47
|
+
|
|
48
|
+
Other targets build modules with the syntax API and print them with `render`. Optional keys, keys that are Python keywords and `Literal` values all work on Python 3.9.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from wesley_python.syntax import Field, Module, TypedDictDef, render
|
|
52
|
+
from wesley_python.types import Int, Literal, Ref
|
|
53
|
+
|
|
54
|
+
module = Module(
|
|
55
|
+
[
|
|
56
|
+
TypedDictDef(
|
|
57
|
+
"Window",
|
|
58
|
+
[
|
|
59
|
+
Field("from", Int(), required=True, doc="The first line, inclusive."),
|
|
60
|
+
Field("to", Int(), required=False),
|
|
61
|
+
],
|
|
62
|
+
doc="A range of line numbers.",
|
|
63
|
+
),
|
|
64
|
+
TypedDictDef(
|
|
65
|
+
"End",
|
|
66
|
+
[
|
|
67
|
+
Field("event", Literal("end"), required=True),
|
|
68
|
+
Field("window", Ref("Window"), required=False),
|
|
69
|
+
],
|
|
70
|
+
),
|
|
71
|
+
]
|
|
72
|
+
)
|
|
73
|
+
print(render(module), end="")
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
prints, exactly as `ast.unparse` lays it out,
|
|
77
|
+
|
|
78
|
+
```text
|
|
79
|
+
from typing import Literal, TypedDict
|
|
80
|
+
_WindowRequired = TypedDict('_WindowRequired', {'from': int})
|
|
81
|
+
_WindowOptional = TypedDict('_WindowOptional', {'to': int}, total=False)
|
|
82
|
+
|
|
83
|
+
class Window(_WindowRequired, _WindowOptional):
|
|
84
|
+
"""A range of line numbers.
|
|
85
|
+
|
|
86
|
+
from: The first line, inclusive.
|
|
87
|
+
"""
|
|
88
|
+
_EndRequired = TypedDict('_EndRequired', {'event': Literal['end']})
|
|
89
|
+
_EndOptional = TypedDict('_EndOptional', {'window': Window}, total=False)
|
|
90
|
+
|
|
91
|
+
class End(_EndRequired, _EndOptional):
|
|
92
|
+
pass
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
- `wesley_python.types`: `Str()`, `Int()`, `Float()`, `Bool()`, `AnyValue()`, `NoneType()`, `Literal(value)` for a `str`, `int` or `bool`, `ListOf(item)`, `DictOf(value)` (string keys), `Ref(name)` for a type the module defines, and `UnionOf(*members)`. A union of one member prints as that member, and a union of literals prints as one `Literal[...]`.
|
|
96
|
+
- `wesley_python.syntax`: `Field(name, type, required, doc=None)`, `TypedDictDef(name, fields, doc=None)`, `AliasDef(name, type, doc=None)`, `Module(definitions, doc=None)`, `to_ast(module)` returning the `ast.Module`, and `render(module)` returning its source, ending in one newline.
|
|
97
|
+
|
|
98
|
+
How a `TypedDict` is written: `typing.NotRequired` is new in Python 3.11, and the class syntax cannot name a key such as `from`. So the required keys and the optional keys are each a functional-form `TypedDict`, the optional one `total=False`, and the class inherits from both; a side with no keys is left out, and a definition with no keys inherits from `TypedDict`. `ast.unparse` cannot print comments, so field docs go into the class docstring as a `name: doc` list, and an alias's doc is a string statement after it. The `from typing import ...` line names exactly the names used, sorted.
|
|
99
|
+
|
|
100
|
+
The caller orders the definitions; `render` keeps the given order. A reference to a later definition is a string forward reference, which is allowed inside a field type or a subscript but not as the whole type of an alias: `AliasDef('Result', Ref('Item'))` before `Item` is a `ValueError` naming both. An alias whose whole type is `None` is written `Name = Literal[None]`, because `Name = None` binds a value rather than an alias. A definition name must be a Python identifier; two definitions may not bind the same name (including the `_NameRequired` and `_NameOptional` helpers), and a definition may not shadow a `typing` or builtin name the module uses. These are `ValueError`s.
|
|
101
|
+
|
|
102
|
+
## Development
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
uv venv -p 3.9 .venv && uv pip install -p .venv -r requirements-dev.txt
|
|
106
|
+
git config --local core.hooksPath scripts/hooks
|
|
107
|
+
PYTHONPATH=src .venv/bin/python -m unittest discover -s tests -t .
|
|
108
|
+
.venv/bin/ruff check . && .venv/bin/ruff format --check . && .venv/bin/mypy
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`requirements-dev.txt` pins mypy, ruff, build and twine exactly; they are development tools, not dependencies. The pre-commit hook runs ruff and mypy, and the pre-push hook runs the test suite. CI runs all of it on Python 3.9 and 3.13. It also builds the sdist and wheel, checks them with `twine check --strict` and for `LICENSE` and `NOTICE`, and installs the wheel into fresh environments to run it (`scripts/check-dist`).
|
|
112
|
+
|
|
113
|
+
Releases are made by pushing an annotated tag; [RELEASING.md](https://github.com/flyingrobots/wesley-python/blob/main/RELEASING.md) describes the procedure.
|
|
114
|
+
|
|
115
|
+
The goldens under `tests/golden/` are compared byte for byte, compiled with `py_compile`, imported, and type-checked by `mypy --strict --python-version 3.9` together with the fixtures under `tests/typecheck/`: the good uses must pass and each `bad_*.py` must fail with its expected error (a wrong `event` literal, a keyword key read as the wrong type, a missing required key). To rewrite the goldens after an intended change, run the suite with `WESLEY_PYTHON_UPDATE_GOLDENS=1` and review the diff.
|
|
116
|
+
|
|
117
|
+
## Where it comes from
|
|
118
|
+
|
|
119
|
+
The plan is issue #1 (transferred from flyingrobots/wesley#858). Its first consumer is Parley's vendorable Python writer (flyingrobots/parley#121).
|
|
120
|
+
|
|
121
|
+
## How it generates code
|
|
122
|
+
|
|
123
|
+
The output is built as a syntax tree of the target language and printed by that language's printer. There is no string templating and no regex anywhere in the generator.
|
|
124
|
+
|
|
125
|
+
| Concern | Choice |
|
|
126
|
+
| --- | --- |
|
|
127
|
+
| Written in | Python 3.9 or later, standard library only |
|
|
128
|
+
| Syntax tree | the standard library `ast` module |
|
|
129
|
+
| Printer | `ast.unparse` |
|
|
130
|
+
| Checked by | `py_compile` and a static type checker on every golden module, on the oldest and newest supported Python |
|
|
131
|
+
|
|
132
|
+
Rules every change keeps:
|
|
133
|
+
|
|
134
|
+
- **No code as strings.** A generated identifier, literal or comment enters the output as a node of the tree, never by concatenating source text. Escaping is the printer's job.
|
|
135
|
+
- **Golden output is checked by the real toolchain.** Every golden file is compiled or type-checked in CI by the toolchain named above, not only compared byte for byte.
|
|
136
|
+
- **Deterministic to the byte.** The same IR and options give the same bytes on every machine. CI runs the generator twice and compares.
|
|
137
|
+
- **The IR is the only input.** The target reads the published IR (flyingrobots/wesley#881) and never depends on Wesley's internals.
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
Apache 2.0. See [LICENSE](https://github.com/flyingrobots/wesley-python/blob/main/LICENSE) and [NOTICE](https://github.com/flyingrobots/wesley-python/blob/main/NOTICE).
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
# 77 is the first setuptools with SPDX `license` expressions and `license-files` (PEP 639).
|
|
3
|
+
requires = ["setuptools>=77"]
|
|
4
|
+
build-backend = "setuptools.build_meta"
|
|
5
|
+
|
|
6
|
+
[project]
|
|
7
|
+
name = "wesley-python"
|
|
8
|
+
version = "0.1.0a1"
|
|
9
|
+
description = "A generic Wesley target that emits standard-library Python TypedDicts from Wesley IR."
|
|
10
|
+
readme = { file = "README.md", content-type = "text/markdown" }
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE", "NOTICE"]
|
|
13
|
+
authors = [{ name = "flyingrobots" }]
|
|
14
|
+
maintainers = [{ name = "flyingrobots" }]
|
|
15
|
+
requires-python = ">=3.9"
|
|
16
|
+
dependencies = []
|
|
17
|
+
keywords = ["wesley", "graphql", "typeddict", "codegen", "code-generation", "typing"]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Development Status :: 3 - Alpha",
|
|
20
|
+
"Environment :: Console",
|
|
21
|
+
"Intended Audience :: Developers",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
25
|
+
"Programming Language :: Python :: 3.9",
|
|
26
|
+
"Programming Language :: Python :: 3.10",
|
|
27
|
+
"Programming Language :: Python :: 3.11",
|
|
28
|
+
"Programming Language :: Python :: 3.12",
|
|
29
|
+
"Programming Language :: Python :: 3.13",
|
|
30
|
+
"Topic :: Software Development :: Code Generators",
|
|
31
|
+
"Typing :: Typed",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://github.com/flyingrobots/wesley-python"
|
|
36
|
+
Repository = "https://github.com/flyingrobots/wesley-python"
|
|
37
|
+
Changelog = "https://github.com/flyingrobots/wesley-python/blob/main/CHANGELOG.md"
|
|
38
|
+
Issues = "https://github.com/flyingrobots/wesley-python/issues"
|
|
39
|
+
|
|
40
|
+
[project.scripts]
|
|
41
|
+
wesley-python = "wesley_python.cli:main"
|
|
42
|
+
|
|
43
|
+
[tool.setuptools.packages.find]
|
|
44
|
+
where = ["src"]
|
|
45
|
+
|
|
46
|
+
[tool.setuptools.package-data]
|
|
47
|
+
wesley_python = ["py.typed"]
|
|
48
|
+
|
|
49
|
+
[tool.ruff]
|
|
50
|
+
target-version = "py39"
|
|
51
|
+
line-length = 120
|
|
52
|
+
# Goldens are generated output in the Python 3.9 typing style, and the type-check fixtures are
|
|
53
|
+
# deliberately wrong. Both are checked by py_compile and mypy in the test suite instead.
|
|
54
|
+
extend-exclude = ["tests/golden", "tests/typecheck"]
|
|
55
|
+
|
|
56
|
+
[tool.ruff.lint]
|
|
57
|
+
select = ["ALL"]
|
|
58
|
+
ignore = [
|
|
59
|
+
# The formatter owns these; ruff documents them as conflicting with `ruff format`.
|
|
60
|
+
"COM812",
|
|
61
|
+
"ISC001",
|
|
62
|
+
# D203 and D211 conflict, as do D212 and D213; keep D211 and D212.
|
|
63
|
+
"D203",
|
|
64
|
+
"D213",
|
|
65
|
+
# Docstrings name their parameters in prose; a Sphinx or Google section per argument is noise.
|
|
66
|
+
"D417",
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
[tool.ruff.lint.per-file-ignores]
|
|
70
|
+
# `wesley_python.types` is the public API's name for the type expressions; it shadows the standard
|
|
71
|
+
# library `types` module only inside the package namespace, which absolute imports make harmless.
|
|
72
|
+
"src/wesley_python/types.py" = ["A005"]
|
|
73
|
+
"tests/**" = [
|
|
74
|
+
# The suite is stdlib `unittest` by choice (no runtime or test dependencies beyond the pinned
|
|
75
|
+
# tools), so unittest-style assertions are the idiom rather than bare `assert`.
|
|
76
|
+
"PT009",
|
|
77
|
+
"PT027",
|
|
78
|
+
# Test classes and methods are named for what they check; docstrings on each would repeat it.
|
|
79
|
+
"D101",
|
|
80
|
+
"D102",
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
[tool.mypy]
|
|
85
|
+
strict = true
|
|
86
|
+
python_version = "3.9"
|
|
87
|
+
files = ["src", "tests", "scripts"]
|
|
88
|
+
exclude = ["^tests/golden/", "^tests/typecheck/"]
|
|
89
|
+
mypy_path = "src"
|
|
90
|
+
warn_unreachable = true
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Copyright 2026 Captain James
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
"""wesley-python: a generic Wesley target that emits standard-library Python.
|
|
4
|
+
|
|
5
|
+
The syntax API is in :mod:`wesley_python.types` and :mod:`wesley_python.syntax`; the plain-GraphQL
|
|
6
|
+
emitter is in :mod:`wesley_python.emit`.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
__version__ = "0.1.0a1"
|