Skip to content

Commit

Permalink
Merge pull request astropy#15950 from eerovaher/avoid-inspect-with-types
Browse files Browse the repository at this point in the history
Avoid importing both `inspect` and `types`
  • Loading branch information
pllim authored Jan 25, 2024
2 parents e064a9f + 77844f3 commit 954302f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 1 addition & 2 deletions astropy/modeling/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion astropy/units/tests/test_quantity_non_ufuncs.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions astropy/utils/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import inspect
import os
import sys
import types
from importlib import metadata

from packaging.version import Version
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 954302f

Please sign in to comment.