From 34d1dee40796d5c53c5355bc88663d83eb69f9dc Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Thu, 6 Jan 2022 09:55:03 +0200 Subject: [PATCH] Add config.update support for setters (#2354) --- .github/workflows/codeql-analysis.yml | 8 ++++++-- .github/workflows/coverage.yml | 1 + .github/workflows/pr-bandit.yml | 1 + .github/workflows/pr-docs.yml | 1 + .github/workflows/pr-linter.yml | 1 + .github/workflows/pr-python310.yml | 1 + .github/workflows/pr-python37.yml | 1 + .github/workflows/pr-python38.yml | 1 + .github/workflows/pr-python39.yml | 1 + .github/workflows/pr-type-check.yml | 1 + .github/workflows/pr-windows.yml | 1 + sanic/config.py | 25 +++++++++++++++---------- tests/test_config.py | 25 ++++++++++++++++++++++--- tests/test_errorpages.py | 16 ++++++++++++++++ tests/test_pipelining.py | 16 ++++++---------- tests/test_server_loop.py | 2 +- 16 files changed, 76 insertions(+), 26 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 5108c24791..1113fa80f9 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -2,9 +2,13 @@ name: "CodeQL" on: push: - branches: [ main ] + branches: + - main + - "*LTS" pull_request: - branches: [ main ] + branches: + - main + - "*LTS" types: [opened, synchronize, reopened, ready_for_review] schedule: - cron: '25 16 * * 0' diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 92d93aa741..9b5834fa13 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -3,6 +3,7 @@ on: push: branches: - main + - "*LTS" tags: - "!*" # Do not execute on tags pull_request: diff --git a/.github/workflows/pr-bandit.yml b/.github/workflows/pr-bandit.yml index ca91312ae9..2bd702041b 100644 --- a/.github/workflows/pr-bandit.yml +++ b/.github/workflows/pr-bandit.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - main + - "*LTS" types: [opened, synchronize, reopened, ready_for_review] jobs: diff --git a/.github/workflows/pr-docs.yml b/.github/workflows/pr-docs.yml index 7b3c2f6ea6..8479aef547 100644 --- a/.github/workflows/pr-docs.yml +++ b/.github/workflows/pr-docs.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - main + - "*LTS" types: [opened, synchronize, reopened, ready_for_review] jobs: diff --git a/.github/workflows/pr-linter.yml b/.github/workflows/pr-linter.yml index 9ed45d0a82..11ad9d296e 100644 --- a/.github/workflows/pr-linter.yml +++ b/.github/workflows/pr-linter.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - main + - "*LTS" types: [opened, synchronize, reopened, ready_for_review] jobs: diff --git a/.github/workflows/pr-python310.yml b/.github/workflows/pr-python310.yml index f3f7c6075f..5e66deec18 100644 --- a/.github/workflows/pr-python310.yml +++ b/.github/workflows/pr-python310.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - main + - "*LTS" types: [opened, synchronize, reopened, ready_for_review] jobs: diff --git a/.github/workflows/pr-python37.yml b/.github/workflows/pr-python37.yml index 50f79c6e0c..c0051d3372 100644 --- a/.github/workflows/pr-python37.yml +++ b/.github/workflows/pr-python37.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - main + - "*LTS" types: [opened, synchronize, reopened, ready_for_review] jobs: diff --git a/.github/workflows/pr-python38.yml b/.github/workflows/pr-python38.yml index 1e0b8050bf..09e93f3ff5 100644 --- a/.github/workflows/pr-python38.yml +++ b/.github/workflows/pr-python38.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - main + - "*LTS" types: [opened, synchronize, reopened, ready_for_review] jobs: diff --git a/.github/workflows/pr-python39.yml b/.github/workflows/pr-python39.yml index 1abd6bcbe0..ff4794598e 100644 --- a/.github/workflows/pr-python39.yml +++ b/.github/workflows/pr-python39.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - main + - "*LTS" types: [opened, synchronize, reopened, ready_for_review] jobs: diff --git a/.github/workflows/pr-type-check.yml b/.github/workflows/pr-type-check.yml index 2fae03be67..c35479205f 100644 --- a/.github/workflows/pr-type-check.yml +++ b/.github/workflows/pr-type-check.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - main + - "*LTS" types: [opened, synchronize, reopened, ready_for_review] jobs: diff --git a/.github/workflows/pr-windows.yml b/.github/workflows/pr-windows.yml index 9721b5b5d0..050ee2bbef 100644 --- a/.github/workflows/pr-windows.yml +++ b/.github/workflows/pr-windows.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - main + - "*LTS" types: [opened, synchronize, reopened, ready_for_review] jobs: diff --git a/sanic/config.py b/sanic/config.py index 133efcdcb8..c5f05bf262 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -124,22 +124,27 @@ def __getattr__(self, attr): raise AttributeError(f"Config has no '{ke.args[0]}'") def __setattr__(self, attr, value) -> None: - if attr in self.__class__.__setters__: - try: - super().__setattr__(attr, value) - except AttributeError: - ... - else: - return None self.update({attr: value}) def __setitem__(self, attr, value) -> None: self.update({attr: value}) def update(self, *other, **kwargs) -> None: - other_mapping = {k: v for item in other for k, v in dict(item).items()} - super().update(*other, **kwargs) - for attr, value in {**other_mapping, **kwargs}.items(): + kwargs.update({k: v for item in other for k, v in dict(item).items()}) + setters: Dict[str, Any] = { + k: kwargs.pop(k) + for k in {**kwargs}.keys() + if k in self.__class__.__setters__ + } + + for key, value in setters.items(): + try: + super().__setattr__(key, value) + except AttributeError: + ... + + super().update(**kwargs) + for attr, value in {**setters, **kwargs}.items(): self._post_set(attr, value) def _post_set(self, attr, value) -> None: diff --git a/tests/test_config.py b/tests/test_config.py index a85bbf3916..81ae2fba35 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -5,7 +5,7 @@ from pathlib import Path from tempfile import TemporaryDirectory from textwrap import dedent -from unittest.mock import Mock +from unittest.mock import Mock, call import pytest @@ -385,5 +385,24 @@ def test_config_set_methods(app, monkeypatch): post_set.assert_called_once_with("FOO", 5) post_set.reset_mock() - app.config.update_config({"FOO": 6}) - post_set.assert_called_once_with("FOO", 6) + app.config.update({"FOO": 6}, {"BAR": 7}) + post_set.assert_has_calls( + calls=[ + call("FOO", 6), + call("BAR", 7), + ] + ) + post_set.reset_mock() + + app.config.update({"FOO": 8}, BAR=9) + post_set.assert_has_calls( + calls=[ + call("FOO", 8), + call("BAR", 9), + ], + any_order=True, + ) + post_set.reset_mock() + + app.config.update_config({"FOO": 10}) + post_set.assert_called_once_with("FOO", 10) diff --git a/tests/test_errorpages.py b/tests/test_errorpages.py index f8e425b0a1..f1e5fbd86a 100644 --- a/tests/test_errorpages.py +++ b/tests/test_errorpages.py @@ -334,6 +334,22 @@ async def start(app, _): assert response.content_type == "text/plain; charset=utf-8" +def test_config_fallback_using_update_dict(app): + app.config.update({"FALLBACK_ERROR_FORMAT": "text"}) + + _, response = app.test_client.get("/error") + assert response.status == 500 + assert response.content_type == "text/plain; charset=utf-8" + + +def test_config_fallback_using_update_kwarg(app): + app.config.update(FALLBACK_ERROR_FORMAT="text") + + _, response = app.test_client.get("/error") + assert response.status == 500 + assert response.content_type == "text/plain; charset=utf-8" + + def test_config_fallback_bad_value(app): message = "Unknown format: fake" with pytest.raises(SanicException, match=message): diff --git a/tests/test_pipelining.py b/tests/test_pipelining.py index 2bb29c5286..6c9987568e 100644 --- a/tests/test_pipelining.py +++ b/tests/test_pipelining.py @@ -62,19 +62,15 @@ async def handler(request): data = ["hello", "world"] - class Data(AsyncByteStream): - def __init__(self, data): - self.data = data - - async def __aiter__(self): - for value in self.data: - yield value.encode("utf-8") - client = ReusableClient(app, port=1234) + async def stream(data): + for value in data: + yield value.encode("utf-8") + with client: - _, response1 = client.post("/", data=Data(data)) - _, response2 = client.post("/", data=Data(data)) + _, response1 = client.post("/", data=stream(data)) + _, response2 = client.post("/", data=stream(data)) assert response1.status == response2.status == 200 assert response1.json["data"] == response2.json["data"] == data diff --git a/tests/test_server_loop.py b/tests/test_server_loop.py index fbd5cc2bb3..30077178f2 100644 --- a/tests/test_server_loop.py +++ b/tests/test_server_loop.py @@ -4,8 +4,8 @@ import pytest -from sanic.server import loop from sanic.compat import OS_IS_WINDOWS, UVLOOP_INSTALLED +from sanic.server import loop @pytest.mark.skipif(