83 lines
2.8 KiB
Python
83 lines
2.8 KiB
Python
from pathlib import Path
|
|
|
|
from clint.config import Config
|
|
from clint.index import SymbolIndex
|
|
from clint.linter import Position, Range, lint_file
|
|
from clint.rules import ForbiddenDeprecationWarning
|
|
|
|
|
|
def test_forbidden_deprecation_warning(index: SymbolIndex) -> None:
|
|
code = """
|
|
import warnings
|
|
|
|
# Bad - should be flagged
|
|
warnings.warn("message", category=DeprecationWarning)
|
|
warnings.warn(
|
|
"multiline message",
|
|
category=DeprecationWarning,
|
|
stacklevel=2
|
|
)
|
|
|
|
# Good - should not be flagged
|
|
warnings.warn("message", category=FutureWarning)
|
|
warnings.warn("message", category=UserWarning)
|
|
warnings.warn("message") # no category specified
|
|
warnings.warn("message", stacklevel=2) # no category specified
|
|
other_function("message", category=DeprecationWarning) # not warnings.warn
|
|
"""
|
|
config = Config(select={ForbiddenDeprecationWarning.name})
|
|
results = lint_file(Path("test.py"), code, config, index)
|
|
assert len(results) == 2
|
|
assert all(isinstance(r.rule, ForbiddenDeprecationWarning) for r in results)
|
|
assert results[0].range == Range(Position(4, 34)) # First warnings.warn call
|
|
assert results[1].range == Range(Position(7, 13)) # Second warnings.warn call
|
|
|
|
|
|
def test_forbidden_deprecation_warning_import_variants(index: SymbolIndex) -> None:
|
|
code = """
|
|
import warnings
|
|
from warnings import warn
|
|
import warnings as w
|
|
|
|
# All of these should be flagged
|
|
warnings.warn("message", category=DeprecationWarning)
|
|
warn("message", category=DeprecationWarning)
|
|
w.warn("message", category=DeprecationWarning)
|
|
"""
|
|
config = Config(select={ForbiddenDeprecationWarning.name})
|
|
results = lint_file(Path("test.py"), code, config, index)
|
|
assert len(results) == 3
|
|
assert all(isinstance(r.rule, ForbiddenDeprecationWarning) for r in results)
|
|
|
|
|
|
def test_forbidden_deprecation_warning_parameter_order(index: SymbolIndex) -> None:
|
|
code = """
|
|
import warnings
|
|
|
|
# Different parameter orders - should be flagged
|
|
warnings.warn("message", category=DeprecationWarning)
|
|
warnings.warn(category=DeprecationWarning, message="test")
|
|
"""
|
|
config = Config(select={ForbiddenDeprecationWarning.name})
|
|
results = lint_file(Path("test.py"), code, config, index)
|
|
assert len(results) == 2
|
|
assert all(isinstance(r.rule, ForbiddenDeprecationWarning) for r in results)
|
|
|
|
|
|
def test_forbidden_deprecation_warning_positional_args(index: SymbolIndex) -> None:
|
|
code = """
|
|
import warnings
|
|
|
|
# Positional arguments - should be flagged
|
|
warnings.warn("message", DeprecationWarning)
|
|
warnings.warn("message", DeprecationWarning, 2)
|
|
|
|
# Good - should not be flagged
|
|
warnings.warn("message", FutureWarning)
|
|
warnings.warn("message") # no category specified
|
|
"""
|
|
config = Config(select={ForbiddenDeprecationWarning.name})
|
|
results = lint_file(Path("test.py"), code, config, index)
|
|
assert len(results) == 2
|
|
assert all(isinstance(r.rule, ForbiddenDeprecationWarning) for r in results)
|