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

Check examples to be standalone #15

Open
bsipocz opened this issue Nov 21, 2017 · 11 comments
Open

Check examples to be standalone #15

bsipocz opened this issue Nov 21, 2017 · 11 comments

Comments

@bsipocz
Copy link
Member

bsipocz commented Nov 21, 2017

It would be nice to add a config option that checks whether the docstring examples are complete standalone ones without relying the namespace of the full file they're being defined.

astropy/astropy#6878 (comment)

@asmeurer
Copy link

Does anyone know of a workaround to get this behavior?

@asmeurer
Copy link

Getting this to work here is as simple as this

diff --git a/pytest_doctestplus/plugin.py b/pytest_doctestplus/plugin.py
index 6d90d71..a0c3a84 100644
--- a/pytest_doctestplus/plugin.py
+++ b/pytest_doctestplus/plugin.py
@@ -518,6 +518,7 @@ class DocTestFinderPlus(doctest.DocTestFinder):
         return True

     def find(self, obj, name=None, module=None, globs=None, extraglobs=None):
+        globs = {}
         tests = doctest.DocTestFinder.find(self, obj, name, module, globs, extraglobs)
         if hasattr(obj, '__doctest_skip__') or hasattr(obj, '__doctest_requires__'):
             if name is None and hasattr(obj, '__name__'):

So it shouldn't be hard to add a flag for that.

However, I also noticed that enabling pytest-doctestplus changes the output of things like

>>> np.array([1, 2])
array([1, 2])

to

>>> np.array([1, 2])
[1, 2]

How hard will it be to make it so that you can use doctestplus to get this "standalone namespace" behavior, and not have it change any of the output or any of the other things it does?

@asmeurer
Copy link

There's also the question of whether this makes more sense to contribute to upstream pytest instead (c.f. #100). CC @nicoddemus

@asmeurer
Copy link

(In case it wasn't clear, I'm interesting in contributing a fix for this one way or the other)

@bsipocz
Copy link
Member Author

bsipocz commented Mar 26, 2020

@asmeurer - contributions are very welcome, either here, or if it's directly upstream in pytest, that's great, too.

As we touched on it in #100, it's not yet clear who will have the capacity to make the transition of this upstream (or what wasn't mentioned there, to straight away make the separation out into one plugin that is being used by pytest by default).

@asmeurer
Copy link

Actually I really want this to be in the upstream doctest. But we can start with baby steps.

Presumably since this isn't implemented yet, putting it in pytest itself would be independent of the work in #100, assuming that is the correct path forward.

@nicoddemus
Copy link
Contributor

Indeed this feature can be added to pytest itself independently from #100. What are you folk thinking, a new option to enable this feature?

@bsipocz
Copy link
Member Author

bsipocz commented Mar 27, 2020

A new option sounds good to me, I'm afraid this cannot be the default as it would break everyone's tests.

@bsipocz
Copy link
Member Author

bsipocz commented Mar 27, 2020

The other option is to use something similar what the matplotlib plot sphinx directive does with the context. After all, I think it would me most sense to be able to control at the individual file level where to have the standalone examples.

@asmeurer
Copy link

For file-level or even docstring-level control I think it would make sense to define something in the file itself, or to put a decorator on the docstring. For my use case I want this behavior universally, so I will implement this as an ini flag. Extending it should not be difficult.

asmeurer added a commit to asmeurer/pytest that referenced this issue Mar 27, 2020
This makes doctests run in a standalone namespace. The default is False, which
does the pre-existing behavior where doctests reuse the namespace of the
module they are written in.

Fixes scientific-python/pytest-doctestplus#15.
@asmeurer
Copy link

Upstream PR over at pytest-dev/pytest#6978.

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 a pull request may close this issue.

3 participants