Allow types to be wrapped in functools.partial
(original reporter: ged) Currently (r5104), the following code:
from functools import partial
from sqlalchemy import *
metadata = MetaData('sqlite:///')
Unicode = partial(Unicode, assert_unicode=None)
thing_table = Table('thing', metadata,
Column('name', Unicode, primary_key=True)
)
produce this traceback:
Traceback (most recent call last):
File "test_partial.py", line 9, in <module>
Column('name', Unicode, primary_key=True)
File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/schema.py", line 113, in __call__
return type.__call__(self, name, metadata, *args, **kwargs)
File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/schema.py", line 246, in __init__
self.__post_init(*args, **kwargs)
File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/schema.py", line 292, in __post_init
self._init_items(*args)
File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/schema.py", line 54, in _init_items
item._set_parent(self)
File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/schema.py", line 691, in _set_parent
self._init_items(*toinit)
File "/home/ged/devel/sqlalchemy/trunk/lib/sqlalchemy/schema.py", line 54, in _init_items
item._set_parent(self)
AttributeError: 'functools.partial' object has no attribute '_set_parent'
This issue was initially reported at: http://elixir.ematia.de/trac/ticket/72
Comments (9)
-
repo owner -
Account Deleted Yes, that works. So should this trivial patch.
-
repo owner I was suggesting that you just call
Unicode()
in your own application code. That patch strikes me as extremely specific to an arbitrary use case. There's lots of ways to do partials in Python. -
Account Deleted - attached partial.diff
let to_instance handle any callable, not just types
-
Account Deleted There are other ways to do partial application, but
partial
is the standard. The use case is whenever there are several columns that need the same parameters.However, I agree the solution isn't to add another special case. The above patch lets
to_instance
handle any callable by removing the current special treatment of types. -
repo owner - changed milestone to 0.5.0
agreed on the use case to use partials, I meant the specific technique of using functools.partial. the new patch is much more agreeable, will try soon.
-
Account Deleted - changed watchers to ged@openhex.org
(original author: ged)
-
- changed status to resolved
-
repo owner - removed milestone
Removing milestone: 0.5.0 (automated comment)
- Log in to comment
what happens if you just say:
?
assuming a Unicode() instance is returned that should be fine.