Skip to content

Commit 58227eb

Browse files
committed
refactor: Reduce size of JSON dumps by removing keys with null values
Issue-403: #403
1 parent 5c9fee2 commit 58227eb

3 files changed

Lines changed: 108 additions & 63 deletions

File tree

docs/schema.json

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,81 @@
3737
"integer",
3838
"null"
3939
]
40+
},
41+
"inherited": {
42+
"title": "Whether the alias is the member of another alias.",
43+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.inherited",
44+
"type": "boolean"
45+
},
46+
"runtime": {
47+
"title": "Whether the alias exists at runtime.",
48+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.runtime",
49+
"type": "boolean"
50+
},
51+
"public": {
52+
"title": "Whether the alias is public.",
53+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.public",
54+
"type": [
55+
"null",
56+
"boolean"
57+
]
58+
},
59+
"deprecated": {
60+
"title": "Whether the alias is explicitly marked as deprecated.",
61+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.deprecated",
62+
"type": [
63+
"null",
64+
"boolean"
65+
]
66+
},
67+
"is_public": {
68+
"title": "Whether the alias is public.",
69+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.is_public",
70+
"type": "boolean"
71+
},
72+
"is_deprecated": {
73+
"title": "Whether the alias is deprecated.",
74+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.is_deprecated",
75+
"type": "boolean"
76+
},
77+
"is_private": {
78+
"title": "Whether the alias is private.",
79+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.is_private",
80+
"type": "boolean"
81+
},
82+
"is_class_private": {
83+
"title": "Whether the alias is class-private.",
84+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.is_class_private",
85+
"type": "boolean"
86+
},
87+
"is_special": {
88+
"title": "Whether the alias is special.",
89+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.is_special",
90+
"type": "boolean"
91+
},
92+
"is_imported": {
93+
"title": "Whether the alias is imported.",
94+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.is_imported",
95+
"type": "boolean"
96+
},
97+
"is_exported": {
98+
"title": "Whether the alias is exported.",
99+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.is_exported",
100+
"type": "boolean"
101+
},
102+
"is_wildcard_exposed": {
103+
"title": "Whether the alias is wildcard-exposed.",
104+
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/alias/#griffe.Alias.is_wildcard_exposed",
105+
"type": "boolean"
40106
}
41107
},
42108
"additionalProperties": false,
43109
"required": [
44110
"name",
45111
"kind",
46-
"path",
47112
"target_path",
48-
"lineno"
113+
"runtime",
114+
"inherited"
49115
]
50116
},
51117
{
@@ -257,11 +323,6 @@
257323
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/#griffe.Object.is_wildcard_exposed",
258324
"type": "boolean"
259325
},
260-
"is_inherited": {
261-
"title": "Whether the object is inherited.",
262-
"markdownDescription": "https://mkdocstrings.github.io/griffe/reference/api/models/#griffe.Object.is_inherited",
263-
"type": "boolean"
264-
},
265326
"bases": true,
266327
"decorators": true,
267328
"parameters": true,
@@ -274,15 +335,7 @@
274335
"required": [
275336
"name",
276337
"kind",
277-
"path",
278-
"filepath",
279-
"relative_filepath",
280-
"relative_package_filepath",
281-
"public",
282-
"runtime",
283-
"deprecated",
284-
"labels",
285-
"members"
338+
"runtime"
286339
],
287340
"allOf": [
288341
{

src/griffe/_internal/encoders.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def _attach_parent_to_exprs(obj: Class | Function | Attribute | TypeAlias, paren
184184
def _load_module(obj_dict: dict[str, Any]) -> Module:
185185
module = Module(
186186
name=obj_dict["name"],
187-
filepath=Path(obj_dict["filepath"]),
187+
filepath=Path(obj_dict["filepath"]) if "filepath" in obj_dict else None,
188188
docstring=_load_docstring(obj_dict),
189189
runtime=obj_dict.get("runtime", True),
190190
)
@@ -268,12 +268,17 @@ def _load_attribute(obj_dict: dict[str, Any]) -> Attribute:
268268

269269

270270
def _load_alias(obj_dict: dict[str, Any]) -> Alias:
271-
return Alias(
271+
alias = Alias(
272272
name=obj_dict["name"],
273273
target=obj_dict["target_path"],
274274
lineno=obj_dict["lineno"],
275275
endlineno=obj_dict.get("endlineno"),
276+
runtime=obj_dict.get("runtime", True),
277+
inherited=obj_dict.get("inherited", False),
276278
)
279+
alias.public = obj_dict.get("public")
280+
alias.deprecated = obj_dict.get("deprecated")
281+
return alias
277282

278283

279284
def _load_type_alias(obj_dict: dict[str, Any]) -> TypeAlias:

src/griffe/_internal/models.py

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,14 +1193,31 @@ def as_dict(self, *, full: bool = False, **kwargs: Any) -> dict[str, Any]:
11931193
base: dict[str, Any] = {
11941194
"kind": self.kind,
11951195
"name": self.name,
1196-
"public": self.public,
1197-
"exports": self.exports and [str(export) for export in self.exports],
1198-
"imports": self.imports,
11991196
"runtime": self.runtime,
1200-
"deprecated": self.deprecated,
1201-
# TODO: Include `self.extra`?
12021197
}
12031198

1199+
if self.public is not None:
1200+
base["public"] = self.public
1201+
if self.exports is not None:
1202+
base["exports"] = [str(export) for export in self.exports]
1203+
if self.imports:
1204+
base["imports"] = self.imports
1205+
if self.deprecated is not None:
1206+
base["deprecated"] = self.deprecated
1207+
if self.lineno is not None:
1208+
base["lineno"] = self.lineno
1209+
if self.endlineno is not None:
1210+
base["endlineno"] = self.endlineno
1211+
if self.docstring:
1212+
base["docstring"] = self.docstring
1213+
if self.type_parameters:
1214+
base["type_parameters"] = [type_param.as_dict(**kwargs) for type_param in self.type_parameters]
1215+
if self.labels:
1216+
base["labels"] = self.labels
1217+
if self.members:
1218+
base["members"] = {name: member.as_dict(full=full, **kwargs) for name, member in self.members.items()}
1219+
# TODO: Include `self.extra`?
1220+
12041221
if full:
12051222
base.update(
12061223
{
@@ -1216,7 +1233,6 @@ def as_dict(self, *, full: bool = False, **kwargs: Any) -> dict[str, Any]:
12161233
"is_imported": self.is_imported,
12171234
"is_exported": self.is_exported,
12181235
"is_wildcard_exposed": self.is_wildcard_exposed,
1219-
"is_inherited": self.inherited,
12201236
# TODO: Add these properties?
12211237
# "is_alias": self.is_alias,
12221238
# "is_collection": self.is_collection,
@@ -1235,18 +1251,6 @@ def as_dict(self, *, full: bool = False, **kwargs: Any) -> dict[str, Any]:
12351251
},
12361252
)
12371253

1238-
if self.lineno is not None:
1239-
base["lineno"] = self.lineno
1240-
if self.endlineno is not None:
1241-
base["endlineno"] = self.endlineno
1242-
if self.docstring:
1243-
base["docstring"] = self.docstring
1244-
if self.type_parameters:
1245-
base["type_parameters"] = [type_param.as_dict(**kwargs) for type_param in self.type_parameters]
1246-
1247-
base["labels"] = self.labels
1248-
base["members"] = {name: member.as_dict(full=full, **kwargs) for name, member in self.members.items()}
1249-
12501254
return base
12511255

12521256

@@ -2077,19 +2081,23 @@ def as_dict(self, *, full: bool = False, **kwargs: Any) -> dict[str, Any]: # no
20772081
"kind": Kind.ALIAS,
20782082
"name": self.name,
20792083
"target_path": self.target_path,
2080-
"public": self.public,
20812084
"runtime": self.runtime,
20822085
"inherited": self.inherited,
2083-
"deprecated": self.deprecated,
20842086
}
20852087

2088+
if self.public is not None:
2089+
base["public"] = self.public
2090+
if self.deprecated is not None:
2091+
base["deprecated"] = self.deprecated
2092+
if self.alias_lineno:
2093+
base["lineno"] = self.alias_lineno
2094+
if self.alias_endlineno:
2095+
base["endlineno"] = self.alias_endlineno
2096+
20862097
if full:
20872098
base.update(
20882099
{
20892100
"path": self.path,
2090-
"filepath": self.filepath,
2091-
"relative_filepath": self.relative_filepath,
2092-
"relative_package_filepath": self.relative_package_filepath,
20932101
"is_public": self.is_public,
20942102
"is_deprecated": self.is_deprecated,
20952103
"is_private": self.is_private,
@@ -2098,30 +2106,9 @@ def as_dict(self, *, full: bool = False, **kwargs: Any) -> dict[str, Any]: # no
20982106
"is_imported": self.is_imported,
20992107
"is_exported": self.is_exported,
21002108
"is_wildcard_exposed": self.is_wildcard_exposed,
2101-
"is_inherited": self.inherited,
2102-
# TODO: Add these properties?
2103-
# "is_alias": self.is_alias,
2104-
# "is_collection": self.is_collection,
2105-
# "is_module": self.is_module,
2106-
# "is_class": self.is_class,
2107-
# "is_function": self.is_function,
2108-
# "is_attribute": self.is_attribute,
2109-
# "is_type_alias": self.is_type_alias,
2110-
# "is_init_module": self.is_init_module,
2111-
# "is_package": self.is_package,
2112-
# "is_subpackage": self.is_subpackage,
2113-
# "is_namespace_package": self.is_namespace_package,
2114-
# "is_namespace_subpackage": self.is_namespace_subpackage,
2115-
# "has_docstring": self.has_docstring,
2116-
# "has_docstrings": self.has_docstrings,
2117-
}
2109+
},
21182110
)
21192111

2120-
if self.alias_lineno:
2121-
base["lineno"] = self.alias_lineno
2122-
if self.alias_endlineno:
2123-
base["endlineno"] = self.alias_endlineno
2124-
21252112
return base
21262113

21272114

0 commit comments

Comments
 (0)