Kaydet (Commit) 3215a6c5 authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

Use dataclass for pkgutil.ModuleInfo

üst f665b96e
...@@ -13,7 +13,7 @@ support. ...@@ -13,7 +13,7 @@ support.
.. class:: ModuleInfo(module_finder, name, ispkg) .. class:: ModuleInfo(module_finder, name, ispkg)
A namedtuple that holds a brief summary of a module's info. A dataclass that holds a brief summary of a module's info.
.. versionadded:: 3.6 .. versionadded:: 3.6
......
"""Utilities to support packages.""" """Utilities to support packages."""
from __future__ import annotations
from collections import namedtuple from dataclasses import dataclass, field, astuple
from functools import singledispatch as simplegeneric from functools import singledispatch as simplegeneric
import importlib import importlib
import importlib.util import importlib.util
...@@ -19,9 +19,32 @@ __all__ = [ ...@@ -19,9 +19,32 @@ __all__ = [
] ]
ModuleInfo = namedtuple('ModuleInfo', 'module_finder name ispkg') @dataclass(frozen=True, order=True)
ModuleInfo.__doc__ = 'A namedtuple with minimal info about a module.' class ModuleInfo:
"""A dataclass with minimal info about a module."""
module_finder: FileFinder = field(compare = False)
name: str
ispkg: bool
def __contains__(self, item):
warnings.warn("Sequence access is deprecated, use attribute access instead",
DeprecationWarning)
return item in astuple(self)
def __len__(self):
warnings.warn("Sequence access is deprecated, use attribute access instead",
DeprecationWarning)
return len(astuple(self))
def __iter__(self):
warnings.warn("Sequence access is deprecated, use attribute access instead",
DeprecationWarning)
return iter(astuple(self))
def __getitem__(self, item):
warnings.warn("Sequence access is deprecated, use attribute access instead",
DeprecationWarning)
return astuple(self)[item]
def _get_spec(finder, name): def _get_spec(finder, name):
"""Return the finder-specific module spec.""" """Return the finder-specific module spec."""
......
...@@ -133,7 +133,7 @@ class PkgutilTests(unittest.TestCase): ...@@ -133,7 +133,7 @@ class PkgutilTests(unittest.TestCase):
'test_walkpackages_filesys.sub', 'test_walkpackages_filesys.sub',
'test_walkpackages_filesys.sub.mod', 'test_walkpackages_filesys.sub.mod',
] ]
actual= [e[1] for e in pkgutil.walk_packages([self.dirname])] actual= [e.name for e in pkgutil.walk_packages([self.dirname])]
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
for pkg in expected: for pkg in expected:
...@@ -167,7 +167,7 @@ class PkgutilTests(unittest.TestCase): ...@@ -167,7 +167,7 @@ class PkgutilTests(unittest.TestCase):
'test_walkpackages_zipfile.sub', 'test_walkpackages_zipfile.sub',
'test_walkpackages_zipfile.sub.mod', 'test_walkpackages_zipfile.sub.mod',
] ]
actual= [e[1] for e in pkgutil.walk_packages([zip_file])] actual= [e.name for e in pkgutil.walk_packages([zip_file])]
self.assertEqual(actual, expected) self.assertEqual(actual, expected)
del sys.path[0] del sys.path[0]
...@@ -229,6 +229,20 @@ class PkgutilPEP302Tests(unittest.TestCase): ...@@ -229,6 +229,20 @@ class PkgutilPEP302Tests(unittest.TestCase):
self.assertEqual(foo.loads, 1) self.assertEqual(foo.loads, 1)
del sys.modules['foo'] del sys.modules['foo']
def test_module_info(self):
dummy = pkgutil.ModuleInfo(None, 'dummy', False)
with check_warnings() as w:
self.assertEqual(dummy[1], 'dummy')
self.assertTrue('dummy' in dummy)
self.assertEqual(len(dummy), 3)
_, name, __ = dummy
self.assertEqual(name, 'dummy')
self.assertEqual(len(w.warnings), 4)
self.assertEqual(dummy.name, 'dummy')
dummy2 = pkgutil.ModuleInfo(None, 'dummiest', False)
self.assertTrue(dummy > dummy2)
# These tests, especially the setup and cleanup, are hideous. They # These tests, especially the setup and cleanup, are hideous. They
# need to be cleaned up once issue 14715 is addressed. # need to be cleaned up once issue 14715 is addressed.
......
pkgutil.ModuleInfo now a dataclass with rich compare methods instead of a
namedtuple
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment