summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Paine <t.paine154@gmail.com>2022-07-23 15:46:43 -0400
committerDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-08-23 11:39:34 +0200
commit6be6ec7b7b2d53e52d4f8a5c3527afeee0f4f082 (patch)
tree4b23e981c1e1800e4399d996ac8d2f1cebbff5a9
parent14a0d657809ab5b42e7d416b686953508e6d8b91 (diff)
downloadastroid-git-6be6ec7b7b2d53e52d4f8a5c3527afeee0f4f082.tar.gz
Fixes #1717 - ignore FutureWarnings which are raised by pandas (#1719)
And probably other modules too.
-rw-r--r--CONTRIBUTORS.txt6
-rw-r--r--ChangeLog3
-rw-r--r--astroid/raw_building.py4
-rw-r--r--tests/testdata/python3/data/fake_module_with_warnings.py22
-rw-r--r--tests/unittest_raw_building.py16
5 files changed, 49 insertions, 2 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 79e254f6..b4a7aa0c 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -175,6 +175,12 @@ Contributors
- Alexander Scheel <alexander.m.scheel@gmail.com>
- Alexander Presnyakov <flagist0@gmail.com>
- Ahmed Azzaoui <ahmed.azzaoui@engie.com>
+- nathannaveen <42319948+nathannaveen@users.noreply.github.com>
+- adam-grant-hendry <59346180+adam-grant-hendry@users.noreply.github.com>
+- Deepyaman Datta <deepyaman.datta@utexas.edu>
+- Batuhan Taskaya <isidentical@gmail.com>
+- Alexander Scheel <alexander.m.scheel@gmail.com>
+- Tim Paine <t.paine154@gmail.com>
Co-Author
---------
diff --git a/ChangeLog b/ChangeLog
index 7b84be1a..f87038f9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,6 +12,9 @@ What's New in astroid 2.12.3?
=============================
Release date: TBA
+* Fix unhandled `FutureWarning` from pandas import in cython modules
+
+ Closes #1717
What's New in astroid 2.12.2?
diff --git a/astroid/raw_building.py b/astroid/raw_building.py
index 93e3ff55..8cff41d3 100644
--- a/astroid/raw_building.py
+++ b/astroid/raw_building.py
@@ -388,9 +388,9 @@ class InspectBuilder:
pypy__class_getitem__ = IS_PYPY and name == "__class_getitem__"
try:
with warnings.catch_warnings():
- warnings.simplefilter("error")
+ warnings.simplefilter("ignore")
member = getattr(obj, name)
- except (AttributeError, DeprecationWarning):
+ except (AttributeError):
# damned ExtensionClass.Base, I know you're there !
attach_dummy_node(node, name)
continue
diff --git a/tests/testdata/python3/data/fake_module_with_warnings.py b/tests/testdata/python3/data/fake_module_with_warnings.py
new file mode 100644
index 00000000..ac818153
--- /dev/null
+++ b/tests/testdata/python3/data/fake_module_with_warnings.py
@@ -0,0 +1,22 @@
+'''
+This is a mock of a module like Pandas, which can throw warnings for deprecated attributes
+'''
+import warnings
+
+
+def __dir__():
+ # GH43028
+ # Int64Index etc. are deprecated, but we still want them to be available in the dir.
+ # Remove in Pandas 2.0, when we remove Int64Index etc. from the code base.
+ return list(globals().keys()) + ["Float64Index"]
+
+
+def __getattr__(name):
+ if name == "Float64Index":
+ warnings.warn("This is what pandas would do", FutureWarning, stacklevel=2)
+ return 5
+ raise AttributeError(f"module 'pandas' has no attribute '{name}'")
+
+
+__all__ = ["Float64Index"] # pylint: disable=E0603
+__doc__ = ""
diff --git a/tests/unittest_raw_building.py b/tests/unittest_raw_building.py
index 387a3f43..59bd6ce1 100644
--- a/tests/unittest_raw_building.py
+++ b/tests/unittest_raw_building.py
@@ -2,11 +2,14 @@
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
+import types
import unittest
import _io
import pytest
+# A fake module to simulate pandas in unittest below
+import tests.testdata.python3.data.fake_module_with_warnings as fm
from astroid.builder import AstroidBuilder
from astroid.const import IS_PYPY
from astroid.raw_building import (
@@ -86,6 +89,19 @@ class RawBuildingTC(unittest.TestCase):
buffered_reader = module.getattr("BufferedReader")[0]
self.assertEqual(buffered_reader.root().name, "io")
+ def test_build_function_deepinspect_deprecation(self) -> None:
+ # Tests https://github.com/PyCQA/astroid/issues/1717
+ # When astroid deep inspection of modules raises
+ # attribute errors when getting all attributes
+ # Create a mock module to simulate a Cython module
+ m = types.ModuleType("test")
+
+ # Attach a mock of pandas with the same behavior
+ m.pd = fm
+
+ # This should not raise an exception
+ AstroidBuilder().module_build(m, "test")
+
if __name__ == "__main__":
unittest.main()