pytorch-lightning

Форк
0
149 строк · 5.5 Кб
1
# Copyright The Lightning AI team.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
"""Test that the warnings actually appear and they have the correct `stacklevel`
15

16
Needs to be run outside of `pytest` as it captures all the warnings.
17

18
"""
19

20
import importlib
21
import inspect
22
import os
23
import sys
24
import warnings
25
from contextlib import redirect_stderr
26
from io import StringIO
27
from pathlib import Path
28
from unittest import mock
29

30
import lightning.fabric
31
import pytest
32
from lightning.fabric.utilities.rank_zero import rank_zero_deprecation, rank_zero_warn
33
from lightning.fabric.utilities.warnings import (
34
    PossibleUserWarning,
35
    _is_path_in_lightning,
36
    disable_possible_user_warnings,
37
)
38
from lightning_utilities.core.rank_zero import WarningCache, _warn
39
from lightning_utilities.test.warning import no_warning_call
40

41

42
def line_number():
43
    return inspect.currentframe().f_back.f_lineno
44

45

46
if __name__ == "__main__":
47
    stderr = StringIO()
48
    # recording
49
    with redirect_stderr(stderr):
50
        base_line = line_number() + 1
51
        _warn("test1")
52
        _warn("test2", category=DeprecationWarning)
53

54
        rank_zero_warn("test3")
55
        rank_zero_warn("test4", category=DeprecationWarning)
56

57
        rank_zero_deprecation("test5")
58

59
        cache = WarningCache()
60
        cache.warn("test6")
61
        cache.deprecation("test7")
62

63
    output = stderr.getvalue()
64
    expected_lines = [
65
        f"test_warnings.py:{base_line}: test1",
66
        f"test_warnings.py:{base_line + 1}: test2",
67
        f"test_warnings.py:{base_line + 3}: test3",
68
        f"test_warnings.py:{base_line + 4}: test4",
69
        f"test_warnings.py:{base_line + 6}: test5",
70
        f"test_warnings.py:{base_line + 9}: test6",
71
        f"test_warnings.py:{base_line + 10}: test7",
72
    ]
73

74
    for ln in expected_lines:
75
        assert ln in output, f"Missing line {ln!r} in:\n{output}"
76
        assert "rank_zero_warn(" not in output, "customized warning wrapper is not used"
77

78
    # check that logging is properly configured
79
    import logging
80

81
    root_logger = logging.getLogger()
82
    lightning_logger = logging.getLogger("lightning.fabric")
83
    # should have a `StreamHandler`
84
    assert lightning_logger.hasHandlers()
85
    assert len(lightning_logger.handlers) == 1
86
    # set our own stream for testing
87
    handler = lightning_logger.handlers[0]
88
    assert isinstance(handler, logging.StreamHandler)
89
    stderr = StringIO()
90
    # necessary with `propagate = False`
91
    lightning_logger.handlers[0].stream = stderr
92

93
    # necessary with `propagate = True`
94
    with redirect_stderr(stderr):
95
        # Lightning should not configure the root `logging` logger by default
96
        logging.info("test1")
97
        root_logger.info("test1")
98
        # but our logger instance
99
        lightning_logger.info("test2")
100
        # level is set to INFO
101
        lightning_logger.debug("test3")
102

103
    output = stderr.getvalue()
104
    assert output == "test2\n", repr(output)
105

106

107
def test_is_path_in_lightning(monkeypatch, tmp_path):
108
    monkeypatch.chdir(tmp_path)
109
    if sys.platform != "win32":
110
        assert _is_path_in_lightning(Path("/a/b/c/lightning"))
111
        assert _is_path_in_lightning(Path("/a/b/c/lightning/core/lightning.py"))
112
        assert _is_path_in_lightning(Path("/a/b/c/lightning/lightning"))
113
        assert not _is_path_in_lightning(Path("/a/b/c/"))
114
        # The following statements should assert the opposite for correctness, but a naive implementation of
115
        # `_is_path_in_lightning` was requested, thus it cannot handle these cases
116
        assert _is_path_in_lightning(Path(""))
117
        assert _is_path_in_lightning(Path("/a/b/lightning"))
118
        assert _is_path_in_lightning(Path("a/b/c/lightning"))
119
    else:
120
        assert _is_path_in_lightning(Path(r"C:\a\b\c\lightning"))
121
        assert _is_path_in_lightning(Path(r"C:\a\b\c\lightning\core\lightning.py"))
122
        assert _is_path_in_lightning(Path(r"C:\a\b\c\lightning\lightning"))
123
        assert not _is_path_in_lightning(Path(r"\a\b\c"))
124
        assert not _is_path_in_lightning(Path(r"C:\a\b\c"))
125
        # The following statements should assert the opposite for correctness, but a naive implementation of
126
        # `_is_path_in_lightning` was requested, thus it cannot handle these cases
127
        assert _is_path_in_lightning(Path(r"D:\a\b\c\lightning"))  # drive letter mismatch
128

129

130
def test_disable_possible_user_warnings():
131
    with pytest.warns(PossibleUserWarning):
132
        warnings.warn("test", PossibleUserWarning)
133
    disable_possible_user_warnings()
134
    with no_warning_call(PossibleUserWarning):
135
        warnings.warn("test", PossibleUserWarning)
136
    warnings.resetwarnings()
137

138

139
@pytest.mark.parametrize("setting", ["0", "off"])
140
@mock.patch.dict(os.environ, {}, clear=True)
141
def test_disable_possible_user_warnings_from_environment(setting):
142
    with pytest.warns(PossibleUserWarning):
143
        warnings.warn("test", PossibleUserWarning)
144
    os.environ["POSSIBLE_USER_WARNINGS"] = setting
145
    importlib.reload(lightning.fabric)
146

147
    with no_warning_call(PossibleUserWarning):
148
        warnings.warn("test", PossibleUserWarning)
149
    warnings.resetwarnings()
150

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.