Skip to content

Commit d6134bc

Browse files
committed
doc: document consider_namespace_packages option
1 parent aac720a commit d6134bc

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

Diff for: changelog/11475.feature.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pytest now correctly identifies modules that are part of `namespace packages <https://packaging.python.org/en/latest/guides/packaging-namespace-packages>`__, for example when importing user-level modules for doctesting.
1+
Added the new :confval:`consider_namespace_packages` configuration option, defaulting to ``False``.
22

3-
Previously pytest was not aware of namespace packages, so running a doctest from a subpackage that is part of a namespace package would import just the subpackage (for example ``app.models``) instead of its full path (for example ``com.company.app.models``).
3+
If set to ``True``, pytest will attempt to identify modules that are part of `namespace packages <https://packaging.python.org/en/latest/guides/packaging-namespace-packages>`__ when importing modules.

Diff for: changelog/11475.improvement.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
:ref:`--import-mode=importlib <import-mode-importlib>` now tries to import modules using the standard import mechanism (but still without changing :py:data:`sys.path`), falling back to importing modules directly only if that fails.
2+
3+
This means that installed packages will be imported under their canonical name if possible first, for example ``app.core.models``, instead of having the module name always be derived from their path (for example ``.env310.lib.site_packages.app.core.models``).

Diff for: doc/en/explanation/pythonpath.rst

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ Import modes
1010

1111
pytest as a testing framework needs to import test modules and ``conftest.py`` files for execution.
1212

13-
Importing files in Python (at least until recently) is a non-trivial processes, often requiring
14-
changing :data:`sys.path`. Some aspects of the
13+
Importing files in Python is a non-trivial processes, so aspects of the
1514
import process can be controlled through the ``--import-mode`` command-line flag, which can assume
1615
these values:
1716

@@ -100,10 +99,17 @@ these values:
10099
Initially we intended to make ``importlib`` the default in future releases, however it is clear now that
101100
it has its own set of drawbacks so the default will remain ``prepend`` for the foreseeable future.
102101

102+
.. note::
103+
104+
By default, pytest will not attempt to resolve namespace packages automatically, but that can
105+
be changed via the :confval:`consider_namespace_packages` configuration variable.
106+
103107
.. seealso::
104108

105109
The :confval:`pythonpath` configuration variable.
106110

111+
The :confval:`consider_namespace_packages` configuration variable.
112+
107113
:ref:`test layout`.
108114

109115

Diff for: doc/en/reference/reference.rst

+13
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,19 @@ passed multiple times. The expected format is ``name=value``. For example::
12741274
variables, that will be expanded. For more information about cache plugin
12751275
please refer to :ref:`cache_provider`.
12761276

1277+
.. confval:: consider_namespace_packages
1278+
1279+
Controls if pytest should attempt to identify `namespace packages <https://packaging.python.org/en/latest/guides/packaging-namespace-packages>`__
1280+
when collecting Python modules. Default is ``False``.
1281+
1282+
Set to ``True`` if you are testing namespace packages installed into a virtual environment and it is important for
1283+
your packages to be imported using their full namespace package name.
1284+
1285+
Only `native namespace packages <https://packaging.python.org/en/latest/guides/packaging-namespace-packages/#native-namespace-packages>`__
1286+
are supported, with no plans to support `legacy namespace packages <https://packaging.python.org/en/latest/guides/packaging-namespace-packages/#legacy-namespace-packages>`__.
1287+
1288+
.. versionadded:: 8.1
1289+
12771290
.. confval:: console_output_style
12781291

12791292
Sets the console output style while running tests:

0 commit comments

Comments
 (0)