Skip to content

Commit

Permalink
Fix deprecation warnings in cpython3.11 (#1344)
Browse files Browse the repository at this point in the history
  • Loading branch information
davfsa authored Nov 6, 2022
1 parent 2f35ae0 commit 16314dd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
1 change: 1 addition & 0 deletions changes/1344.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix deprecation warnings in CPython3.11 in `hikari.internal.ux`.
9 changes: 8 additions & 1 deletion hikari/internal/ux.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ def init_logging(
"""Set of env variables which always indicate that ANSI flags should be included."""


def _read_banner(package: str) -> str:
if sys.version_info >= (3, 9):
return importlib.resources.files(package).joinpath("banner.txt").open("r").read()
else:
return importlib.resources.read_text(package, "banner.txt")


def print_banner(
package: typing.Optional[str],
allow_color: bool,
Expand Down Expand Up @@ -188,7 +195,7 @@ def print_banner(
if package is None:
return

raw_banner = importlib.resources.read_text(package, "banner.txt")
raw_banner = _read_banner(package)

system_bits = (platform.machine(), platform.system(), platform.release())
filtered_system_bits = (s.strip() for s in system_bits if s.strip())
Expand Down
58 changes: 48 additions & 10 deletions tests/hikari/internal/test_ux.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,42 @@ def test_when_doesnt_support_color(self):
supports_color.assert_called_once_with(True, False)


class TestRedBanner:
def test_when_above_3_9(self):
class MockTraversable:
joint_path = None
open_mode = None

def joinpath(self, path):
self.joint_path = path
return self

def open(self, mode):
self.open_mode = mode
return self

def read(self):
return read

traversable = MockTraversable()
read = object()

with mock.patch.object(sys, "version_info", new=(3, 9)):
with mock.patch.object(importlib.resources, "files", return_value=traversable, create=True) as read_text:
assert ux._read_banner("hikaru") is read

read_text.assert_called_once_with("hikaru")
assert traversable.joint_path == "banner.txt"
assert traversable.open_mode == "r"

def test_when_bellow_3_9(self):
with mock.patch.object(sys, "version_info", new=(2, 7)):
with mock.patch.object(importlib.resources, "read_text") as read_text:
assert ux._read_banner("hikaru") is read_text.return_value

read_text.assert_called_once_with("hikaru", "banner.txt")


class TestPrintBanner:
def test_when_package_is_none(self):
with mock.patch.object(sys.stdout, "write") as write:
Expand Down Expand Up @@ -179,15 +215,15 @@ def test_when_supports_color(self, mock_args):
)
stack.enter_context(mock.patch.object(time, "sleep"))
supports_color = stack.enter_context(mock.patch.object(ux, "supports_color", return_value=True))
read_text = stack.enter_context(mock.patch.object(importlib.resources, "read_text"))
read_banner = stack.enter_context(mock.patch.object(ux, "_read_banner"))
template = stack.enter_context(mock.patch.object(string, "Template"))
builtins_open = stack.enter_context(mock.patch.object(builtins, "open"))
abspath = stack.enter_context(mock.patch.object(os.path, "abspath", return_value="some path"))
dirname = stack.enter_context(mock.patch.object(os.path, "dirname"))
fileno = stack.enter_context(mock.patch.object(sys.stdout, "fileno"))

with stack:
ux.print_banner("hikari", True, False)
ux.print_banner("hikaru", True, False)

args = {
# Hikari stuff.
Expand All @@ -207,7 +243,8 @@ def test_when_supports_color(self, mock_args):
"blue": 2,
}

template.assert_called_once_with(read_text())
read_banner.assert_called_once_with("hikaru")
template.assert_called_once_with(read_banner.return_value)
template().safe_substitute.assert_called_once_with(args)
builtins_open.assert_called_once_with(fileno.return_value, "w", encoding="utf-8", closefd=False)
builtins_open.return_value.__enter__.return_value.write.assert_called_once_with(template().safe_substitute())
Expand All @@ -222,15 +259,15 @@ def test_when_doesnt_supports_color(self, mock_args):
)
stack.enter_context(mock.patch.object(time, "sleep"))
supports_color = stack.enter_context(mock.patch.object(ux, "supports_color", return_value=False))
read_text = stack.enter_context(mock.patch.object(importlib.resources, "read_text"))
read_banner = stack.enter_context(mock.patch.object(ux, "_read_banner"))
template = stack.enter_context(mock.patch.object(string, "Template"))
abspath = stack.enter_context(mock.patch.object(os.path, "abspath", return_value="some path"))
dirname = stack.enter_context(mock.patch.object(os.path, "dirname"))
builtins_open = stack.enter_context(mock.patch.object(builtins, "open"))
fileno = stack.enter_context(mock.patch.object(sys.stdout, "fileno"))

with stack:
ux.print_banner("hikari", True, False)
ux.print_banner("hikaru", True, False)

args = {
# Hikari stuff.
Expand All @@ -250,7 +287,7 @@ def test_when_doesnt_supports_color(self, mock_args):
"blue": "",
}

template.assert_called_once_with(read_text())
template.assert_called_once_with(read_banner.return_value)
template().safe_substitute.assert_called_once_with(args)
dirname.assert_called_once_with("~/hikari")
abspath.assert_called_once_with(dirname())
Expand All @@ -262,7 +299,7 @@ def test_use_extra_args(self, mock_args):
stack = contextlib.ExitStack()
stack.enter_context(mock.patch.object(colorlog.escape_codes, "escape_codes", new={}))
stack.enter_context(mock.patch.object(time, "sleep"))
read_text = stack.enter_context(mock.patch.object(importlib.resources, "read_text"))
read_banner = stack.enter_context(mock.patch.object(ux, "_read_banner"))
template = stack.enter_context(mock.patch.object(string, "Template"))
builtins_open = stack.enter_context(mock.patch.object(builtins, "open"))
stack.enter_context(mock.patch.object(os.path, "abspath", return_value="some path"))
Expand All @@ -274,7 +311,7 @@ def test_use_extra_args(self, mock_args):
}

with stack:
ux.print_banner("hikari", True, False, extra_args=extra_args)
ux.print_banner("hikaru", True, False, extra_args=extra_args)

args = {
# Hikari stuff.
Expand All @@ -293,7 +330,8 @@ def test_use_extra_args(self, mock_args):

args.update(extra_args)

template.assert_called_once_with(read_text())
read_banner.assert_called_once_with("hikaru")
template.assert_called_once_with(read_banner.return_value)
template().safe_substitute.assert_called_once_with(args)
builtins_open.assert_called_once_with(fileno.return_value, "w", encoding="utf-8", closefd=False)
builtins_open.return_value.__enter__.return_value.write.assert_called_once_with(template().safe_substitute())
Expand All @@ -302,7 +340,7 @@ def test_overwrite_args_raises_error(self, mock_args):
stack = contextlib.ExitStack()
stack.enter_context(mock.patch.object(time, "sleep"))
stack.enter_context(mock.patch.object(colorlog.escape_codes, "escape_codes", new={}))
stack.enter_context(mock.patch.object(importlib.resources, "read_text"))
stack.enter_context(mock.patch.object(ux, "_read_banner"))
stack.enter_context(mock.patch.object(string, "Template"))
stack.enter_context(mock.patch.object(sys.stdout, "write"))
stack.enter_context(mock.patch.object(os.path, "abspath", return_value="some path"))
Expand Down

0 comments on commit 16314dd

Please sign in to comment.