Suppress Python MySQL Connector DeprecationWarning and ResourceWarnings In Unittests
I ran into warnings using mysql.connector. The looked liked this:
Code
/Users/user/workshop/venv/lib/python3.9/site-packages/mysql/connector/connection_cext.py:513: DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats
self._cmysql.query(query,
../usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py:353: ResourceWarning: unclosed <ssl.SSLSocket fd=8, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('###.###.###.###', 676767), raddr=('###.###.###.###', 443)>
obj, end = self.scan_once(s, idx)
ResourceWarning: Enable tracemalloc to get the object allocation traceback
And sometimes:
Code
sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=8, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('###.###.###.###', 676767), raddr=('###.###.###.###', 443)>
The fix was to import the warnings module, create a `ignore_warnings` function and add it as a decorator. Like this:
Code
#!/usr/bin/env python3
return
global
=
= 3
=
=
=
With that in place, the warnings were suppressed and I'd get just the results of the unittests.
Here's the post that gave me the solution.