Update type handling
"""Provides utilities when working with types."""
import inspect
import numbers
+import types
from inspect import isclass, isfunction
from typing import Type, Optional, Callable, Any
@@ -98,14 +99,16 @@ def get_class_that_defined_method(method: object) -> Optional[object]:
:return: The class that defines the method
"""
if inspect.ismethod(method):
- for cls in inspect.getmro(method.__self__.__class__): # type: ignore
- if cls.__dict__.get(method.__name__) is method: # type: ignore
+ assert isinstance(method, types.MethodType)
+ for cls in inspect.getmro(method.__self__.__class__):
+ if cls.__dict__.get(method.__name__) is method:
return cls
- method = method.__func__ # type: ignore # fallback to __qualname__ parsing
+ method = method.__func__ # fallback to __qualname__ parsing
if inspect.isfunction(method):
+ assert isinstance(method, types.FunctionType)
cls = getattr(
inspect.getmodule(method),
- method.__qualname__.split(".<locals>", 1)[0].rsplit(".", 1)[0], # type: ignore
+ method.__qualname__.split(".<locals>", 1)[0].rsplit(".", 1)[0],
)
if isinstance(cls, type):
return cls
