Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deprecation warning filter #2546

Merged
merged 4 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
Union,
)
from urllib.parse import urlencode, urlunparse
from warnings import filterwarnings

from sanic_routing.exceptions import FinalizationError, NotFound
from sanic_routing.route import Route
Expand Down Expand Up @@ -105,8 +104,6 @@
if OS_IS_WINDOWS: # no cov
enable_windows_color_support()

filterwarnings("once", category=DeprecationWarning)


class Sanic(BaseSanic, StartupMixin, metaclass=TouchUpMeta):
"""
Expand Down
29 changes: 29 additions & 0 deletions sanic/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import annotations

import sys

from inspect import getmembers, isclass, isdatadescriptor
from os import environ
from pathlib import Path
from typing import Any, Callable, Dict, Optional, Sequence, Union
from warnings import filterwarnings

from sanic.constants import LocalCertCreator
from sanic.errorpages import DEFAULT_FORMAT, check_error_format
Expand All @@ -13,6 +16,20 @@
from sanic.utils import load_module_from_file_location, str_to_bool


if sys.version_info >= (3, 8):
from typing import Literal

FilterWarningType = Union[
Literal["default"],
Literal["error"],
Literal["ignore"],
Literal["always"],
Literal["module"],
Literal["once"],
]
else:
FilterWarningType = str

SANIC_PREFIX = "SANIC_"


Expand All @@ -22,6 +39,7 @@
"AUTO_EXTEND": True,
"AUTO_RELOAD": False,
"EVENT_AUTOREGISTER": False,
"DEPRECATION_FILTER": "once",
"FORWARDED_FOR_HEADER": "X-Forwarded-For",
"FORWARDED_SECRET": None,
"GRACEFUL_SHUTDOWN_TIMEOUT": 15.0, # 15 sec
Expand Down Expand Up @@ -72,6 +90,7 @@ class Config(dict, metaclass=DescriptorMeta):
AUTO_EXTEND: bool
AUTO_RELOAD: bool
EVENT_AUTOREGISTER: bool
DEPRECATION_FILTER: FilterWarningType
FORWARDED_FOR_HEADER: str
FORWARDED_SECRET: Optional[str]
GRACEFUL_SHUTDOWN_TIMEOUT: float
Expand Down Expand Up @@ -130,6 +149,7 @@ def __init__(
self.load_environment_vars(SANIC_PREFIX)

self._configure_header_size()
self._configure_warnings()
self._check_error_format()
self._init = True

Expand Down Expand Up @@ -178,6 +198,8 @@ def _post_set(self, attr, value) -> None:
self.LOCAL_CERT_CREATOR = LocalCertCreator[
self.LOCAL_CERT_CREATOR.upper()
]
elif attr == "DEPRECATION_FILTER":
self._configure_warnings()

@property
def FALLBACK_ERROR_FORMAT(self) -> str:
Expand Down Expand Up @@ -205,6 +227,13 @@ def _configure_header_size(self):
self.REQUEST_MAX_SIZE,
)

def _configure_warnings(self):
filterwarnings(
self.DEPRECATION_FILTER,
category=DeprecationWarning,
module=r"sanic.*",
)

def _check_error_format(self, format: Optional[str] = None):
check_error_format(format or self.FALLBACK_ERROR_FORMAT)

Expand Down
11 changes: 11 additions & 0 deletions tests/test_deprecation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import pytest

from sanic import Sanic
from sanic.log import deprecation


def test_deprecation():
message = r"\[DEPRECATION v9\.9\] hello"
with pytest.warns(DeprecationWarning, match=message):
deprecation("hello", 9.9)


@pytest.mark.parametrize(
"filter,expected",
(("default", 1), ("once", 1), ("ignore", 0)),
)
def test_deprecation_filter(app: Sanic, filter, expected, recwarn):
app.config.DEPRECATION_FILTER = filter
deprecation("hello", 9.9)
assert len(recwarn) == expected