1

My application is flooded with warnings from a 3rd package

transformers/modeling_deberta.py:18: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working
from collections import Sequence

How can I suppress those warnings?

I've tried:

export PYTHONWARNINGS="ignore::DeprecationWarning"
warnings.filterwarnings(action="ignore")
warnings.filterwarnings(action="ignore", category=DeprecationWarning)
warnings.filterwarnings(action="ignore", category=DeprecationWarning, module=r".*transformers.*")
warnings.filterwarnings(action="ignore", category=DeprecationWarning, module=r".*collections.*")
warnings.filterwarnings(action="ignore", message=r".*collections.abc.*")

Update The following options are not feasible:

  • Remove the 3rd package that generates those warnings. It is irreplaceable.
  • Downgrade to python 3.3

Maybe I should just wait for the 3rd package to upgrade. Just wonder if there is any other option to suppress specific 3rd party warnings in python.

phi
  • 10,572
  • 3
  • 21
  • 30
  • 1
    Just change things like `from collections import Sequence` to `from collections.abc import Sequence`. If the code can't be modified, then you can always roll back to an earlier Python release. At some point you'll need to do one or the other, since it will cease to be a warning and will simply fail if you upgrade to 3.9. – Tom Karzes Nov 10 '20 at 10:49

2 Answers2

2

The warning tells you that you are getting some resources from a location which was correct prior to Python 3.3 and will not work at all starting from Python 3.9. You are using a Python version between 3.3 and 3.9, which means that this will still work for you for the time being, but you will need to refactor your code so you import ABCs from collections.abc instead of from collections. Unless you refactor your code in the manner the error suggests, you will be stuck with Python versions prior to 3.9, which limits your possibilities, will disallow the usage of any goodies implemented after those versions and will increasingly see libraries incompatible with your project due to they being too modern for your project.

You can get rid of the warnings by downgrading your project to a Python version prior to 3.3, but that's a direction you should strive to avoid if possible. The best solution is to refactor your project to comply to the terms of modern Python versions and if you use packages that prevent you from doing so, then you might want to upgrade those packages. If there is no upgrade that would solve this issue, then it is well worth asking your question whether it is a higher cost to implement their functionality in a more modern way in terms of labor on your part or is it a higher cost in terms of technological shortage if you get stuck with old Python versions.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 1
    Thanks for your detail answer. Unfortunately those options are not feasible for my project. I updated my question. – phi Nov 10 '20 at 14:01
  • @phi I understand your point. Since you are stuck with your version below a certain level, but above another certain level, precisely inside an interval where these warnings are always shown, it gets frutstrating over time. I maintain that my answer is correct, even though you do not have the option to apply it at the moment. While you are restricted in your options, you might want to suppress warnings. See this answer for more information: https://stackoverflow.com/questions/24572689/suppress-warning-without-modifying-third-party-code – Lajos Arpad Nov 10 '20 at 14:07
1

I found my answer from here

Solution: make sure the following code runs before the 3rd package import. If multiprocessing is used, the code has to be called in each process.

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    from collections import Sequence
phi
  • 10,572
  • 3
  • 21
  • 30