Skip to content

Commit b4cd428

Browse files
committed
Expect some nullable fields to be missing
Ref crystal-lang/crystal#11438
1 parent 3845d35 commit b4cd428

1 file changed

Lines changed: 24 additions & 21 deletions

File tree

mkdocstrings/handlers/crystal/items.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from functools import cached_property
1111
except ImportError:
1212
from cached_property import cached_property
13+
1314
from mkdocstrings.handlers.base import CollectionError
1415

1516
from . import crystal_html
@@ -46,9 +47,9 @@ def abs_id(self) -> str:
4647
return self.data["id"]
4748

4849
@property
49-
def doc(self) -> str:
50+
def doc(self) -> Optional[str]:
5051
"""The doc comment of this item."""
51-
return self.data["doc"]
52+
return self.data.get("doc")
5253

5354
@classmethod
5455
def _properties(cls):
@@ -169,65 +170,69 @@ def is_abstract(self) -> bool:
169170
@cached_property
170171
def constants(self) -> DocMapping[DocConstant]:
171172
"""The constants (or enum members) within this type."""
172-
return DocMapping([DocConstant(x, self, self.root) for x in self.data["constants"]])
173+
return DocMapping([DocConstant(x, self, self.root) for x in self.data.get("constants", ())])
173174

174175
@cached_property
175176
def instance_methods(self) -> DocMapping[DocInstanceMethod]:
176177
"""The instance methods within this type."""
177178
return DocMapping(
178-
[DocInstanceMethod(x, self, self.root) for x in self.data["instance_methods"]]
179+
[DocInstanceMethod(x, self, self.root) for x in self.data.get("instance_methods", ())]
179180
)
180181

181182
@cached_property
182183
def class_methods(self) -> DocMapping[DocClassMethod]:
183184
"""The class methods within this type."""
184-
return DocMapping([DocClassMethod(x, self, self.root) for x in self.data["class_methods"]])
185+
return DocMapping(
186+
[DocClassMethod(x, self, self.root) for x in self.data.get("class_methods", ())]
187+
)
185188

186189
@cached_property
187190
def constructors(self) -> DocMapping[DocConstructor]:
188191
"""The constructors within this type."""
189-
return DocMapping([DocConstructor(x, self, self.root) for x in self.data["constructors"]])
192+
return DocMapping(
193+
[DocConstructor(x, self, self.root) for x in self.data.get("constructors", ())]
194+
)
190195

191196
@cached_property
192197
def macros(self) -> DocMapping[DocMacro]:
193198
"""The macros within this type."""
194-
return DocMapping([DocMacro(x, self, self.root) for x in self.data["macros"]])
199+
return DocMapping([DocMacro(x, self, self.root) for x in self.data.get("macros", ())])
195200

196201
@cached_property
197202
def types(self) -> DocMapping[DocType]:
198203
"""The types nested in this type as a namespace."""
199-
return DocMapping([DocType(x, self, self.root) for x in self.data["types"]])
204+
return DocMapping([DocType(x, self, self.root) for x in self.data.get("types", ())])
200205

201206
@cached_property
202207
def superclass(self) -> Optional[DocPath]:
203208
"""The possible superclass of this type."""
204-
if self.data["superclass"] is not None:
209+
if self.data.get("superclass") is not None:
205210
return DocPath(self.data["superclass"], self)
206211

207212
@cached_property
208213
def ancestors(self) -> Sequence[DocPath]:
209214
"""The modules and classes this type inherited."""
210-
return [DocPath(x, self) for x in self.data["ancestors"]]
215+
return [DocPath(x, self) for x in self.data.get("ancestors", ())]
211216

212217
@cached_property
213218
def included_modules(self) -> Sequence[DocPath]:
214219
"""The modules that this type included."""
215-
return [DocPath(x, self) for x in self.data["included_modules"]]
220+
return [DocPath(x, self) for x in self.data.get("included_modules", ())]
216221

217222
@cached_property
218223
def extended_modules(self) -> Sequence[DocPath]:
219224
"""The modules that this type extended."""
220-
return [DocPath(x, self) for x in self.data["extended_modules"]]
225+
return [DocPath(x, self) for x in self.data.get("extended_modules", ())]
221226

222227
@cached_property
223228
def subclasses(self) -> Sequence[DocPath]:
224229
"""Known subclasses of this type."""
225-
return [DocPath(x, self) for x in self.data["subclasses"]]
230+
return [DocPath(x, self) for x in self.data.get("subclasses", ())]
226231

227232
@cached_property
228233
def including_types(self) -> Sequence[DocPath]:
229234
"""Known types that include this type."""
230-
return [DocPath(x, self) for x in self.data["including_types"]]
235+
return [DocPath(x, self) for x in self.data.get("including_types", ())]
231236

232237
@cached_property
233238
def locations(self) -> Sequence[DocLocation]:
@@ -318,7 +323,7 @@ class DocMethod(DocItem):
318323
def rel_id(self):
319324
d = self.data["def"]
320325

321-
args = [arg["external_name"] for arg in d["args"]]
326+
args = [arg.get("external_name", arg["name"]) for arg in d.get("args", ())]
322327
if d.get("splat_index") is not None:
323328
args[d["splat_index"]] = "*"
324329
if d.get("double_splat"):
@@ -342,7 +347,7 @@ def short_name(self):
342347
@property
343348
def is_abstract(self) -> bool:
344349
"""Whether this method is abstract."""
345-
return self.data["abstract"]
350+
return bool(self.data.get("abstract"))
346351

347352
@cached_property
348353
def args_string(self) -> crystal_html.TextWithLinks:
@@ -354,17 +359,15 @@ def args_string(self) -> crystal_html.TextWithLinks:
354359
try:
355360
html = self.data["args_html"]
356361
except KeyError:
357-
html = self.data["args_string"]
362+
html = self.data.get("args_string", "")
358363
return crystal_html.parse_crystal_html(html)
359364

360365
@cached_property
361366
def location(self) -> Optional[DocLocation]:
362367
"""[Code location][mkdocstrings.handlers.crystal.items.DocLocation] of this method. Can be `None` if unknown."""
363368
# https://github.com/crystal-lang/crystal/pull/10122
364-
try:
365-
loc = self.data["location"]
366-
except KeyError:
367-
loc = None
369+
loc = self.data.get("location")
370+
if loc is None:
368371
url = self.data.get("source_link")
369372
if url:
370373
regex = r"(?P<url>.+?/(?:blob|tree)/[^/]+/(?P<filename>.+)#L(?P<line>\d+))"

0 commit comments

Comments
 (0)