From 7d2db185bd1bf6b5a131c6c483f3da3c65dd820e Mon Sep 17 00:00:00 2001 From: Pierre Glaser Date: Sun, 21 Feb 2021 18:47:46 +0000 Subject: [PATCH 1/5] correct identification of stdlib TypeVar objs In Python <= 3.7, the module attribute of TypeVar constructs is always set to typing and thus irrelevant to the true origin of the construct (stdlib, third-party, user-defined). cloudpickle used to cirumvent this issue in `_whichmodule` by exhaustively search for the construct in all imported modules. This would generate false positive in the case of stdlib TypeVar constructs such as AnyStr being used by imported third-party module. For such TypeVar construct, `_whichmodule` would occasionally flag them as belonging to the third party module. This resulted in a flaky test (`test_lookup_module_and_qualname_stdlib_typevar`) where `typing.AnyStr` was occasionally marked as defined in `_pytest.capture` instead of `typing`. --- cloudpickle/cloudpickle.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cloudpickle/cloudpickle.py b/cloudpickle/cloudpickle.py index 05d52afa0..6b9758c1c 100644 --- a/cloudpickle/cloudpickle.py +++ b/cloudpickle/cloudpickle.py @@ -136,11 +136,14 @@ def _whichmodule(obj, name): # Workaround bug in old Python versions: prior to Python 3.7, # T.__module__ would always be set to "typing" even when the TypeVar T # would be defined in a different module. - # - # For such older Python versions, we ignore the __module__ attribute of - # TypeVar instances and instead exhaustively lookup those instances in - # all currently imported modules. - module_name = None + if name is not None and getattr(typing, name, None) is obj: + # Built-in typeVar defined in typing such as AnyStr + return 'typing' + else: + # User defined or third-party TypeVar: __module__ attribute is + # irrelevant, thus trigger a exhaustive search for obj in all + # modules. + module_name = None else: module_name = getattr(obj, '__module__', None) From bfc71aa513458b83651a2b3a7ac93d72b57d5282 Mon Sep 17 00:00:00 2001 From: Pierre Glaser Date: Sun, 21 Feb 2021 18:59:53 +0000 Subject: [PATCH 2/5] Fix python-nightly ci entry (credits to @faucct - #411) --- .github/workflows/testing.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index b8d3ed462..77b272f29 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -92,6 +92,8 @@ jobs: python-nightly: runs-on: ubuntu-18.04 + env: + ACTIONS_ALLOW_UNSECURE_COMMANDS: true steps: - uses: actions/checkout@v1 - name: Install Python from ppa:deadsnakes/nightly From d231b27f8833e7f6ec8f94ad8e93b3d4df1f9d6d Mon Sep 17 00:00:00 2001 From: Pierre Glaser Date: Sun, 21 Feb 2021 19:32:10 +0000 Subject: [PATCH 3/5] CI trigger From 8ff581a814e2d586ebd3a0791aac2d55c2e5ee95 Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Mon, 1 Mar 2021 11:34:26 +0100 Subject: [PATCH 4/5] Update cloudpickle/cloudpickle.py --- cloudpickle/cloudpickle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudpickle/cloudpickle.py b/cloudpickle/cloudpickle.py index 6b9758c1c..a01a3c0f7 100644 --- a/cloudpickle/cloudpickle.py +++ b/cloudpickle/cloudpickle.py @@ -137,7 +137,7 @@ def _whichmodule(obj, name): # T.__module__ would always be set to "typing" even when the TypeVar T # would be defined in a different module. if name is not None and getattr(typing, name, None) is obj: - # Built-in typeVar defined in typing such as AnyStr + # Built-in TypeVar defined in typing such as AnyStr return 'typing' else: # User defined or third-party TypeVar: __module__ attribute is From 142f6ac5fab2de03bf6a7a0ffdfe0a07c9291c5d Mon Sep 17 00:00:00 2001 From: Olivier Grisel Date: Mon, 1 Mar 2021 16:23:21 +0100 Subject: [PATCH 5/5] Update .github/workflows/testing.yml --- .github/workflows/testing.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 77b272f29..b8d3ed462 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -92,8 +92,6 @@ jobs: python-nightly: runs-on: ubuntu-18.04 - env: - ACTIONS_ALLOW_UNSECURE_COMMANDS: true steps: - uses: actions/checkout@v1 - name: Install Python from ppa:deadsnakes/nightly