-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add an ini option doctest_standalone_namespace #6978
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,12 @@ def pytest_addoption(parser): | |
parser.addini( | ||
"doctest_encoding", "encoding used for doctest files", default="utf-8" | ||
) | ||
parser.addini( | ||
"doctest_standalone_namespace", | ||
help="treat each module as having a standalone namespace", | ||
type="bool", | ||
default=False, | ||
) | ||
group = parser.getgroup("collect") | ||
group.addoption( | ||
"--doctest-modules", | ||
|
@@ -465,15 +471,19 @@ def _find_lineno(self, obj, source_lines): | |
return doctest.DocTestFinder._find_lineno(self, obj, source_lines) | ||
|
||
def _find( | ||
self, tests, obj, name, module, source_lines, globs, seen | ||
_self, tests, obj, name, module, source_lines, globs, seen | ||
) -> None: | ||
if _is_mocked(obj): | ||
return | ||
|
||
if self.config.getini("doctest_standalone_namespace"): | ||
globs = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on https://github.com/pytest-dev/pytest/blob/5.3.1/src/_pytest/doctest.py#L109-L113 that seems to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think non-py files can have variables that are defined outside of the doctest. Or is there something I'm missing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is the fixture I am still getting familiar with it, though I have the impression that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure how this currently interacts with that, nor how it should. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does interact, but in this case I don't think the new option should interact at all with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it makes sense to allow There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, taking a second look at the code, @asmeurer could you please add a test which ensures setting |
||
|
||
with _patch_unwrap_mock_aware(): | ||
|
||
# Type ignored because this is a private function. | ||
doctest.DocTestFinder._find( # type: ignore | ||
self, tests, obj, name, module, source_lines, globs, seen | ||
_self, tests, obj, name, module, source_lines, globs, seen | ||
) | ||
|
||
if self.fspath.basename == "conftest.py": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't think of any better options for the name either.
How about calling it
doctest_context
with a non bool type, or even better,doctest_reset_context
? That would leave the option up to implement something similar to the plot directive's context. And then, this can remain a bool.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with
doctest_reset_context
, although perhapsdoctest_empty_context
better conveys what the option actually does?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From a technical point of view, "namespace" is a better term than "context". Two function doctests don't interact with each other, which is what "reset context" seems to imply to me. So if you define a variable in one doctest it won't be available in another doctest.
Rather, they just reuse the module namespace, so if a variable is defined at the module level (not in a doctest), it gets defined automatically in a doctest. Maybe a name like
doctest_no_module_namespace
(although honestly my biggest issue with the current name is that it is too long).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO a long option name is OK if it correctly describes the behavior, because it will be written in a
pytest.ini
file only once anyway. 😁There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess my problem isn't so much the length as the fact that the option name seems convoluted. I don't think many people will look at "doctest_standalone_namespace" and understand what it means unless they already know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point, agree.
doctest_no_module_namespace
seems better in that light then, IMHO.