Allow types to be wrapped in functools.partial

Issue #1165 resolved
Former user created an issue

(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)

  1. Mike Bayer repo owner

    what happens if you just say:

    Column('name', Unicode())
    

    ?

    assuming a Unicode() instance is returned that should be fine.

  2. Mike Bayer 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.

  3. Former user 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.

  4. Mike Bayer 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.

  5. Log in to comment