From 77844f386c4f1a8184ecfc08adcefac0e4de078b Mon Sep 17 00:00:00 2001 From: Eero Vaher Date: Thu, 25 Jan 2024 17:37:12 +0100 Subject: [PATCH] Avoid importing both `inspect` and `types` --- astropy/modeling/core.py | 3 +-- astropy/units/tests/test_quantity_non_ufuncs.py | 9 ++++++++- astropy/utils/introspection.py | 5 ++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/astropy/modeling/core.py b/astropy/modeling/core.py index aea3e2f403f5..80ec78c9e6f5 100644 --- a/astropy/modeling/core.py +++ b/astropy/modeling/core.py @@ -19,7 +19,6 @@ import inspect import itertools import operator -import types from collections import defaultdict, deque from inspect import signature from itertools import chain @@ -1479,7 +1478,7 @@ def bounding_box(self): # This typically implies a hard-coded bounding box. This will # probably be rare, but it is an option return self._bounding_box - elif isinstance(self._bounding_box, types.MethodType): + elif inspect.ismethod(self._bounding_box): return ModelBoundingBox.validate(self, self._bounding_box()) else: # The only other allowed possibility is that it's a ModelBoundingBox diff --git a/astropy/units/tests/test_quantity_non_ufuncs.py b/astropy/units/tests/test_quantity_non_ufuncs.py index 281f3ee683a1..dce43b7f3bc3 100644 --- a/astropy/units/tests/test_quantity_non_ufuncs.py +++ b/astropy/units/tests/test_quantity_non_ufuncs.py @@ -1,7 +1,10 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst + +from __future__ import annotations + import inspect import itertools -from types import FunctionType, ModuleType +from typing import TYPE_CHECKING import numpy as np import numpy.lib.recfunctions as rfn @@ -24,6 +27,10 @@ NUMPY_LT_2_0, ) +if TYPE_CHECKING: + from types import FunctionType, ModuleType + + VAR_POSITIONAL = inspect.Parameter.VAR_POSITIONAL VAR_KEYWORD = inspect.Parameter.VAR_KEYWORD POSITIONAL_ONLY = inspect.Parameter.POSITIONAL_ONLY diff --git a/astropy/utils/introspection.py b/astropy/utils/introspection.py index 1165a6db348e..060f884f2e3c 100644 --- a/astropy/utils/introspection.py +++ b/astropy/utils/introspection.py @@ -6,7 +6,6 @@ import inspect import os import sys -import types from importlib import metadata from packaging.version import Version @@ -128,7 +127,7 @@ def minversion(module, version, inclusive=True): >>> minversion(astropy, '0.4.4') True """ - if isinstance(module, types.ModuleType): + if inspect.ismodule(module): module_name = module.__name__ module_version = getattr(module, "__version__", None) elif isinstance(module, str): @@ -398,7 +397,7 @@ def isinstancemethod(cls, obj): but this function will always return `False` if the given object is not a member of the given class). """ - if not isinstance(obj, types.FunctionType): + if not inspect.isfunction(obj): return False # Unfortunately it seems the easiest way to get to the original