From 7c85d68de2d126658786fce2fd26a867378abc46 Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Fri, 30 Jan 2015 13:25:59 +0100 Subject: [PATCH] Verify amount of arguments passed to Attribute --- attr/_make.py | 2 ++ tests/test_make.py | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/attr/_make.py b/attr/_make.py index 0c48d876..8ca46926 100644 --- a/attr/_make.py +++ b/attr/_make.py @@ -350,6 +350,8 @@ class Attribute(object): ] # we can't use ``attrs`` so we have to cheat a little. def __init__(self, **kw): + if len(kw) > len(Attribute._attributes): + raise TypeError("Too many arguments.") try: for a in Attribute._attributes: setattr(self, a, kw[a]) diff --git a/tests/test_make.py b/tests/test_make.py index 60f53424..973610d4 100644 --- a/tests/test_make.py +++ b/tests/test_make.py @@ -19,7 +19,7 @@ from attr._make import ( ) -class TestAttr(object): +class TestCountingAttr(object): """ Tests for `attr`. """ @@ -190,12 +190,21 @@ class TestAttribute(object): """ def test_missing_argument(self): """ - Raises TypeError if an Argument is missing. + Raises `TypeError` if an Argument is missing. """ with pytest.raises(TypeError) as e: - Attribute(default=NOTHING, factory=NOTHING, validator=None) + Attribute(default=NOTHING, validator=None) assert ("Missing argument 'name'.",) == e.value.args + def test_too_many_arguments(self): + """ + Raises `TypeError` if extra arguments are passed. + """ + with pytest.raises(TypeError) as e: + Attribute(name="foo", default=NOTHING, + factory=NOTHING, validator=None) + assert ("Too many arguments.",) == e.value.args + class TestMakeClass(object): """