pythonpath: make it compatible with pytest-pythonpath until pytest 8
Fix #9636.
This commit is contained in:
parent
bc33ba0be9
commit
973c84aea1
|
@ -0,0 +1,2 @@
|
||||||
|
Restore compatiblity to users of the pytest-pythonpath plugins by adding two ini options ``python_paths`` and ``site_dirs``.
|
||||||
|
These options are immediately marked as deprecated. See :ref:`python_paths-site_dir-ini` for details.
|
|
@ -18,6 +18,24 @@ Deprecated Features
|
||||||
Below is a complete list of all pytest features which are considered deprecated. Using those features will issue
|
Below is a complete list of all pytest features which are considered deprecated. Using those features will issue
|
||||||
:class:`PytestWarning` or subclasses, which can be filtered using :ref:`standard warning filters <warnings>`.
|
:class:`PytestWarning` or subclasses, which can be filtered using :ref:`standard warning filters <warnings>`.
|
||||||
|
|
||||||
|
|
||||||
|
.. _python_paths-site_dir-ini:
|
||||||
|
|
||||||
|
The ``python_paths`` and ``site_dirs`` ini options
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. deprecated:: 7.0.1
|
||||||
|
|
||||||
|
These two deprecations are relevant for users of the pytest-pythonpath plugin.
|
||||||
|
In pytest 7.0.0, a built-in ``pythonpath`` plugin was added with similar functionality to pytest-pythonpath.
|
||||||
|
In pytest 7.0.1, it was found that the built-in plugin prevents the external pytest-pythonpath from loading,
|
||||||
|
so to compensate, the options from the pytest-pythonpath plugins are implemented, but are immediately deprecated.
|
||||||
|
|
||||||
|
The ``python_paths`` ini option is deprecated. Use :confval:`pythonpath` instead.
|
||||||
|
|
||||||
|
The ``site_dirs`` ini option is deprecated. There is no replacement. If you use it, let the pytest developers know.
|
||||||
|
|
||||||
|
|
||||||
.. _instance-collector-deprecation:
|
.. _instance-collector-deprecation:
|
||||||
|
|
||||||
The ``pytest.Instance`` collector
|
The ``pytest.Instance`` collector
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from distutils.core import setup
|
from distutils.core import setup
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -104,6 +104,17 @@ INSTANCE_COLLECTOR = PytestRemovedIn8Warning(
|
||||||
"See https://docs.pytest.org/en/latest/deprecations.html#the-pytest-instance-collector",
|
"See https://docs.pytest.org/en/latest/deprecations.html#the-pytest-instance-collector",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
PYTHON_PATHS_INI = PytestRemovedIn8Warning(
|
||||||
|
"The python_paths ini option is deprecated. Use the pythonpath ini option instead. "
|
||||||
|
"If you are using the pytest-pythonpath plugin, you can remove it - it is built-in since pytest 7.0.0. "
|
||||||
|
"See https://docs.pytest.org/en/latest/deprecations.html#python_paths-site_dir-ini",
|
||||||
|
)
|
||||||
|
|
||||||
|
SITE_DIRS_INI = PytestRemovedIn8Warning(
|
||||||
|
"The site_dirs ini option is deprecated. "
|
||||||
|
"See https://docs.pytest.org/en/latest/deprecations.html#python_paths-site_dir-ini",
|
||||||
|
)
|
||||||
|
|
||||||
# You want to make some `__init__` or function "private".
|
# You want to make some `__init__` or function "private".
|
||||||
#
|
#
|
||||||
# def my_private_function(some, args):
|
# def my_private_function(some, args):
|
||||||
|
|
|
@ -1,24 +1,54 @@
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from _pytest.deprecated import PYTHON_PATHS_INI
|
||||||
|
from _pytest.deprecated import SITE_DIRS_INI
|
||||||
from pytest import Config
|
from pytest import Config
|
||||||
from pytest import Parser
|
from pytest import Parser
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser: Parser) -> None:
|
def pytest_addoption(parser: Parser) -> None:
|
||||||
parser.addini("pythonpath", type="paths", help="Add paths to sys.path", default=[])
|
parser.addini("pythonpath", type="paths", help="Add paths to sys.path", default=[])
|
||||||
|
parser.addini(
|
||||||
|
"python_paths", type="paths", help="Deprecated alias for pythonpath", default=[]
|
||||||
|
)
|
||||||
|
parser.addini(
|
||||||
|
"site_dirs",
|
||||||
|
type="paths",
|
||||||
|
help="Deprecated: directory paths to add to via site.addsitedir(path)",
|
||||||
|
default=[],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.hookimpl(tryfirst=True)
|
@pytest.hookimpl(tryfirst=True)
|
||||||
def pytest_load_initial_conftests(early_config: Config) -> None:
|
def pytest_load_initial_conftests(early_config: Config) -> None:
|
||||||
|
pythonpath = early_config.getini("pythonpath")
|
||||||
|
python_paths = early_config.getini("python_paths")
|
||||||
|
if python_paths:
|
||||||
|
early_config.issue_config_time_warning(PYTHON_PATHS_INI, 2)
|
||||||
|
if not pythonpath:
|
||||||
|
pythonpath = python_paths
|
||||||
# `pythonpath = a b` will set `sys.path` to `[a, b, x, y, z, ...]`
|
# `pythonpath = a b` will set `sys.path` to `[a, b, x, y, z, ...]`
|
||||||
for path in reversed(early_config.getini("pythonpath")):
|
for path in reversed(pythonpath):
|
||||||
sys.path.insert(0, str(path))
|
sys.path.insert(0, str(path))
|
||||||
|
|
||||||
|
site_dirs = early_config.getini("site_dirs")
|
||||||
|
if site_dirs:
|
||||||
|
early_config.issue_config_time_warning(SITE_DIRS_INI, 2)
|
||||||
|
|
||||||
|
import site
|
||||||
|
|
||||||
|
for path in site_dirs:
|
||||||
|
site.addsitedir(str(path))
|
||||||
|
|
||||||
|
|
||||||
@pytest.hookimpl(trylast=True)
|
@pytest.hookimpl(trylast=True)
|
||||||
def pytest_unconfigure(config: Config) -> None:
|
def pytest_unconfigure(config: Config) -> None:
|
||||||
for path in config.getini("pythonpath"):
|
pythonpath = config.getini("pythonpath")
|
||||||
|
python_paths = config.getini("python_paths")
|
||||||
|
if python_paths and not pythonpath:
|
||||||
|
pythonpath = python_paths
|
||||||
|
for path in pythonpath:
|
||||||
path_str = str(path)
|
path_str = str(path)
|
||||||
if path_str in sys.path:
|
if path_str in sys.path:
|
||||||
sys.path.remove(path_str)
|
sys.path.remove(path_str)
|
||||||
|
|
Loading…
Reference in New Issue