diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index db0cf2ab5b..8aa1577b46 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -5,6 +5,7 @@ on: branches: - main - 2.* + - conditional_test_pickle* pull_request: ~ env: diff --git a/tests/testdata/python3/data/conditional.py b/tests/testdata/python3/data/conditional.py new file mode 100644 index 0000000000..8f607c234c --- /dev/null +++ b/tests/testdata/python3/data/conditional.py @@ -0,0 +1,6 @@ +from data.conditional_import import ( + dump, + # dumps, + # load, + # loads, +) \ No newline at end of file diff --git a/tests/testdata/python3/data/conditional_import/__init__.py b/tests/testdata/python3/data/conditional_import/__init__.py new file mode 100644 index 0000000000..500ecbf3a1 --- /dev/null +++ b/tests/testdata/python3/data/conditional_import/__init__.py @@ -0,0 +1,11 @@ + +import pickle + +if False: + + def dump(obj, file, protocol=None): + pass + +else: + from functools import partial + dump = partial(pickle.dump, protocol=0) diff --git a/tests/unittest_nodes.py b/tests/unittest_nodes.py index eaa1aa6186..eabf49c8e3 100644 --- a/tests/unittest_nodes.py +++ b/tests/unittest_nodes.py @@ -36,7 +36,7 @@ import pytest import astroid -from astroid import bases, builder +from astroid import bases, builder, Uninferable from astroid import context as contextmod from astroid import node_classes, nodes, parse, test_utils, transforms, util from astroid.const import BUILTINS, PY38_PLUS, PY310_PLUS, Context @@ -566,6 +566,26 @@ def test_more_absolute_import(self): module = resources.build_file("data/module1abs/__init__.py", "data.module1abs") self.assertIn("sys", module.locals) + _pickle_names = ("dump",)# "dumps", "load", "loads") + + def test_conditional(self): + module = resources.build_file("data/conditional_import/__init__.py") + ctx = contextmod.InferenceContext() + + for name in self._pickle_names: + ctx.lookupname = name + some = list(module[name].infer(ctx)) + assert Uninferable not in some, name + + + def test_conditional_import(self): + module = resources.build_file("data/conditional.py") + ctx = contextmod.InferenceContext() + + for name in self._pickle_names: + ctx.lookupname = name + some = list(module[name].infer(ctx)) + assert Uninferable not in some, name class CmpNodeTest(unittest.TestCase): def test_as_string(self):