- changed milestone to 0.8.0
Better introspection for callable `default`
I've been caught by surprise more than once by the fact that a callable default
is interpreted differently depending on whether its signature accepts zero or more than zero parameters; in the second case it is assumed to be a context-sensitive default function and it is called by passing the context. This assumption fails for callables that take parameters but they are all optional. The current workaround is to wrap the function in a parameterless lambda but it shouldn't be hard to tweak the introspection so that callables with only optional parameters are called without parameters (i.e. not passing context).
Comments (7)
-
repo owner -
reporter Don't context-sensitive functions require the context? Can't think of why would one define my_default(context=None).
Use case is callables that weren't defined specifically to be used as column defaults, they are used in other places with their non default values. For example:
def random_token(min_length=64, max_length=64, alphabet=string.letters + string.digits): """Generate a random token""" ... access_token = Column(Unicode(64), default=random_token)
-
repo owner Yeah I think that usage of
random_token
should be wrapped. Ideally the callable passed todefault
would have exactly one signature, but because usage ofcontext
is such the rare case we have the introspection thing happening. -
reporter Indeed it would be better if introspection was avoided but given that it is happening, I don't really see the harm in supporting better the common case (context-less callable). I could whip up a patch if it makes a difference.
-
repo owner - changed status to resolved
there's already lots of expectation of callables with keyword arguments including tests, so I'm not about to reverse course on all of that right now even though I don't think I'd ever design the API this way now. So ce1b80ad08f58ea18914a93805754a5e19a85abb is just a tweak in 0.8.
-
reporter Thanks a lot!
-
repo owner - removed milestone
Removing milestone: 0.8.0b1 (automated comment)
- Log in to comment
right but that would break backwards compatibility if someone is using it with a callable like
def my_default(context=None)
. What's the use case for passing a callable that isn't exactlymy_default()
ormy_default(ctx)
? if anything the introspection should just raise if it isn't exactly those signatures (guess it is).