diff --git a/changelog/7685.improvement.rst b/changelog/7685.improvement.rst new file mode 100644 index 000000000..597721624 --- /dev/null +++ b/changelog/7685.improvement.rst @@ -0,0 +1,3 @@ +Added two new attributes :attr:`rootpath <_pytest.config.Config.rootpath>` and :attr:`inipath <_pytest.config.Config.inipath>` to :class:`Config <_pytest.config.Config>`. +These attributes are :class:`pathlib.Path` versions of the existing :attr:`rootdir <_pytest.config.Config.rootdir>` and :attr:`inifile <_pytest.config.Config.inifile>` attributes, +and should be preferred over them when possible. diff --git a/doc/en/customize.rst b/doc/en/customize.rst index e1f1b253b..9f7c365dc 100644 --- a/doc/en/customize.rst +++ b/doc/en/customize.rst @@ -180,10 +180,15 @@ are never merged - the first match wins. The internal :class:`Config <_pytest.config.Config>` object (accessible via hooks or through the :fixture:`pytestconfig` fixture) will subsequently carry these attributes: -- ``config.rootdir``: the determined root directory, guaranteed to exist. +- :attr:`config.rootpath <_pytest.config.Config.rootpath>`: the determined root directory, guaranteed to exist. -- ``config.inifile``: the determined ``configfile``, may be ``None`` (it is named ``inifile`` - for historical reasons). +- :attr:`config.inipath <_pytest.config.Config.inipath>`: the determined ``configfile``, may be ``None`` + (it is named ``inipath`` for historical reasons). + +.. versionadded:: 6.1 + The ``config.rootpath`` and ``config.inipath`` properties. They are :class:`pathlib.Path` + versions of the older ``config.rootdir`` and ``config.inifile``, which have type + ``py.path.local``, and still exist for backward compatibility. The ``rootdir`` is used as a reference directory for constructing test addresses ("nodeids") and can be used also by plugins for storing diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index ce88fc82f..6cda7da71 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -916,12 +916,53 @@ class Config: def invocation_dir(self) -> py.path.local: """The directory from which pytest was invoked. - Prefer to use :attr:`invocation_params.dir `. + Prefer to use :attr:`invocation_params.dir `, + which is a :class:`pathlib.Path`. :type: py.path.local """ return py.path.local(str(self.invocation_params.dir)) + @property + def rootpath(self) -> Path: + """The path to the :ref:`rootdir `. + + :type: pathlib.Path + + .. versionadded:: 6.1 + """ + return self._rootpath + + @property + def rootdir(self) -> py.path.local: + """The path to the :ref:`rootdir `. + + Prefer to use :attr:`rootpath`, which is a :class:`pathlib.Path`. + + :type: py.path.local + """ + return py.path.local(str(self.rootpath)) + + @property + def inipath(self) -> Optional[Path]: + """The path to the :ref:`configfile `. + + :type: Optional[pathlib.Path] + + .. versionadded:: 6.1 + """ + return self._inipath + + @property + def inifile(self) -> Optional[py.path.local]: + """The path to the :ref:`configfile `. + + Prefer to use :attr:`inipath`, which is a :class:`pathlib.Path`. + + :type: Optional[py.path.local] + """ + return py.path.local(str(self.inipath)) if self.inipath else None + def add_cleanup(self, func: Callable[[], None]) -> None: """Add a function to be called when the config object gets out of use (usually coninciding with pytest_unconfigure).""" @@ -1032,8 +1073,8 @@ class Config: rootdir_cmd_arg=ns.rootdir or None, config=self, ) - self.rootdir = py.path.local(str(rootpath)) - self.inifile = py.path.local(str(inipath)) if inipath else None + self._rootpath = rootpath + self._inipath = inipath self.inicfg = inicfg self._parser.extra_info["rootdir"] = self.rootdir self._parser.extra_info["inifile"] = self.inifile