Skip to content

[MRG] Fixes cross-reference and no reference issues in sphinx#13000

Merged
adrinjalali merged 25 commits intoscikit-learn:masterfrom
thomasjpfan:more_one_target_cross_reference
Feb 28, 2019
Merged

[MRG] Fixes cross-reference and no reference issues in sphinx#13000
adrinjalali merged 25 commits intoscikit-learn:masterfrom
thomasjpfan:more_one_target_cross_reference

Conversation

@thomasjpfan
Copy link
Copy Markdown
Member

@thomasjpfan thomasjpfan commented Jan 17, 2019

Reference Issues/PRs

Fixes #12802

What does this implement/fix? Explain your changes.

Overrides the default ReferencesResolver with a CustomReferencesResolver. The custom resolver does two things:

  1. If running in the context of a python class, it places the py domain above the std domain, allowing class methods to be referenced first.
  2. Removes the logging when there is more than one reference.

Any other comments?

This together with #12970 should resolve most of the warnings that appear in sphinx when we switched the default role to "any".

@jnothman
Copy link
Copy Markdown
Member

Hmmm... are you sure this is what you mean? This will override the global default role so that only term and only meth can be respectively linked in these contexts through single backticks without a role being specified (e.g. :meth:). What I think you want is to change the priority in different contexts. That will be done only by redefining the any role, I think.

@thomasjpfan thomasjpfan changed the title [MRG] Fixes cross-reference issues in sphinx [WIP] Fixes cross-reference issues in sphinx Jan 17, 2019
@thomasjpfan
Copy link
Copy Markdown
Member Author

Yea, I wanted to change the priority order. It looks like the order for 'any' is defined in ReferencesResolver.resolve_anyref. The std domain takes priority.

@jnothman
Copy link
Copy Markdown
Member

jnothman commented Jan 18, 2019 via email

@thomasjpfan thomasjpfan changed the title [WIP] Fixes cross-reference issues in sphinx [MRG] Fixes cross-reference issues in sphinx Jan 19, 2019
@thomasjpfan
Copy link
Copy Markdown
Member Author

thomasjpfan commented Jan 19, 2019

This PR was updated to override the default ReferencesResolver with a CustomReferencesResolver. The custom resolver does two things:

  1. If running in the context of a python class, it places the py domain above the std domain, allowing class methods to be referenced first.
  2. Removes the logging when there is more than one reference.

Codewise, it adds the following few lines to the default ReferencesResolver.resolve_anyref:

with suppress(KeyError):
    py_domain = self.env.domains['py']
    results.extend(py_domain.resolve_any_xref(
        self.env, refdoc, self.app.builder, target, node, contnode))

and removes the logging that has to do with more than one reference.

@thomasjpfan
Copy link
Copy Markdown
Member Author

This PR was updated with a refactor to clean up the code and make it easier to review. If a reference is found, it will immediately be processed into a node and returned.

@jnothman
Copy link
Copy Markdown
Member

jnothman commented Jan 20, 2019 via email

@thomasjpfan
Copy link
Copy Markdown
Member Author

It prioritizes references in class docstrings to reference itself. Here are a few examples:

  1. SimpleImputer: PR and master with fit and transform.
  2. StandardScaler: PR and master with transform.
  3. LatentDirichletAllocation: PR and master with fit and partial_fit.
  4. GaussianNB: PR and master with partial_fit.

On master the links point to the glossary. With this PR, they point to the class methods.

@jnothman
Copy link
Copy Markdown
Member

Looks nice (not looked at code yet)! I have a small concern about referencing base estimator methods in meta-estimator documentation where, if you want a link at all, it is not to the local fit etc, but perhaps to term. But at least there are ways to specify that.

@thomasjpfan
Copy link
Copy Markdown
Member Author

I have a small concern about referencing base estimator methods

This is true, this PR's ColumnTransformer, links fit and transform to its own function, and not the glossary. Since these meta-estimator links are the special case, I think adding term to these references would be preferred.

target = node['reftarget']

# first, try resolving as :doc:
doc_ref = stddomain.resolve_xref(self.env, refdoc, self.app.builder,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we really ever want to link to :doc:, do we?

@@ -0,0 +1,83 @@
"""Adapted from
sphinx.transforms.post_transforms.ReferencesResolver.resolve_anyref
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include any relevant licensing information to avoid breach of terms.

return self.create_node(('doc', doc_ref))

# process 'py' domain first for python classes
if "py:class" in node:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice.


def create_node(self, result):
res_role, newnode = result
# Override "any" class with the actual role type to get the styling
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!! One confusing thing is that :term: does not generally require monospace, but some terms are explicitly formatted with double backticks in the glossary (but the link target does not include backticks). I assume we're not really able to make that work nicely. No big deal.

I can't see what code controls styling when there's no match.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the reference is missing the missing-reference event is called. This is handled in another PR: https://github.com/scikit-learn/scikit-learn/pull/12970/files. In that PR, the single backticks are treated as double backticks when there are not matches.

@thomasjpfan thomasjpfan changed the title [MRG] Fixes cross-reference issues in sphinx [MRG] Fixes cross-reference and no reference issues in sphinx Jan 21, 2019
@thomasjpfan
Copy link
Copy Markdown
Member Author

This PR was updated to address the "no reference" case. If no reference is found, the single backticked strings will be displayed as <code>. This way, #12970 is no longer needed.

@thomasjpfan
Copy link
Copy Markdown
Member Author

This PR was updated to address the "no reference" case. Codewise, the following was added:

    def resolve_anyref(self, refdoc, node, contnode):
        ...
        # no results considered to be <code>
        contnode['classes'] = []
        return contnode

If no reference is found, the single backticked strings will be displayed as <code>.

Copy link
Copy Markdown
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, but I'm not sure if we should continue calling this 'any'. Is it easy to rename the default role to show maintainers that it's different?

post_transforms[i] = CustomReferencesResolver
break
else:
raise RuntimeError
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might add a message: ReferencesResolver not found

@thomasjpfan
Copy link
Copy Markdown
Member Author

If we wish to change the default role's name, we would most likely need to create a custom role and a corresponding domain. This would require a different approach than this PR.

A comment was added to the default_role='any' config option to communicate the changes in this PR.

Copy link
Copy Markdown
Member

@jnothman jnothman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@thomasjpfan
Copy link
Copy Markdown
Member Author

@amueller This PR enables the following:

  • Fixes the "no reference" and cross-reference warnings when the docs are generated.
  • Configures "single backticks with no reference" to be treated as code blocks (or "double backticks"). - Gives higher priority to Python class methods. If fit can reference a python class method, it will reference that class method instead of the glossary.

By setting ``remainder`` to be an estimator, the remaining
non-specified columns will use the ``remainder`` estimator. The
estimator must support `fit` and `transform`.
estimator must support :term:`fit` and :term:`transform`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove :term: please?

@adrinjalali adrinjalali merged commit db2290c into scikit-learn:master Feb 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
…earn#13000)

* DOC: Fixes cross references

* RFC: Minimizes diffs

* REV: Removes default-role

* ENH: Process python domain first

* ENH: Support sphinx 1.6.2

* RFC: Only reorder for python classes

* RFC: Adds early exit

* DOC: Uses term for ColumnTransformer

* REV: Removes styling from term

* ENH: Handle no references case

* ENH: Adds RuntimeError

* RFC: Adds runtime error message

* DOC: Adds comment on default_role

* CLN Removes term
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
…earn#13000)

* DOC: Fixes cross references

* RFC: Minimizes diffs

* REV: Removes default-role

* ENH: Process python domain first

* ENH: Support sphinx 1.6.2

* RFC: Only reorder for python classes

* RFC: Adds early exit

* DOC: Uses term for ColumnTransformer

* REV: Removes styling from term

* ENH: Handle no references case

* ENH: Adds RuntimeError

* RFC: Adds runtime error message

* DOC: Adds comment on default_role

* CLN Removes term
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix 'any' doc build warnings

3 participants