Skip to content

Commit 9cee495

Browse files
committed
Suppress deprecation warnings while collecting modules
- closes #542
1 parent 3a359a4 commit 9cee495

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ The released versions correspond to PyPi releases.
33

44
## Version 4.2.0 (as yet unreleased)
55

6+
#### Fixes
7+
* suppress deprecation warnings while collecting modules
8+
(see [#542](../../issues/542))
9+
610
## [Version 4.1.0](https://pypi.python.org/pypi/pyfakefs/4.1.0)
711

812
#### New Features

pyfakefs/fake_filesystem_unittest.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,19 @@ def _def_values(self, item):
503503
elif inspect.isclass(item):
504504
# check for methods in class (nested classes are ignored for now)
505505
try:
506-
for m in inspect.getmembers(item,
507-
predicate=inspect.isfunction):
508-
m = m[1]
509-
if m.__defaults__:
510-
for i, d in enumerate(m.__defaults__):
511-
if self._is_fs_function(d):
512-
yield m, i, d
506+
with warnings.catch_warnings():
507+
# ignore deprecation warnings, see #542
508+
warnings.filterwarnings(
509+
'ignore',
510+
category=DeprecationWarning
511+
)
512+
for m in inspect.getmembers(item,
513+
predicate=inspect.isfunction):
514+
m = m[1]
515+
if m.__defaults__:
516+
for i, d in enumerate(m.__defaults__):
517+
if self._is_fs_function(d):
518+
yield m, i, d
513519
except Exception:
514520
# Ignore any exception, examples:
515521
# ImportError: No module named '_gdbm'

pyfakefs/tests/fake_filesystem_unittest_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import sys
2626
import tempfile
2727
import unittest
28+
import warnings
2829
from distutils.dir_util import copy_tree, remove_tree
2930
from unittest import TestCase
3031

@@ -659,5 +660,20 @@ def test_cwd(self, fs):
659660
self.assertTrue(dot_abs.exists())
660661

661662

663+
class TestDeprecationSuppression(fake_filesystem_unittest.TestCase):
664+
def test_no_deprecation_warning(self):
665+
"""Ensures that deprecation warnings are suppressed during module
666+
lookup, see #542.
667+
"""
668+
669+
from pyfakefs.tests.fixtures.deprecated_property import \
670+
DeprecationTest # noqa: F401
671+
672+
with warnings.catch_warnings(record=True) as w:
673+
warnings.simplefilter("error", DeprecationWarning)
674+
self.setUpPyfakefs()
675+
self.assertEqual(0, len(w))
676+
677+
662678
if __name__ == "__main__":
663679
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
"""Used for testing suppression of deprecation warnings while iterating
14+
over modules. The code is modeled after code in xmlbuilder.py in Python 3.6.
15+
See issue #542.
16+
"""
17+
import warnings
18+
19+
20+
class DeprecatedProperty:
21+
22+
def __get__(self, instance, cls):
23+
warnings.warn("async is deprecated", DeprecationWarning)
24+
return instance
25+
26+
27+
class DeprecationTest:
28+
locals()['async'] = DeprecatedProperty()

0 commit comments

Comments
 (0)