Skip to content

Commit a346028

Browse files
committed
config: add Config.{rootpath,inipath}, turn Config.{rootdir,inifile} to properties
1 parent 3085c99 commit a346028

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

changelog/7685.improvement.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added two new attributes :attr:`rootpath <_pytest.config.Config.rootpath>` and :attr:`inipath <_pytest.config.Config.inipath>` to :class:`Config <_pytest.config.Config>`.
2+
These attributes are :class:`pathlib.Path` versions of the existing :attr:`rootdir <_pytest.config.Config.rootdir>` and :attr:`inifile <_pytest.config.Config.inifile>` attributes,
3+
and should be preferred over them when possible.

doc/en/customize.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,15 @@ are never merged - the first match wins.
180180
The internal :class:`Config <_pytest.config.Config>` object (accessible via hooks or through the :fixture:`pytestconfig` fixture)
181181
will subsequently carry these attributes:
182182

183-
- ``config.rootdir``: the determined root directory, guaranteed to exist.
183+
- :attr:`config.rootpath <_pytest.config.Config.rootpath>`: the determined root directory, guaranteed to exist.
184184

185-
- ``config.inifile``: the determined ``configfile``, may be ``None`` (it is named ``inifile``
186-
for historical reasons).
185+
- :attr:`config.inipath <_pytest.config.Config.inipath>`: the determined ``configfile``, may be ``None``
186+
(it is named ``inipath`` for historical reasons).
187+
188+
.. versionadded:: 6.1
189+
The ``config.rootpath`` and ``config.inipath`` properties. They are :class:`pathlib.Path`
190+
versions of the older ``config.rootdir`` and ``config.inifile``, which have type
191+
``py.path.local``, and still exist for backward compatibility.
187192

188193
The ``rootdir`` is used as a reference directory for constructing test
189194
addresses ("nodeids") and can be used also by plugins for storing

src/_pytest/config/__init__.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,12 +916,53 @@ def __init__(
916916
def invocation_dir(self) -> py.path.local:
917917
"""The directory from which pytest was invoked.
918918
919-
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`.
919+
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`,
920+
which is a :class:`pathlib.Path`.
920921
921922
:type: py.path.local
922923
"""
923924
return py.path.local(str(self.invocation_params.dir))
924925

926+
@property
927+
def rootpath(self) -> Path:
928+
"""The path to the :ref:`rootdir <rootdir>`.
929+
930+
:type: pathlib.Path
931+
932+
.. versionadded:: 6.1
933+
"""
934+
return self._rootpath
935+
936+
@property
937+
def rootdir(self) -> py.path.local:
938+
"""The path to the :ref:`rootdir <rootdir>`.
939+
940+
Prefer to use :attr:`rootpath`, which is a :class:`pathlib.Path`.
941+
942+
:type: py.path.local
943+
"""
944+
return py.path.local(str(self.rootpath))
945+
946+
@property
947+
def inipath(self) -> Optional[Path]:
948+
"""The path to the :ref:`configfile <configfiles>`.
949+
950+
:type: Optional[pathlib.Path]
951+
952+
.. versionadded:: 6.1
953+
"""
954+
return self._inipath
955+
956+
@property
957+
def inifile(self) -> Optional[py.path.local]:
958+
"""The path to the :ref:`configfile <configfiles>`.
959+
960+
Prefer to use :attr:`inipath`, which is a :class:`pathlib.Path`.
961+
962+
:type: Optional[py.path.local]
963+
"""
964+
return py.path.local(str(self.inipath)) if self.inipath else None
965+
925966
def add_cleanup(self, func: Callable[[], None]) -> None:
926967
"""Add a function to be called when the config object gets out of
927968
use (usually coninciding with pytest_unconfigure)."""
@@ -1032,8 +1073,8 @@ def _initini(self, args: Sequence[str]) -> None:
10321073
rootdir_cmd_arg=ns.rootdir or None,
10331074
config=self,
10341075
)
1035-
self.rootdir = py.path.local(str(rootpath))
1036-
self.inifile = py.path.local(str(inipath)) if inipath else None
1076+
self._rootpath = rootpath
1077+
self._inipath = inipath
10371078
self.inicfg = inicfg
10381079
self._parser.extra_info["rootdir"] = self.rootdir
10391080
self._parser.extra_info["inifile"] = self.inifile

0 commit comments

Comments
 (0)