Repository: Project-MONAI/MONAI
File: monai/__init__.py (Python)

Code before:
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import os
import sys
import logging
import warnings
from ._version import get_versions


old_showwarning = warnings.showwarning


def custom_warning_handler(message, category, filename, lineno, file=None, line=None):
    ignore_files = ["ignite/handlers/checkpoint", "modelopt/torch/quantization/tensor_quant"]
    if any(ignore in filename for ignore in ignore_files):
        return
    old_showwarning(message, category, filename, lineno, file, line)


class DeprecatedTypesWarningFilter(logging.Filter):
    def filter(self, record):
        deprecated_aliases = [
            "np.bool8",
            "np.object0",
            "np.int0",
            "np.uint0",
            "np.void0",
            "np.str0",
            "np.bytes0",
            "@validator",
            "@root_validator",
            "class-based `config`",
            "pkg_resources",
            "Implicitly cleaning up",
        ]
        for alias in deprecated_aliases:
            if alias in record.getMessage():
                return False
        return True


# workaround for https://github.com/Project-MONAI/MONAI/issues/8060
# Set the custom warning handler to filter warning
warnings.showwarning = custom_warning_handler
# Get the logger for warnings
logger = logging.getLogger("py.warnings")
# Create and add the filter to the logger

Review comment (suggestion):
the variable `deprecated_aliases` is named for the numpy deprecated objects. Maybe we can make this more general, such as `message_bodies_to_ignore`

Code after:
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import os
import sys
import logging
import warnings
from ._version import get_versions


old_showwarning = warnings.showwarning


def custom_warning_handler(message, category, filename, lineno, file=None, line=None):
    ignore_files = ["ignite/handlers/checkpoint", "modelopt/torch/quantization/tensor_quant"]
    if any(ignore in filename for ignore in ignore_files):
        return
    old_showwarning(message, category, filename, lineno, file, line)


class DeprecatedTypesWarningFilter(logging.Filter):
    def filter(self, record):
        message_bodies_to_ignore = [
            "np.bool8",
            "np.object0",
            "np.int0",
            "np.uint0",
            "np.void0",
            "np.str0",
            "np.bytes0",
            "@validator",
            "@root_validator",
            "class-based `config`",
            "pkg_resources",
            "Implicitly cleaning up",
        ]
        for message in message_bodies_to_ignore:
            if message in record.getMessage():
                return False
        return True


# workaround for https://github.com/Project-MONAI/MONAI/issues/8060
# TODO: remove this workaround after upstream fixed the warning
# Set the custom warning handler to filter warning
warnings.showwarning = custom_warning_handler
# Get the logger for warnings and add the filter to the logger
logging.getLogger("py.warnings").addFilter(DeprecatedTypesWarningFilter())