Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autodoc: only mock specified modules with their descendants. #4413

Merged
merged 1 commit into from
Jan 14, 2018

Conversation

rjarry
Copy link
Contributor

@rjarry rjarry commented Jan 11, 2018

Do not mock the ancestors of the specified modules in autodoc_mock_imports. Only mock the modules themselves and their descendants (as specified in the docs). This changes behavior compared to the original mock import implementation that was done in version 1.3 where all ancestors were also mocked implicitly (may require warning in release notes?).

I fixed the test configs accordingly.

Fixes #2557

@rjarry rjarry changed the title autodoc: fix mock import ancestors autodoc: only mock specified modules with their descendants. Jan 11, 2018
Copy link
Member

@tk0miya tk0miya left a comment

Choose a reason for hiding this comment

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

As commented at #2557, we need to add a new configuration to switch old and new behaviors to keep compatibility.
And make it new behavior by default :-)

@@ -84,13 +84,13 @@ def __getattr__(self, name):
class _MockImporter(object):
def __init__(self, names):
# type: (List[str]) -> None
self.base_packages = set() # type: Set[str]
for n in names:
self.base_packages = set() # type: Set[Tuple[str, ...]]
Copy link
Member

Choose a reason for hiding this comment

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

Is this base package? It is better to me to rename like target_packages this because the purpose is changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

package = tuple(name.split('.'))
# check if name is (or is a descendant of) one of our base_packages
for pkg in self.base_packages:
if package[:len(pkg)] == pkg:
Copy link
Member

Choose a reason for hiding this comment

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

I feel comparing package[:len(pkg)] is a bit unreadable.
How about this?

# self.target_packages contains package names as string (not tuple).
for pkg in self.target_packages:
    if name == pkg or name.startswith(pkg + '.'):
        return self

try:
importer = _MockImporter(names)
yield
finally:
importer.disable()
if importer is not None:
Copy link
Member

Choose a reason for hiding this comment

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

Why did you check importer here? Did you mean instantiation of _MockImporter might be failed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I put the explanation in the git commit message:

fe24d5c

Copy link
Member

Choose a reason for hiding this comment

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

Oh, sorry. I'd overlooked it. I understand.

'missing_package3.missing_module3',
'missing_package1',
'missing_package2',
'missing_package3',
Copy link
Member

Choose a reason for hiding this comment

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

We have to add existing_package.missing_module pattern.

@tk0miya
Copy link
Member

tk0miya commented Jan 12, 2018

In addition, we have feature freeze for 1.7 release in this weekend.
Can you work until then? If not, I'd like to modify this.

@rjarry
Copy link
Contributor Author

rjarry commented Jan 12, 2018

I'm not sure I'll have time to work on it before this weekend. If you want to take over my draft, please do. I don't mind :)

Do not mock the ancestors of the specified modules in
autodoc_mock_imports. Only mock the modules themselves and their
descendants (as specified in the docs).

Fix the test configs accordingly.

Signed-off-by: Robin Jarry <robin@jarry.cc>
@rjarry rjarry force-pushed the autodoc-mock-imports branch from fe24d5c to c24dffc Compare January 13, 2018 16:44
@rjarry
Copy link
Contributor Author

rjarry commented Jan 13, 2018

@tk0miya I pushed the changes you suggested. Tell me what you think :)

@tk0miya tk0miya added this to the 1.7 milestone Jan 13, 2018
@tk0miya
Copy link
Member

tk0miya commented Jan 13, 2018

As commented at #2557, we need to add a new configuration to switch old and new behaviors to keep compatibility.
And make it new behavior by default :-)

we need to add this for backward compatibility. Can you work it?

To do that, we have to give an option to _MockImporter.

mocker = _MockImporter(names, mock_ascendants=True)
...
mocker.disable()

and give an option from autodoc side:

with mock(names, config.autodoc_mock_ascendants):
    ...

(I don't have time now, so the naming is very roughly)

@rjarry
Copy link
Contributor Author

rjarry commented Jan 13, 2018

I'm not sure adding a new option is a good idea.

In fact we are only fixing bad behavior. If some users are relying on this corner case, they can can change autodoc_mock_imports to directly specify base packages instead of sub modules.

@tk0miya
Copy link
Member

tk0miya commented Jan 14, 2018

Absolutely, adding base package will resolve it instead option.
Okay, I'll mention that incompatible change in CHANGES.

@rjarry
Copy link
Contributor Author

rjarry commented Jan 14, 2018

Thanks @tk0miya!

@tk0miya tk0miya merged commit b296487 into sphinx-doc:master Jan 14, 2018
@tk0miya
Copy link
Member

tk0miya commented Jan 14, 2018

Merged. Thank you for great work!

tk0miya added a commit that referenced this pull request Jan 14, 2018
@tk0miya
Copy link
Member

tk0miya commented Jan 14, 2018

Now I added an incompatible entry for this: 0d61b25.
Please let me know if wrong (I'm not a native and not good at English :-p)

@rjarry
Copy link
Contributor Author

rjarry commented Jan 16, 2018

Don't worry, I'm not a native either :)

I added a comment on the commit 0d61b25

Thanks @tk0miya!

@rjarry rjarry deleted the autodoc-mock-imports branch June 18, 2018 11:28
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

autodoc mock imports the entire module hierarchy when mock importing a submodule
2 participants