Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Added support for serializing class methods. - Refs #21290. #1775

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants
Member

loic commented Oct 18, 2013

The new handling allows us to do away with the whitelisting that was
required to support date and datetime objects.

Added support for serializing class methods. - Refs #21290.
The new handling allows us to do away with the whitelisting that was
required to support date and datetime objects.

@timgraham timgraham commented on the diff Oct 19, 2013

django/db/migrations/writer.py
@@ -146,18 +146,9 @@ def serialize(cls, value):
return cls.serialize_deconstructed(path, args, kwargs)
# Functions
elif isinstance(value, (types.FunctionType, types.BuiltinFunctionType)):
- # Special-cases, as these don't have im_class
- special_cases = [
- (datetime.datetime.now, "datetime.datetime.now", ["import datetime"]),
- (datetime.datetime.utcnow, "datetime.datetime.utcnow", ["import datetime"]),
- (datetime.date.today, "datetime.date.today", ["import datetime"]),
- ]
- for func, string, imports in special_cases:
- if func == value: # For some reason "utcnow is not utcnow"
- return string, set(imports)
- # Method?
- if hasattr(value, "im_class"):
- klass = value.im_class
+ # @classmethod?
+ if getattr(value, "__self__", None) and isinstance(value.__self__, type):
@timgraham

timgraham Oct 19, 2013

Owner

getattr->hasattr?

@loic

loic Oct 19, 2013

Member

getattr works well here, since we both want to check if __self__ exists (which means that func is a method), and that it's not None, which would be the case for an unbound method on PY2.

Owner

timgraham commented Oct 19, 2013

Thanks for the explanation, merged in 8d6953d.

@timgraham timgraham closed this Oct 19, 2013

Member

loic commented Oct 19, 2013

Thanks for the merge ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment