chore(descriptor): rename descriptor class

rename `DefaultProperty` to `Default`
This commit is contained in:
mpa 2020-05-30 21:00:20 +04:00
parent a2e85c5a42
commit 41dd03ff05
No known key found for this signature in database
GPG key ID: BCCFBFCCC9B754A8
3 changed files with 28 additions and 13 deletions

View file

@ -8,7 +8,7 @@ from typing import Any, AsyncGenerator, Callable, ClassVar, Optional, Type, Type
from aiogram.utils.exceptions import TelegramAPIError
from ....utils.helper import DefaultProperty
from ....utils.helper import Default
from ...methods import Response, TelegramMethod
from ..telegram import PRODUCTION, TelegramAPIServer
@ -21,10 +21,10 @@ class BaseSession(abc.ABC):
# global session timeout
default_timeout: ClassVar[float] = 60.0
api: DefaultProperty[TelegramAPIServer] = DefaultProperty(PRODUCTION)
json_loads: DefaultProperty[_JsonLoads] = DefaultProperty(json.loads)
json_dumps: DefaultProperty[_JsonDumps] = DefaultProperty(json.dumps)
timeout: DefaultProperty[float] = DefaultProperty(
api: Default[TelegramAPIServer] = Default(PRODUCTION)
json_loads: Default[_JsonLoads] = Default(json.loads)
json_dumps: Default[_JsonDumps] = Default(json.dumps)
timeout: Default[float] = Default(
fget=lambda self: float(self.__class__.default_timeout)
)

View file

@ -238,9 +238,24 @@ class OrderedHelper(Helper, metaclass=OrderedHelperMeta):
return result
class DefaultProperty(Generic[T]):
class Default(Generic[T]):
"""
Descriptor that holds default value and for a class and
Descriptor that holds default value getter
Example:
>>> class MyClass:
... att = Default("dflt")
...
>>> my_instance = MyClass()
>>> my_instance.att = "not dflt"
>>> my_instance.att
'not dflt'
>>> MyClass.att
'dflt'
>>> del my_instance.att
>>> my_instance.att
'dflt'
>>>
Intended to be used as a class attribute and only internally.
"""

View file

@ -1,6 +1,6 @@
import pytest
from aiogram.utils.helper import DefaultProperty, Helper, HelperMode, Item, ListItem, OrderedHelper
from aiogram.utils.helper import Default, Helper, HelperMode, Item, ListItem, OrderedHelper
class TestHelper:
@ -138,7 +138,7 @@ class TestDefaultDescriptor:
def test_descriptor_fs(self):
obj = type("ClassA", (), {})()
default_x_val = "some_x"
x = DefaultProperty(default_x_val)
x = Default(default_x_val)
# we can omit owner, usually it's just obj.__class__
assert x.__get__(instance=obj, owner=None) == default_x_val
@ -162,20 +162,20 @@ class TestDefaultDescriptor:
def test_init(self):
class A:
x = DefaultProperty(fget=lambda a_inst: "nothing")
x = Default(fget=lambda a_inst: "nothing")
assert isinstance(A.__dict__["x"], DefaultProperty)
assert isinstance(A.__dict__["x"], Default)
a = A()
assert a.x == "nothing"
x = DefaultProperty("x")
x = Default("x")
assert x.__get__(None, None) == "x"
assert x.fget(None) == x.__get__(None, None)
def test_nullability(self):
class A:
x = DefaultProperty(default=None, fget=None)
x = Default(default=None, fget=None)
assert A.x is None
assert A().x is None