From b11bfd021b981ab0bd8a993c3ce2d9855af60a0a Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Thu, 22 Aug 2024 16:08:14 +0200 Subject: [PATCH 1/2] Deprecates `uname_attr` and `uname_info` public methods distro as well as LinuxDistribution `uname_attr` and `uname_info` public methods are based on `_parse_uname_content` function which purposely ignores release information part from `uname -rs` command output on Linux platforms. This makes it specially designed for distro internals, and shouldn't be publicly available as stable API. We'll deprecate these methods in v1.10.0, in order to allow API removals in the future (e.g. distro v2). Developers are notified to rather use `os.uname` or `platform.uname` API from Python standard library. > closes #322 --- src/distro/distro.py | 71 ++++++++++++++++++++++++++++++++++++++++---- tests/test_distro.py | 18 +++++------ 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/src/distro/distro.py b/src/distro/distro.py index 14b8a07..c7aa60f 100755 --- a/src/distro/distro.py +++ b/src/distro/distro.py @@ -546,9 +546,21 @@ def distro_release_info() -> Dict[str, str]: def uname_info() -> Dict[str, str]: """ + .. deprecated:: 1.10.0 + + :func:`distro.uname_info()` is deprecated and will be removed in a + future version. Please use :func:`os.uname()` or :func:`platform.uname()` + instead. + Return a dictionary containing key-value pairs for the information items from the distro release file data source of the current OS distribution. """ + warnings.warn( + "distro.uname_info() is deprecated and will be removed in a future version. " + "Please use os.uname() or platform.uname() instead.", + DeprecationWarning, + stacklevel=2, + ) return _distro.uname_info() @@ -612,6 +624,12 @@ def distro_release_attr(attribute: str) -> str: def uname_attr(attribute: str) -> str: """ + .. deprecated:: 1.10.0 + + :func:`distro.uname_attr()` is deprecated and will be removed in a + future version. Please use :func:`os.uname()` or :func:`platform.uname()` + instead. + Return a single named information item from the distro release file data source of the current OS distribution. @@ -624,6 +642,12 @@ def uname_attr(attribute: str) -> str: * (string): Value of the information item, if the item exists. The empty string, if the item does not exist. """ + warnings.warn( + "distro.uname_attr() is deprecated and will be removed in a future version. " + "Please use os.uname() or platform.uname() instead.", + DeprecationWarning, + stacklevel=2, + ) return _distro.uname_attr(attribute) @@ -853,7 +877,7 @@ def normalize(distro_id: str, table: Dict[str, str]) -> str: if distro_id: return normalize(distro_id, NORMALIZED_DISTRO_ID) - distro_id = self.uname_attr("id") + distro_id = self._uname_attr("id") if distro_id: return normalize(distro_id, NORMALIZED_DISTRO_ID) @@ -869,14 +893,14 @@ def name(self, pretty: bool = False) -> str: self.os_release_attr("name") or self.lsb_release_attr("distributor_id") or self.distro_release_attr("name") - or self.uname_attr("name") + or self._uname_attr("name") ) if pretty: name = self.os_release_attr("pretty_name") or self.lsb_release_attr( "description" ) if not name: - name = self.distro_release_attr("name") or self.uname_attr("name") + name = self.distro_release_attr("name") or self._uname_attr("name") version = self.version(pretty=True) if version: name = f"{name} {version}" @@ -898,9 +922,9 @@ def version(self, pretty: bool = False, best: bool = False) -> str: self._parse_distro_release_content( self.lsb_release_attr("description") ).get("version_id", ""), - self.uname_attr("release"), + self._uname_attr("release"), ] - if self.uname_attr("id").startswith("aix"): + if self._uname_attr("id").startswith("aix"): # On AIX platforms, prefer oslevel command output. versions.insert(0, self.oslevel_info()) elif self.id() == "debian" or "debian" in self.like().split(): @@ -1042,11 +1066,25 @@ def distro_release_info(self) -> Dict[str, str]: def uname_info(self) -> Dict[str, str]: """ + .. deprecated:: 1.10.0 + + :func:`LinuxDistribution.uname_info()` is deprecated and will be removed + in a future version. Please use :func:`os.uname()` or + :func:`platform.uname()` instead. + Return a dictionary containing key-value pairs for the information items from the uname command data source of the OS distribution. For details, see :func:`distro.uname_info`. """ + warnings.warn( + ( + "LinuxDistribution.uname_info() is deprecated and will be removed in a" + " future version. Please use os.uname() or platform.uname() instead." + ), + DeprecationWarning, + stacklevel=2, + ) return self._uname_info def oslevel_info(self) -> str: @@ -1083,6 +1121,29 @@ def distro_release_attr(self, attribute: str) -> str: return self._distro_release_info.get(attribute, "") def uname_attr(self, attribute: str) -> str: + """ + .. deprecated:: 1.10.0 + + :func:`LinuxDistribution.uname_attr()` is deprecated and will be removed in + a future version. Please use :func:`os.uname()` or :func:`platform.uname()` + instead. + + Return a single named information item from the uname command + output data source of the OS distribution. + + For details, see :func:`distro.uname_attr`. + """ + warnings.warn( + ( + "LinuxDistribution.uname_attr() is deprecated and will be removed in a" + " future version. Please use os.uname() or platform.uname() instead." + ), + DeprecationWarning, + stacklevel=2, + ) + return self._uname_attr(attribute) + + def _uname_attr(self, attribute: str) -> str: """ Return a single named information item from the uname command output data source of the OS distribution. diff --git a/tests/test_distro.py b/tests/test_distro.py index a886c7e..439a152 100644 --- a/tests/test_distro.py +++ b/tests/test_distro.py @@ -798,9 +798,9 @@ def test_dontincludeuname(self) -> None: self.distro = distro.LinuxDistribution(include_uname=False) - assert self.distro.uname_attr("id") == "" - assert self.distro.uname_attr("name") == "" - assert self.distro.uname_attr("release") == "" + assert self.distro._uname_attr("id") == "" + assert self.distro._uname_attr("name") == "" + assert self.distro._uname_attr("release") == "" def test_unknowndistro_release(self) -> None: self._setup_for_distro(os.path.join(TESTDISTROS, "distro", "unknowndistro")) @@ -824,17 +824,17 @@ def test_bad_uname(self) -> None: self._setup_for_distro(os.path.join(TESTDISTROS, "distro", "baduname")) self.distro = distro.LinuxDistribution() - assert self.distro.uname_attr("id") == "" - assert self.distro.uname_attr("name") == "" - assert self.distro.uname_attr("release") == "" + assert self.distro._uname_attr("id") == "" + assert self.distro._uname_attr("name") == "" + assert self.distro._uname_attr("release") == "" def test_empty_uname(self) -> None: self._setup_for_distro(os.path.join(TESTDISTROS, "distro", "emptyuname")) self.distro = distro.LinuxDistribution() - assert self.distro.uname_attr("id") == "" - assert self.distro.uname_attr("name") == "" - assert self.distro.uname_attr("release") == "" + assert self.distro._uname_attr("id") == "" + assert self.distro._uname_attr("name") == "" + assert self.distro._uname_attr("release") == "" def test_usrlibosreleaseonly(self) -> None: self._setup_for_distro( From 7f0c2f9c1703445309ac607e416426511c3a0e39 Mon Sep 17 00:00:00 2001 From: Samuel FORESTIER Date: Sun, 2 Nov 2025 14:31:02 +0100 Subject: [PATCH 2/2] Make pytest distro ignore own deprecation warnings --- pyproject.toml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index fed528d..32f8696 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,10 @@ [build-system] requires = ["setuptools"] build-backend = "setuptools.build_meta" + +[tool.pytest.ini_options] +filterwarnings = [ + "error", + "ignore::DeprecationWarning", + 'ignore:distro\..*\(\) is deprecated and will be removed in a future version.*', +]