Skip to content

Commit 0db263a

Browse files
committed
config: add Config.{rootpath,inipath}, turn Config.{rootdir,inifile} to properties
1 parent 9cd8a15 commit 0db263a

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
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.

src/_pytest/config/__init__.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -907,18 +907,54 @@ def __init__(
907907
if TYPE_CHECKING:
908908
from _pytest.cacheprovider import Cache
909909

910+
self.rootpath = None # type: Path # type: ignore[assignment]
911+
"""The path to the :ref:`rootdir <rootdir>`.
912+
913+
:type: pathlib.Path
914+
915+
.. versionadded:: 6.1
916+
"""
917+
self.inipath = None # type: Optional[Path]
918+
"""The path to the :ref:`configfile <configfiles>`.
919+
920+
:type: Optional[pathlib.Path]
921+
922+
.. versionadded:: 6.1
923+
"""
924+
910925
self.cache = None # type: Optional[Cache]
911926

912927
@property
913928
def invocation_dir(self) -> py.path.local:
914929
"""The directory from which pytest was invoked.
915930
916-
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`.
931+
Prefer to use :attr:`invocation_params.dir <InvocationParams.dir>`,
932+
which is a :class:`pathlib.Path`.
917933
918934
:type: py.path.local
919935
"""
920936
return py.path.local(str(self.invocation_params.dir))
921937

938+
@property
939+
def rootdir(self) -> py.path.local:
940+
"""The path to the :ref:`rootdir <rootdir>`.
941+
942+
Prefer to use :attr:`rootpath`, which is a :class:`pathlib.Path`.
943+
944+
:type: py.path.local
945+
"""
946+
return py.path.local(str(self.rootpath))
947+
948+
@property
949+
def inifile(self) -> Optional[py.path.local]:
950+
"""The path to the :ref:`configfile <configfiles>`.
951+
952+
Prefer to use :attr:`inipath`, which is a :class:`pathlib.Path`.
953+
954+
:type: Optional[py.path.local]
955+
"""
956+
return py.path.local(str(self.inipath)) if self.inipath else None
957+
922958
def add_cleanup(self, func: Callable[[], None]) -> None:
923959
"""Add a function to be called when the config object gets out of
924960
use (usually coninciding with pytest_unconfigure)."""
@@ -1029,8 +1065,8 @@ def _initini(self, args: Sequence[str]) -> None:
10291065
rootdir_cmd_arg=ns.rootdir or None,
10301066
config=self,
10311067
)
1032-
self.rootdir = py.path.local(str(rootpath))
1033-
self.inifile = py.path.local(str(inipath)) if inipath else None
1068+
self.rootpath = rootpath
1069+
self.inipath = inipath
10341070
self.inicfg = inicfg
10351071
self._parser.extra_info["rootdir"] = self.rootdir
10361072
self._parser.extra_info["inifile"] = self.inifile

0 commit comments

Comments
 (0)