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

Code before:
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 warnings
from ._version import get_versions


def custom_warning_handler(message, category, filename, lineno, file=None, line=None):
    filename1 = "ignite/handlers/checkpoint.py"
    filename2 = "modelopt/torch/quantization/tensor_quant.py"
    if filename1 in filename or filename2 in filename:
        return
    warnings.showwarning(message, category, filename, lineno, file, line)


warnings.showwarning = custom_warning_handler
PY_REQUIRED_MAJOR = 3
PY_REQUIRED_MINOR = 9

version_dict = get_versions()
__version__: str = version_dict.get("version", "0+unknown")
__revision_id__: str = version_dict.get("full-revisionid")
del get_versions, version_dict

__copyright__ = "(c) MONAI Consortium"

__basedir__ = os.path.dirname(__file__)

if sys.version_info.major != PY_REQUIRED_MAJOR or sys.version_info.minor < PY_REQUIRED_MINOR:
    import warnings

    warnings.warn(
        f"MONAI requires Python {PY_REQUIRED_MAJOR}.{PY_REQUIRED_MINOR} or higher. "
        f"But the current Python is: {sys.version}",
        category=RuntimeWarning,
    )

from .utils.module import load_submodules  # noqa: E402

# handlers_* have some external decorators the users may not have installed
# *.so files and folder "_C" may not exist when the cpp extensions are not compiled

Review comment (suggestion):
```suggestion
old_showwarning = warnings.showwarning
def custom_warning_handler(message, category, filename, lineno, file=None, line=None):
    filename1 = "ignite/handlers/checkpoint.py"
    filename2 = "modelopt/torch/quantization/tensor_quant.py"
    if filename1 in filename or filename2 in filename:
        return
    old_showwarning(message, category, filename, lineno, file, line)


warnings.showwarning = custom_warning_handler
```
This might work, or call `formatwarning` instead.

Code after:
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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