From 9f2dbf817d01556133716dba8661c8beceafe20a Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 4 Aug 2024 19:10:04 +0200 Subject: [PATCH] Use `typing.NamedTuple` instead of `collections.namedtuple` Inheriting from `typing.NamedTuple` creates a custom `tuple` subclass in the same way as using the `collections.namedtuple` factory function. However, using `typing.NamedTuple` allows you to provide a type annotation for each field in the class. --- tests/test_markers.py | 5 ++--- tests/test_musllinux.py | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_markers.py b/tests/test_markers.py index 775b51fd..e461c306 100644 --- a/tests/test_markers.py +++ b/tests/test_markers.py @@ -2,12 +2,11 @@ # 2.0, and the BSD License. See the LICENSE file in the root of this repository # for complete details. -import collections import itertools import os import platform import sys -from typing import cast +from typing import NamedTuple, cast from unittest import mock import pytest @@ -94,7 +93,7 @@ def test_allows_prerelease(self): ) -FakeVersionInfo = collections.namedtuple( +FakeVersionInfo = NamedTuple( "FakeVersionInfo", ["major", "minor", "micro", "releaselevel", "serial"] ) diff --git a/tests/test_musllinux.py b/tests/test_musllinux.py index 1a6a9a26..5ac28cc5 100644 --- a/tests/test_musllinux.py +++ b/tests/test_musllinux.py @@ -1,6 +1,6 @@ -import collections import pathlib import subprocess +from typing import NamedTuple import pretend import pytest @@ -60,7 +60,7 @@ def test_parse_musl_version(output, version): ) def test_get_musl_version(monkeypatch, executable, output, version, ld_musl): def mock_run(*args, **kwargs): - return collections.namedtuple("Proc", "stderr")(output) + return NamedTuple("Proc", "stderr")(output) run_recorder = pretend.call_recorder(mock_run) monkeypatch.setattr(_musllinux.subprocess, "run", run_recorder)