From f12e4e5a165b6cb64d993297511565cf424dd64c Mon Sep 17 00:00:00 2001 From: BobDotCom <71356958+BobDotCom@users.noreply.github.com> Date: Fri, 10 Feb 2023 16:44:09 -0600 Subject: [PATCH] fix: properly store advanced version_info data (#1920) * fix: properly store advanced version_info data * fix: fix deprecated alternative name --- discord/__main__.py | 6 ++--- discord/_version.py | 61 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/discord/__main__.py b/discord/__main__.py index 1c4b5f0d2c..12b5b5a1c3 100644 --- a/discord/__main__.py +++ b/discord/__main__.py @@ -44,11 +44,9 @@ def show_version() -> None: version_info = discord.version_info entries.append( - "- py-cord v{0.major}.{0.minor}.{0.micro}-{0.release_level}".format( - version_info - ) + "- py-cord v{0.major}.{0.minor}.{0.micro}-{0.releaselevel}".format(version_info) ) - if version_info.release_level != "final": + if version_info.releaselevel != "final": pkg = pkg_resources.get_distribution("py-cord") if pkg: entries.append(f" - py-cord pkg_resources: v{pkg.version}") diff --git a/discord/_version.py b/discord/_version.py index 53ee5883cf..c58c8baa91 100644 --- a/discord/_version.py +++ b/discord/_version.py @@ -24,11 +24,13 @@ """ from __future__ import annotations +import datetime import re import warnings -from datetime import date from importlib.metadata import PackageNotFoundError, version +from ._typed_dict import TypedDict + __all__ = ("__version__", "VersionInfo", "version_info") from typing import Literal, NamedTuple @@ -56,20 +58,54 @@ ) +class AdvancedVersionInfo(TypedDict): + serial: int + build: int | None + commit: str | None + date: datetime.date | None + + class VersionInfo(NamedTuple): major: int minor: int micro: int - release_level: Literal["alpha", "beta", "candidate", "final"] - serial: int - build: int | None = None - commit: str | None = None - date: date | None = None + releaselevel: Literal["alpha", "beta", "candidate", "final"] + + # We can't set instance attributes on a NamedTuple, so we have to use a + # global variable to store the advanced version info. + @property + def advanced(self) -> AdvancedVersionInfo: + return _advanced + + @advanced.setter + def advanced(self, value: object) -> None: + global _advanced + _advanced = value + + @property + @deprecated("releaselevel", "2.4") + def release_level(self) -> Literal["alpha", "beta", "candidate", "final"]: + return self.releaselevel @property - @deprecated("release_level", "2.3") - def releaselevel(self) -> Literal["alpha", "beta", "candidate", "final"]: - return self.release_level + @deprecated('.advanced["serial"]', "2.4") + def serial(self) -> int: + return self.advanced["serial"] + + @property + @deprecated('.advanced["build"]', "2.4") + def build(self) -> int | None: + return self.advanced["build"] + + @property + @deprecated('.advanced["commit"]', "2.4") + def commit(self) -> str | None: + return self.advanced["commit"] + + @property + @deprecated('.advanced["date"]', "2.4") + def date(self) -> datetime.date | None: + return self.advanced["date"] version_regex = re.compile( @@ -97,7 +133,7 @@ def releaselevel(self) -> Literal["alpha", "beta", "candidate", "final"]: raise RuntimeError("Invalid release level") if (raw_date := raw_info["date"] or raw_info["date1"]) is not None: - date_info = date( + date_info = datetime.date( int(raw_date[:4]), int(raw_date[4:6]), int(raw_date[6:]), @@ -109,7 +145,10 @@ def releaselevel(self) -> Literal["alpha", "beta", "candidate", "final"]: major=int(raw_info["major"] or 0) or None, minor=int(raw_info["minor"] or 0) or None, micro=int(raw_info["patch"] or 0) or None, - release_level=level_info, + releaselevel=level_info, +) + +_advanced = AdvancedVersionInfo( serial=raw_info["serial"], build=int(raw_info["build"] or 0) or None, commit=raw_info["commit"],