Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
Fix stray deprecation warnings
Browse files Browse the repository at this point in the history
Also don't rely on warnings being switched on by command line.

Fixes #17.
  • Loading branch information
hynek committed Aug 22, 2014
1 parent 4c0a6d9 commit 43323c2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 53 deletions.
90 changes: 44 additions & 46 deletions characteristic.py
Expand Up @@ -192,16 +192,44 @@ def __repr__(self):
)


def _ensure_attributes(attrs):
def _ensure_attributes(attrs, defaults):
"""
Return a list of :class:`Attribute` generated by creating new instances for
all non-Attributes.
"""
return [
Attribute(a, init_aliaser=None)
if not isinstance(a, Attribute) else a
for a in attrs
]
if defaults is not NOTHING:
defaults = defaults or {}
warnings.warn(
"`defaults` has been deprecated in 14.0, please use the "
"`Attribute` class instead.",
DeprecationWarning,
stacklevel=4,
)
else:
defaults = {}

rv = []
for attr in attrs:
if isinstance(attr, Attribute):
if defaults != {}:
raise ValueError(
"Mixing of the 'defaults' keyword argument and passing "
"instances of Attribute for 'attrs' is prohibited. "
"Please don't use 'defaults' anymore, it has been "
"deprecated in 14.0."
)
else:
rv.append(attr)
else:
rv.append(
Attribute(
attr,
init_aliaser=None,
default_value=defaults.get(attr, NOTHING)
)
)

return rv


def with_cmp(attrs):
Expand Down Expand Up @@ -293,7 +321,7 @@ def wrap(cl):
return cl

attrs = [a
for a in _ensure_attributes(attrs)
for a in _ensure_attributes(attrs, NOTHING)
if a.exclude_from_cmp is False]
return wrap

Expand Down Expand Up @@ -321,7 +349,7 @@ def wrap(cl):
return cl

attrs = [a
for a in _ensure_attributes(attrs)
for a in _ensure_attributes(attrs, NOTHING)
if a.exclude_from_repr is False]
return wrap

Expand Down Expand Up @@ -355,16 +383,6 @@ def with_init(attrs, **kw):
:param defaults: Default values if attributes are omitted on instantiation.
:type defaults: ``dict`` or ``None``
"""
if "defaults" not in kw:
defaults = {}
else:
defaults = kw["defaults"] or {}
warnings.warn(
"`defaults` has been deprecated in 14.0, please use the "
"`Attribute` class instead.",
DeprecationWarning
)

def characteristic_init(self, *args, **kw):
"""
Attribute initializer automatically created by characteristic.
Expand Down Expand Up @@ -403,32 +421,11 @@ def wrap(cl):
)
return cl

new_attrs = []
for a in attrs:
if isinstance(a, Attribute):
if defaults != {}:
raise ValueError(
"Mixing of the 'defaults' keyword argument and passing "
"instances of Attribute for 'attrs' is prohibited. "
"Please don't use 'defaults' anymore, it has been "
"deprecated in 14.0."
)
if not a.exclude_from_init:
new_attrs.append(a)
else:
default_value = defaults.get(a)
if default_value:
new_attrs.append(
Attribute(
a,
default_value=default_value,
init_aliaser=None,
)
)
else:
new_attrs.append(Attribute(a, init_aliaser=None))

attrs = new_attrs
attrs = [attr
for attr in _ensure_attributes(attrs,
defaults=kw.get("defaults",
NOTHING))
if attr.exclude_from_init is False]
return wrap


Expand All @@ -446,7 +443,7 @@ def immutable(attrs):
"""
# In this case, we just want to compare (native) strings.
attrs = frozenset(attr.name if isinstance(attr, Attribute) else attr
for attr in _ensure_attributes(attrs)
for attr in _ensure_attributes(attrs, NOTHING)
if attr.exclude_from_immutable is False)

def characteristic_immutability_sentry(self, attr, value):
Expand Down Expand Up @@ -529,6 +526,7 @@ def attributes(attrs, apply_with_cmp=True, apply_with_init=True,
"`create_init` has been deprecated in 14.0, please use "
"`apply_with_init`.", DeprecationWarning
)
attrs = _ensure_attributes(attrs, defaults=kw.get("defaults", NOTHING))

def wrap(cl):
if apply_with_repr is True:
Expand All @@ -540,6 +538,6 @@ def wrap(cl):
if apply_immutable is True:
cl = immutable(attrs)(cl)
if apply_with_init is True:
cl = with_init(attrs, defaults=kw.get("defaults"))(cl)
cl = with_init(attrs)(cl)
return cl
return wrap
4 changes: 3 additions & 1 deletion docs/changelog.rst
Expand Up @@ -26,7 +26,9 @@ Deprecations:
Changes:
^^^^^^^^

-
- Fix stray deprecation warnings.
- Don't rely on warnings being switched on by command line.
[`17 <https://github.com/hynek/characteristic/issues/17>`_]


14.0.0 (2014-08-21)
Expand Down
40 changes: 35 additions & 5 deletions test_characteristic.py
Expand Up @@ -18,6 +18,8 @@

PY2 = sys.version_info[0] == 2

warnings.simplefilter("always")


class TestAttribute(object):
def test_init_simple(self):
Expand Down Expand Up @@ -474,12 +476,12 @@ def test_deprecation_defaults(self):
Emits a DeprecationWarning if `defaults` is used.
"""
with warnings.catch_warnings(record=True) as w:
@attributes(["a"], create_init=False)
@with_init(["a"], defaults={"a": 42})
class C(object):
pass
assert (
'`create_init` has been deprecated in 14.0, please use '
'`apply_with_init`.'
'`defaults` has been deprecated in 14.0, please use the '
'`Attribute` class instead.'
) == w[0].message.args[0]
assert issubclass(w[0].category, DeprecationWarning)

Expand Down Expand Up @@ -598,23 +600,51 @@ class C(object):
) == w[0].message.args[0]
assert issubclass(w[0].category, DeprecationWarning)

def test_deprecation_defaults(self):
"""
Emits a DeprecationWarning if `defaults` is used.
"""
with warnings.catch_warnings(record=True) as w:
@attributes(["a"], defaults={"a": 42})
class C(object):
pass
assert (
'`defaults` has been deprecated in 14.0, please use the '
'`Attribute` class instead.'
) == w[0].message.args[0]
assert issubclass(w[0].category, DeprecationWarning)


class TestEnsureAttributes(object):
def test_leaves_attribute_alone(self):
"""
List items that are an Attribute stay an Attribute.
"""
a = Attribute("a")
assert a is _ensure_attributes([a])[0]
assert a is _ensure_attributes([a], {})[0]

def test_converts_rest(self):
"""
Any other item will be transformed into an Attribute.
"""
l = _ensure_attributes(["a"])
l = _ensure_attributes(["a"], {})
assert isinstance(l[0], Attribute)
assert "a" == l[0].name

def test_defaults(self):
"""
Legacy defaults are translated into default_value attributes.
"""
l = _ensure_attributes(["a"], {"a": 42})
assert 42 == l[0].default_value

def test_defaults_Attribute(self):
"""
Raises ValueError on defaults != {} and an Attribute within attrs.
"""
with pytest.raises(ValueError):
_ensure_attributes([Attribute("a")], defaults={"a": 42})


class TestImmutable(object):
def test_bare(self):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -6,7 +6,7 @@ deps =
pytest
pytest-cov
commands =
python -Walways setup.py test -a "--cov characteristic --cov-report term-missing"
python setup.py test -a "--cov characteristic --cov-report term-missing"

[testenv:flake8]
basepython = python2.7
Expand Down

0 comments on commit 43323c2

Please sign in to comment.