Drop now redundant use of inspect (#46057)

inspect.isclass -> isinstance(..., type)
inspect.getmro -> cls.__mro__
This commit is contained in:
Harmen Stoppels 2024-08-28 14:35:08 +02:00 committed by GitHub
parent 202e64872a
commit fb4811ec3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 9 additions and 14 deletions

View File

@ -39,7 +39,6 @@ def _search_duplicate_compilers(error_cls):
import collections import collections
import collections.abc import collections.abc
import glob import glob
import inspect
import io import io
import itertools import itertools
import os import os
@ -525,7 +524,7 @@ def _search_for_reserved_attributes_names_in_packages(pkgs, error_cls):
name_definitions = collections.defaultdict(list) name_definitions = collections.defaultdict(list)
pkg_cls = spack.repo.PATH.get_pkg_class(pkg_name) pkg_cls = spack.repo.PATH.get_pkg_class(pkg_name)
for cls_item in inspect.getmro(pkg_cls): for cls_item in pkg_cls.__mro__:
for name in RESERVED_NAMES: for name in RESERVED_NAMES:
current_value = cls_item.__dict__.get(name) current_value = cls_item.__dict__.get(name)
if current_value is None: if current_value is None:

View File

@ -1559,7 +1559,7 @@ def __init__(self, package):
#: Modules for the classes in the MRO up to PackageBase #: Modules for the classes in the MRO up to PackageBase
modules_in_mro = [] modules_in_mro = []
for cls in inspect.getmro(type(package)): for cls in type(package).__mro__:
module = cls.module module = cls.module
if module == self.current_module: if module == self.current_module:

View File

@ -25,7 +25,6 @@
so package authors should use their judgement. so package authors should use their judgement.
""" """
import functools import functools
import inspect
from contextlib import contextmanager from contextlib import contextmanager
import spack.directives_meta import spack.directives_meta
@ -133,7 +132,7 @@ def __call__(self, package_or_builder_self, *args, **kwargs):
# its superclasses for successive calls. We don't have that # its superclasses for successive calls. We don't have that
# information within `SpecMultiMethod`, because it is not # information within `SpecMultiMethod`, because it is not
# associated with the package class. # associated with the package class.
for cls in inspect.getmro(package_or_builder_self.__class__)[1:]: for cls in package_or_builder_self.__class__.__mro__[1:]:
superself = cls.__dict__.get(self.__name__, None) superself = cls.__dict__.get(self.__name__, None)
if isinstance(superself, SpecMultiMethod): if isinstance(superself, SpecMultiMethod):

View File

@ -878,7 +878,7 @@ def fullname(cls):
def fullnames(cls): def fullnames(cls):
"""Fullnames for this package and any packages from which it inherits.""" """Fullnames for this package and any packages from which it inherits."""
fullnames = [] fullnames = []
for cls in inspect.getmro(cls): for cls in cls.__mro__:
namespace = getattr(cls, "namespace", None) namespace = getattr(cls, "namespace", None)
if namespace: if namespace:
fullnames.append("%s.%s" % (namespace, cls.name)) fullnames.append("%s.%s" % (namespace, cls.name))

View File

@ -4,7 +4,6 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT) # SPDX-License-Identifier: (Apache-2.0 OR MIT)
import hashlib import hashlib
import inspect
import os import os
import os.path import os.path
import pathlib import pathlib
@ -185,8 +184,8 @@ def __init__(
# search mro to look for the file # search mro to look for the file
abs_path: Optional[str] = None abs_path: Optional[str] = None
# At different times we call FilePatch on instances and classes # At different times we call FilePatch on instances and classes
pkg_cls = pkg if inspect.isclass(pkg) else pkg.__class__ pkg_cls = pkg if isinstance(pkg, type) else pkg.__class__
for cls in inspect.getmro(pkg_cls): # type: ignore for cls in pkg_cls.__mro__: # type: ignore
if not hasattr(cls, "module"): if not hasattr(cls, "module"):
# We've gone too far up the MRO # We've gone too far up the MRO
break break

View File

@ -1281,7 +1281,7 @@ def get_pkg_class(self, pkg_name: str) -> Type["spack.package_base.PackageBase"]
raise RepoError(msg) from e raise RepoError(msg) from e
cls = getattr(module, class_name) cls = getattr(module, class_name)
if not inspect.isclass(cls): if not isinstance(cls, type):
tty.die(f"{pkg_name}.{class_name} is not a class") tty.die(f"{pkg_name}.{class_name} is not a class")
# Clear any prior changes to class attributes in case the class was loaded from the # Clear any prior changes to class attributes in case the class was loaded from the

View File

@ -2,7 +2,6 @@
# Spack Project Developers. See the top-level COPYRIGHT file for details. # Spack Project Developers. See the top-level COPYRIGHT file for details.
# #
# SPDX-License-Identifier: (Apache-2.0 OR MIT) # SPDX-License-Identifier: (Apache-2.0 OR MIT)
import inspect
import os import os
import platform import platform
import posixpath import posixpath
@ -593,7 +592,7 @@ def test_setting_attributes(self, default_mock_concretization):
# We can also propagate the settings to classes in the MRO # We can also propagate the settings to classes in the MRO
module_wrapper.propagate_changes_to_mro() module_wrapper.propagate_changes_to_mro()
for cls in inspect.getmro(type(s.package)): for cls in type(s.package).__mro__:
current_module = cls.module current_module = cls.module
if current_module == spack.package_base: if current_module == spack.package_base:
break break

View File

@ -2,7 +2,6 @@
# Spack Project Developers. See the top-level COPYRIGHT file for details. # Spack Project Developers. See the top-level COPYRIGHT file for details.
# #
# SPDX-License-Identifier: (Apache-2.0 OR MIT) # SPDX-License-Identifier: (Apache-2.0 OR MIT)
import inspect
import itertools import itertools
import os import os
import re import re
@ -700,7 +699,7 @@ def test_parse_multiple_specs(text, tokens, expected_specs):
], ],
) )
def test_cli_spec_roundtrip(args, expected): def test_cli_spec_roundtrip(args, expected):
if inspect.isclass(expected) and issubclass(expected, BaseException): if isinstance(expected, type) and issubclass(expected, BaseException):
with pytest.raises(expected): with pytest.raises(expected):
spack.cmd.parse_specs(args) spack.cmd.parse_specs(args)
return return