Skip to content

Commit

Permalink
Explicitly treat ObjectDescription._doc_field_type_map as an instance…
Browse files Browse the repository at this point in the history
… variable

- Aims to address [issue sphinx-doc#6478](sphinx-doc#6478)

- Left ObjectDescription._doc_field_type_map declaration alone and in place in
  case there are other factors at play that require it declared on the class.

- In ObjectDescription constructor, explicitly set _doc_field_type_map's value
  as an instance variable.

- This presumably needs to be built with every instantiation of an
  ObjectDescription or inheriting class, so we're not attempting to "define it
  once on a class and then use it"... that approach seems much harder to reason
  about and get correctly done (as was maybe demonstrated by the refactoring
  that lead to this problem in the linked-to issue).

- Runs through the test suite clean locally except for an unrelated single test
  failure on an image_converter test, which also failed on the base branch
  before my changes here.
  • Loading branch information
ViktorHaag committed Jun 13, 2019
1 parent 5b307fc commit 3973e89
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions sphinx/directives/__init__.py
Expand Up @@ -70,18 +70,23 @@ class ObjectDescription(SphinxDirective):
# Warning: this might be removed in future version. Don't touch this from extensions.
_doc_field_type_map = {} # type: Dict[str, Tuple[Field, bool]]

def _process_type_map(self, typelist):
typemap = {}
for field in typelist:
for name in field.names:
typemap[name] = (field, False)
if field.is_typed:
typed_field = cast(TypedField, field)
for name in typed_field.typenames:
typemap[name] = (field, True)
return typemap

def __init__(self, *args, **kwargs):
SphinxDirective.__init__(self, *args, **kwargs)
self._doc_field_type_map = self._process_type_map(self.doc_field_types)

def get_field_type_map(self):
# type: () -> Dict[str, Tuple[Field, bool]]
if self._doc_field_type_map == {}:
for field in self.doc_field_types:
for name in field.names:
self._doc_field_type_map[name] = (field, False)

if field.is_typed:
typed_field = cast(TypedField, field)
for name in typed_field.typenames:
self._doc_field_type_map[name] = (field, True)

return self._doc_field_type_map

def get_signatures(self):
Expand Down

0 comments on commit 3973e89

Please sign in to comment.