Skip to content

Commit

Permalink
Move __setattr__ for __fake_slots__ check into base calss
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Mar 17, 2021
1 parent 4e13e6e commit 3e58c86
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 40 deletions.
13 changes: 0 additions & 13 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
Union,
)
from urllib.parse import urlencode, urlunparse
from warnings import warn

from sanic_routing.exceptions import FinalizationError # type: ignore
from sanic_routing.exceptions import NotFound # type: ignore
Expand Down Expand Up @@ -185,18 +184,6 @@ def __init__(
if dumps:
BaseHTTPResponse._dumps = dumps

def __setattr__(self, name: str, value: Any) -> None:
# This is a temporary compat layer so we can raise a warning until
# setting attributes on the app instance can be removed and deprecated
# with a proper implementation of __slots__
if name not in self.__fake_slots__:
warn(
"Setting variables on application instances is deprecated and "
"will be removed in version 21.9. You should change your "
f"application to use app.ctx.{name} instead."
)
super().__setattr__(name, value)

@property
def loop(self):
"""
Expand Down
18 changes: 18 additions & 0 deletions sanic/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Any, Tuple
from warnings import warn

from sanic.mixins.exceptions import ExceptionMixin
from sanic.mixins.listeners import ListenerMixin
from sanic.mixins.middleware import MiddlewareMixin
Expand Down Expand Up @@ -35,8 +38,23 @@ class BaseSanic(
SignalMixin,
metaclass=Base,
):
__fake_slots__: Tuple[str, ...]

def __str__(self) -> str:
return f"<{self.__class__.__name__} {self.name}>"

def __repr__(self) -> str:
return f'{self.__class__.__name__}(name="{self.name}")'

def __setattr__(self, name: str, value: Any) -> None:
# This is a temporary compat layer so we can raise a warning until
# setting attributes on the app instance can be removed and deprecated
# with a proper implementation of __slots__
if name not in self.__fake_slots__:
warn(
f"Setting variables on {self.__class__.__name__} instances is "
"deprecated and will be removed in version 21.9. You should "
f"change your {self.__class__.__name__} instance to use "
f"instance.ctx.{name} instead."
)
super().__setattr__(name, value)
24 changes: 1 addition & 23 deletions sanic/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@

from collections import defaultdict
from types import SimpleNamespace
from typing import (
TYPE_CHECKING,
Any,
Dict,
Iterable,
List,
Optional,
Set,
Union,
)
from warnings import warn
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Set, Union

from sanic_routing.exceptions import NotFound # type: ignore
from sanic_routing.route import Route # type: ignore
Expand Down Expand Up @@ -115,18 +105,6 @@ def __repr__(self) -> str:
)
return f"Blueprint({args})"

def __setattr__(self, name: str, value: Any) -> None:
# This is a temporary compat layer so we can raise a warning until
# setting attributes on the app instance can be removed and deprecated
# with a proper implementation of __slots__
if name not in self.__fake_slots__:
warn(
"Setting variables on blueprint instances is deprecated and "
"will be removed in version 21.9. You should change your "
f"blueprint to use bp.ctx.{name} instead."
)
super().__setattr__(name, value)

@property
def apps(self):
if not self._apps:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ def test_app_set_attribute_warning(app):

assert len(record) == 1
assert record[0].message.args[0] == (
"Setting variables on application instances is deprecated "
"Setting variables on Sanic instances is deprecated "
"and will be removed in version 21.9. You should change your "
"application to use app.ctx.foo instead."
"Sanic instance to use instance.ctx.foo instead."
)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ def test_bp_set_attribute_warning():

assert len(record) == 1
assert record[0].message.args[0] == (
"Setting variables on blueprint instances is deprecated "
"Setting variables on Blueprint instances is deprecated "
"and will be removed in version 21.9. You should change your "
"blueprint to use bp.ctx.foo instead."
"Blueprint instance to use instance.ctx.foo instead."
)

0 comments on commit 3e58c86

Please sign in to comment.