Skip to content

Commit

Permalink
fix: properly store advanced version_info data (#1920)
Browse files Browse the repository at this point in the history
* fix: properly store advanced version_info data

* fix: fix deprecated alternative name
  • Loading branch information
BobDotCom authored Feb 10, 2023
1 parent 4b84447 commit f12e4e5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
6 changes: 2 additions & 4 deletions discord/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
61 changes: 50 additions & 11 deletions discord/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:]),
Expand All @@ -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"],
Expand Down

0 comments on commit f12e4e5

Please sign in to comment.