incur-cli 0.10.0__py3-none-any.whl
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.
- incur/__init__.py +62 -0
- incur/_cli.py +2139 -0
- incur/_command.py +426 -0
- incur/_completions.py +170 -0
- incur/_errors.py +234 -0
- incur/_filter.py +173 -0
- incur/_formatter.py +112 -0
- incur/_gateway.py +173 -0
- incur/_help.py +260 -0
- incur/_http.py +389 -0
- incur/_js.py +467 -0
- incur/_jsonschema.py +320 -0
- incur/_mcp.py +872 -0
- incur/_mcpsource.py +226 -0
- incur/_model.py +348 -0
- incur/_openapi.py +640 -0
- incur/_parser.py +347 -0
- incur/_pm.py +49 -0
- incur/_schema.py +555 -0
- incur/_serve_http.py +236 -0
- incur/_skill.py +355 -0
- incur/_sync.py +403 -0
- incur/_syncmcp.py +188 -0
- incur/_tokens.py +130 -0
- incur/_toon.py +202 -0
- incur/_tree.py +273 -0
- incur/_update.py +373 -0
- incur/_yaml.py +651 -0
- incur/py.typed +0 -0
- incur_cli-0.10.0.dist-info/METADATA +376 -0
- incur_cli-0.10.0.dist-info/RECORD +32 -0
- incur_cli-0.10.0.dist-info/WHEEL +4 -0
incur/_jsonschema.py
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"""JSON Schema to schema nodes, as Zod's `z.fromJSONSchema` converts them.
|
|
2
|
+
|
|
3
|
+
Generated OpenAPI and MCP-source commands build their input from JSON Schema. Each node keeps
|
|
4
|
+
the JSON Schema that Zod would emit for it (`Type.raw`), because that output follows Zod's
|
|
5
|
+
wrapper rules: a wrapper's own keys, such as `default` or a description added after
|
|
6
|
+
`.optional()`, come before the keys of the schema it wraps.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from dataclasses import replace
|
|
12
|
+
|
|
13
|
+
from ._js import Json
|
|
14
|
+
from ._schema import ISO_DATETIME, MAX_SAFE_INTEGER, Field, Kind, Type, is_number
|
|
15
|
+
|
|
16
|
+
#: Keys Zod's converter reads; any other key is kept as metadata.
|
|
17
|
+
_RECOGNIZED = frozenset(
|
|
18
|
+
{
|
|
19
|
+
"$schema",
|
|
20
|
+
"$ref",
|
|
21
|
+
"$defs",
|
|
22
|
+
"definitions",
|
|
23
|
+
"$id",
|
|
24
|
+
"id",
|
|
25
|
+
"$comment",
|
|
26
|
+
"$anchor",
|
|
27
|
+
"$vocabulary",
|
|
28
|
+
"$dynamicRef",
|
|
29
|
+
"$dynamicAnchor",
|
|
30
|
+
"type",
|
|
31
|
+
"enum",
|
|
32
|
+
"const",
|
|
33
|
+
"anyOf",
|
|
34
|
+
"oneOf",
|
|
35
|
+
"allOf",
|
|
36
|
+
"not",
|
|
37
|
+
"properties",
|
|
38
|
+
"required",
|
|
39
|
+
"additionalProperties",
|
|
40
|
+
"patternProperties",
|
|
41
|
+
"propertyNames",
|
|
42
|
+
"minProperties",
|
|
43
|
+
"maxProperties",
|
|
44
|
+
"items",
|
|
45
|
+
"prefixItems",
|
|
46
|
+
"additionalItems",
|
|
47
|
+
"minItems",
|
|
48
|
+
"maxItems",
|
|
49
|
+
"uniqueItems",
|
|
50
|
+
"contains",
|
|
51
|
+
"minContains",
|
|
52
|
+
"maxContains",
|
|
53
|
+
"minLength",
|
|
54
|
+
"maxLength",
|
|
55
|
+
"pattern",
|
|
56
|
+
"format",
|
|
57
|
+
"minimum",
|
|
58
|
+
"maximum",
|
|
59
|
+
"exclusiveMinimum",
|
|
60
|
+
"exclusiveMaximum",
|
|
61
|
+
"multipleOf",
|
|
62
|
+
"description",
|
|
63
|
+
"default",
|
|
64
|
+
"contentEncoding",
|
|
65
|
+
"contentMediaType",
|
|
66
|
+
"contentSchema",
|
|
67
|
+
"unevaluatedItems",
|
|
68
|
+
"unevaluatedProperties",
|
|
69
|
+
"if",
|
|
70
|
+
"then",
|
|
71
|
+
"else",
|
|
72
|
+
"dependentSchemas",
|
|
73
|
+
"dependentRequired",
|
|
74
|
+
"nullable",
|
|
75
|
+
"readOnly",
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
#: Recognized keys that Zod still keeps as metadata.
|
|
79
|
+
_KEPT = ("$id", "id", "$comment", "$anchor", "$vocabulary", "$dynamicRef", "$dynamicAnchor")
|
|
80
|
+
_CONTENT = ("contentEncoding", "contentMediaType", "contentSchema")
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _number(value: Json | None) -> float | None:
|
|
84
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
85
|
+
return None
|
|
86
|
+
return value
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def convert(schema: Json) -> Type:
|
|
90
|
+
"""Converts a JSON Schema to a schema node, as `z.fromJSONSchema` does."""
|
|
91
|
+
if not isinstance(schema, dict):
|
|
92
|
+
return Type("unknown", raw={})
|
|
93
|
+
ty = _compose(schema, _base(schema))
|
|
94
|
+
extra = {k: schema[k] for k in (*_KEPT, *_CONTENT) if k in schema}
|
|
95
|
+
extra.update((k, v) for k, v in schema.items() if k not in _RECOGNIZED)
|
|
96
|
+
if not extra:
|
|
97
|
+
return ty
|
|
98
|
+
raw = ty.raw or {}
|
|
99
|
+
deprecated = extra.get("deprecated") is True
|
|
100
|
+
if ty.default is not None:
|
|
101
|
+
rest = {k: v for k, v in raw.items() if k != "default" and k not in extra}
|
|
102
|
+
raw = {"default": ty.default, **extra, **rest}
|
|
103
|
+
return replace(ty, raw=raw, extra=extra, deprecated=deprecated)
|
|
104
|
+
# Zod stores the metadata in place of the description, which is lost.
|
|
105
|
+
raw = {**{k: v for k, v in raw.items() if k != "description"}, **extra}
|
|
106
|
+
return replace(ty, raw=raw, extra=extra, deprecated=deprecated, description=None)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _compose(schema: dict[str, Json], ty: Type) -> Type:
|
|
110
|
+
"""Applies `anyOf`, `oneOf`, and `allOf`: each replaces an untyped base or intersects it."""
|
|
111
|
+
explicit = schema.get("type") not in (None, "") or "enum" in schema or "const" in schema
|
|
112
|
+
for key in ("anyOf", "oneOf"):
|
|
113
|
+
options = schema.get(key)
|
|
114
|
+
if isinstance(options, list):
|
|
115
|
+
variants = [convert(o) for o in options]
|
|
116
|
+
raws: list[Json] = [v.raw for v in variants]
|
|
117
|
+
union = Type("union", variants=variants, exclusive=key == "oneOf", raw={key: raws})
|
|
118
|
+
ty = _intersect(ty, union) if explicit else union
|
|
119
|
+
members = schema.get("allOf")
|
|
120
|
+
if isinstance(members, list):
|
|
121
|
+
if not members:
|
|
122
|
+
return ty if explicit else Type("unknown", raw={})
|
|
123
|
+
if not explicit:
|
|
124
|
+
ty = convert(members[0])
|
|
125
|
+
for member in members if explicit else members[1:]:
|
|
126
|
+
ty = _intersect(ty, convert(member))
|
|
127
|
+
return ty
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _intersect(left: Type, right: Type) -> Type:
|
|
131
|
+
"""Intersects two schemas, as `z.intersection` does, flattening plain `allOf` lists."""
|
|
132
|
+
raws: list[Json] = []
|
|
133
|
+
for side in (left.raw or {}, right.raw or {}):
|
|
134
|
+
allof = side.get("allOf")
|
|
135
|
+
raws += allof if list(side) == ["allOf"] and isinstance(allof, list) else [side]
|
|
136
|
+
return Type("intersection", variants=[left, right], raw={"allOf": raws})
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _base(schema: dict[str, Json]) -> Type:
|
|
140
|
+
"""Converts the type-defining keys, then applies the description and default."""
|
|
141
|
+
kind = schema.get("type")
|
|
142
|
+
enum = schema.get("enum")
|
|
143
|
+
const = schema.get("const", ...)
|
|
144
|
+
if isinstance(enum, list) and len(enum) == 1:
|
|
145
|
+
const = enum[0]
|
|
146
|
+
# Enums, constants, and type unions skip the description and default, as Zod's converter
|
|
147
|
+
# returns them early.
|
|
148
|
+
if isinstance(enum, list) and len(enum) > 1:
|
|
149
|
+
strings = [v for v in enum if isinstance(v, str)]
|
|
150
|
+
if len(strings) == len(enum):
|
|
151
|
+
return Type("enum", values=strings, raw={"type": "string", "enum": list[Json](strings)})
|
|
152
|
+
literals = [_literal(v) for v in enum]
|
|
153
|
+
raws: list[Json] = [v.raw for v in literals]
|
|
154
|
+
return Type("union", variants=literals, raw={"anyOf": raws})
|
|
155
|
+
if const is not ...:
|
|
156
|
+
if isinstance(const, str):
|
|
157
|
+
return Type("enum", values=[const], raw={"type": "string", "const": const})
|
|
158
|
+
return _literal(const)
|
|
159
|
+
if isinstance(kind, list):
|
|
160
|
+
variants = [_base({**schema, "type": t}) for t in kind]
|
|
161
|
+
if len(variants) == 1:
|
|
162
|
+
return variants[0]
|
|
163
|
+
raws: list[Json] = [v.raw for v in variants if v.raw is not None]
|
|
164
|
+
return Type("union", variants=variants, raw={"anyOf": raws})
|
|
165
|
+
if kind == "string":
|
|
166
|
+
ty = _string(schema)
|
|
167
|
+
elif kind in ("number", "integer"):
|
|
168
|
+
ty = _numeric(schema, "integer" if kind == "integer" else "number")
|
|
169
|
+
elif kind in ("boolean", "null"):
|
|
170
|
+
ty = Type("boolean" if kind == "boolean" else "null", raw={"type": kind})
|
|
171
|
+
elif kind == "object":
|
|
172
|
+
ty = _object(schema)
|
|
173
|
+
elif kind == "array":
|
|
174
|
+
ty = _array(schema)
|
|
175
|
+
else:
|
|
176
|
+
ty = Type("unknown", raw={})
|
|
177
|
+
description = schema.get("description")
|
|
178
|
+
if isinstance(description, str) and description:
|
|
179
|
+
ty = describe(ty, description)
|
|
180
|
+
if "default" in schema and schema["default"] is not None:
|
|
181
|
+
default = schema["default"]
|
|
182
|
+
assert ty.raw is not None
|
|
183
|
+
ty = replace(ty, default=default, raw={"default": default, **ty.raw})
|
|
184
|
+
return ty
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _literal(value: Json) -> Type:
|
|
188
|
+
"""A schema that accepts one value, as `z.literal` does."""
|
|
189
|
+
if value is None:
|
|
190
|
+
kind = "null"
|
|
191
|
+
elif isinstance(value, bool):
|
|
192
|
+
kind = "boolean"
|
|
193
|
+
elif is_number(value):
|
|
194
|
+
kind = "number"
|
|
195
|
+
else:
|
|
196
|
+
kind = "string" if isinstance(value, str) else "object"
|
|
197
|
+
return Type("literal", literal=value, raw={"type": kind, "const": value})
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _string(schema: dict[str, Json]) -> Type:
|
|
201
|
+
low, high = _number(schema.get("minLength")), _number(schema.get("maxLength"))
|
|
202
|
+
pattern = schema.get("pattern")
|
|
203
|
+
pattern = pattern if isinstance(pattern, str) and pattern else None
|
|
204
|
+
date_time = schema.get("format") == "date-time"
|
|
205
|
+
raw: dict[str, Json] = {"type": "string"}
|
|
206
|
+
if low is not None:
|
|
207
|
+
raw["minLength"] = low
|
|
208
|
+
if high is not None:
|
|
209
|
+
raw["maxLength"] = high
|
|
210
|
+
patterns = [p for p in (ISO_DATETIME if date_time else None, pattern) if p is not None]
|
|
211
|
+
# A pattern check replaces the format Zod reports.
|
|
212
|
+
if date_time and pattern is None:
|
|
213
|
+
raw["format"] = "date-time"
|
|
214
|
+
if len(patterns) == 1:
|
|
215
|
+
raw["pattern"] = patterns[0]
|
|
216
|
+
elif patterns:
|
|
217
|
+
raw["allOf"] = [{"pattern": p} for p in patterns]
|
|
218
|
+
return Type(
|
|
219
|
+
"string",
|
|
220
|
+
min=low,
|
|
221
|
+
max=high,
|
|
222
|
+
pattern=pattern,
|
|
223
|
+
format="date-time" if date_time else None,
|
|
224
|
+
raw=raw,
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _numeric(schema: dict[str, Json], kind: Kind) -> Type:
|
|
229
|
+
low, high = _number(schema.get("minimum")), _number(schema.get("maximum"))
|
|
230
|
+
raw: dict[str, Json] = {"type": kind}
|
|
231
|
+
if kind == "integer":
|
|
232
|
+
raw["minimum"] = -MAX_SAFE_INTEGER if low is None else max(low, -MAX_SAFE_INTEGER)
|
|
233
|
+
raw["maximum"] = MAX_SAFE_INTEGER if high is None else min(high, MAX_SAFE_INTEGER)
|
|
234
|
+
else:
|
|
235
|
+
if low is not None:
|
|
236
|
+
raw["minimum"] = low
|
|
237
|
+
if high is not None:
|
|
238
|
+
raw["maximum"] = high
|
|
239
|
+
return Type(kind, min=low, max=high, raw=raw)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def _object(schema: dict[str, Json]) -> Type:
|
|
243
|
+
properties = schema.get("properties")
|
|
244
|
+
required = schema.get("required")
|
|
245
|
+
names = set(required) if isinstance(required, list) else set[Json]()
|
|
246
|
+
fields: list[Field] = []
|
|
247
|
+
for name, prop in (properties if isinstance(properties, dict) else {}).items():
|
|
248
|
+
ty = convert(prop)
|
|
249
|
+
fields.append(Field(name, ty if name in names else optional(ty)))
|
|
250
|
+
extra = schema.get("additionalProperties")
|
|
251
|
+
raw: dict[str, Json] = {
|
|
252
|
+
"type": "object",
|
|
253
|
+
"properties": {f.name: f.ty.raw for f in fields},
|
|
254
|
+
}
|
|
255
|
+
listed: list[Json] = [f.name for f in fields if f.ty.required is not False]
|
|
256
|
+
if listed:
|
|
257
|
+
raw["required"] = listed
|
|
258
|
+
raw["additionalProperties"] = (
|
|
259
|
+
False if extra is False else convert(extra).raw if isinstance(extra, dict) else {}
|
|
260
|
+
)
|
|
261
|
+
return Type("object", fields=fields, loose=extra is not False, raw=raw)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _array(schema: dict[str, Json]) -> Type:
|
|
265
|
+
items = schema.get("items")
|
|
266
|
+
item = convert(items) if isinstance(items, dict) else Type("unknown", raw={})
|
|
267
|
+
low, high = _number(schema.get("minItems")), _number(schema.get("maxItems"))
|
|
268
|
+
raw: dict[str, Json] = {}
|
|
269
|
+
if low is not None:
|
|
270
|
+
raw["minItems"] = low
|
|
271
|
+
if high is not None:
|
|
272
|
+
raw["maxItems"] = high
|
|
273
|
+
raw.update(type="array", items=item.raw)
|
|
274
|
+
return Type("array", items=item, min=low, max=high, raw=raw)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def describe(ty: Type, text: str) -> Type:
|
|
278
|
+
"""Adds a description, as `.describe()` does.
|
|
279
|
+
|
|
280
|
+
A wrapper's own keys come first: the description of an optional schema leads, and that of a
|
|
281
|
+
schema with a default follows the default. A plain schema lists it last.
|
|
282
|
+
"""
|
|
283
|
+
raw = {k: v for k, v in (ty.raw or {}).items() if k != "description"}
|
|
284
|
+
if ty.optional:
|
|
285
|
+
raw = {"description": text, **raw}
|
|
286
|
+
elif ty.default is not None:
|
|
287
|
+
raw = {"default": ty.default, **ty.extra, "description": text, **raw}
|
|
288
|
+
else:
|
|
289
|
+
raw = {**(ty.raw or {}), "description": text}
|
|
290
|
+
return replace(ty, description=text, raw=raw)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def optional(ty: Type) -> Type:
|
|
294
|
+
"""Makes a schema optional, as `.optional()` does."""
|
|
295
|
+
return replace(ty, optional=True, required=False)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def coerce(ty: Type) -> Type:
|
|
299
|
+
"""Coerces number and boolean input from text, as incur's `coerceIfNeeded` does.
|
|
300
|
+
|
|
301
|
+
Applies to plain and optional numbers, booleans, and unions containing them; a schema with a
|
|
302
|
+
default keeps its type.
|
|
303
|
+
"""
|
|
304
|
+
if ty.default is not None:
|
|
305
|
+
return ty
|
|
306
|
+
kinds = [v.kind for v in ty.variants] if ty.kind == "union" else [ty.kind]
|
|
307
|
+
if "number" in kinds or "integer" in kinds:
|
|
308
|
+
kind: Kind = "number"
|
|
309
|
+
elif "boolean" in kinds:
|
|
310
|
+
kind = "boolean"
|
|
311
|
+
else:
|
|
312
|
+
return ty
|
|
313
|
+
coerced = Type(
|
|
314
|
+
kind,
|
|
315
|
+
coerce=True,
|
|
316
|
+
optional=ty.optional,
|
|
317
|
+
required=False if ty.optional else None,
|
|
318
|
+
raw={"type": kind},
|
|
319
|
+
)
|
|
320
|
+
return describe(coerced, ty.description) if ty.description else coerced
|