From 8aec45386aaf410a36bdf57ebf24cfe9f1fdf3b2 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 12 Jan 2012 08:59:36 -0700 Subject: Clarify extended if check in DeclarationDict.update_with_public. --- factory/containers.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/factory/containers.py b/factory/containers.py index 0218e47..847b2c0 100644 --- a/factory/containers.py +++ b/factory/containers.py @@ -104,6 +104,20 @@ class LazyStub(object): class DeclarationDict(dict): """Slightly extended dict to work with OrderedDeclaration.""" + def is_declaration(self, name, value): + """Determines if a class attribute is a field value declaration. + + Based on the name and value of the class attribute, return ``True`` if + it looks like a declaration of a default field value, ``False`` if it + is private (name starts with '_') or a classmethod or staticmethod. + + """ + if isinstance(value, classmethod): + return False + elif isinstance(value, declarations.OrderedDeclaration): + return True + return (not name.startswith("_")) + def update_with_public(self, d): """Updates the DeclarationDict from a class definition dict. @@ -114,10 +128,10 @@ class DeclarationDict(dict): """ remaining = {} for k, v in d.iteritems(): - if isinstance(v, classmethod) or k.startswith('_') and not isinstance(v, declarations.OrderedDeclaration): - remaining[k] = v - else: + if self.is_declaration(k, v): self[k] = v + else: + remaining[k] = v return remaining def copy(self, extra=None): -- cgit v1.2.3