From ca63be7bd644a5ef92a845dca9caeae5da1ec880 Mon Sep 17 00:00:00 2001 From: Udi Meiri Date: Thu, 12 Sep 2019 14:24:20 -0700 Subject: [PATCH] [BEAM-7060] Prefer inspect module over funcsigs In Python 3 with funcsigs installed, prefer inspect.{signature,Paramter} over funcsigs equivalents. --- sdks/python/apache_beam/transforms/core.py | 2 ++ sdks/python/apache_beam/typehints/decorators.py | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/sdks/python/apache_beam/transforms/core.py b/sdks/python/apache_beam/transforms/core.py index 6433ba06f7cb..5f68294972c6 100644 --- a/sdks/python/apache_beam/transforms/core.py +++ b/sdks/python/apache_beam/transforms/core.py @@ -317,6 +317,8 @@ def get_function_args_defaults(f): it doesn't include bound arguments and may follow function wrappers. """ signature = get_signature(f) + # Fall back on funcsigs if inspect module doesn't have 'Parameter'; prefer + # inspect.Parameter over funcsigs.Parameter if both are available. try: parameter = inspect.Parameter except AttributeError: diff --git a/sdks/python/apache_beam/typehints/decorators.py b/sdks/python/apache_beam/typehints/decorators.py index 69c77e45d5ed..77793150742b 100644 --- a/sdks/python/apache_beam/typehints/decorators.py +++ b/sdks/python/apache_beam/typehints/decorators.py @@ -167,10 +167,12 @@ def get_signature(func): latter: 'the "self" parameter is always reported, even for bound methods' https://github.com/python/cpython/blob/44f91c388a6f4da9ed3300df32ca290b8aa104ea/Lib/inspect.py#L1103 """ - if funcsigs is not None: - inspect_ = funcsigs - else: + # Fall back on funcsigs if inspect module doesn't have 'signature'; prefer + # inspect.signature over funcsigs.signature if both are available. + if hasattr(inspect, 'signature'): inspect_ = inspect + else: + inspect_ = funcsigs try: signature = inspect_.signature(func)