From 6701e74fec4897e8aa2f18514b5cde2f7c59aeed Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 12 Nov 2020 11:36:48 +0100 Subject: [PATCH] fix(typing): allow any object as first argument for get_type_hints (#4744) get_type_hints works on more object Fixes #4678 --- stdlib/3/typing.pyi | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index c91c83c10..098a29790 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -1,7 +1,10 @@ import collections # Needed by aliases like DefaultDict, see mypy issue 2986 import sys from abc import ABCMeta, abstractmethod -from types import CodeType, FrameType, TracebackType +from types import BuiltinFunctionType, CodeType, FrameType, FunctionType, MethodType, ModuleType, TracebackType + +if sys.version_info >= (3, 7): + from types import MethodDescriptorType, MethodWrapperType, WrapperDescriptorType if sys.version_info >= (3, 9): from types import GenericAlias @@ -585,9 +588,31 @@ class Pattern(Generic[AnyStr]): # Functions +if sys.version_info >= (3, 7): + _get_type_hints_obj_allowed_types = Union[ + object, + Callable[..., Any], + FunctionType, + BuiltinFunctionType, + MethodType, + ModuleType, + WrapperDescriptorType, + MethodWrapperType, + MethodDescriptorType, + ] +else: + _get_type_hints_obj_allowed_types = Union[ + object, + Callable[..., Any], + FunctionType, + BuiltinFunctionType, + MethodType, + ModuleType, + ] + if sys.version_info >= (3, 9): def get_type_hints( - obj: Callable[..., Any], + obj: _get_type_hints_obj_allowed_types, globalns: Optional[Dict[str, Any]] = ..., localns: Optional[Dict[str, Any]] = ..., include_extras: bool = ..., @@ -595,7 +620,9 @@ if sys.version_info >= (3, 9): else: def get_type_hints( - obj: Callable[..., Any], globalns: Optional[Dict[str, Any]] = ..., localns: Optional[Dict[str, Any]] = ... + obj: _get_type_hints_obj_allowed_types, + globalns: Optional[Dict[str, Any]] = ..., + localns: Optional[Dict[str, Any]] = ..., ) -> Dict[str, Any]: ... if sys.version_info >= (3, 8):