Column default raises AttributeError when it takes a callable without __module__ attribute

Issue #3823 resolved
Hong Minhee created an issue

Since SQLAlchemy 1.1, Column raises AttributeError when its default option takes a callable having no __module__ attribute. The following example code had worked until SQLAlchemy 1.1:

created_at = Column(
    DateTime(timezone=True),
    default=functools.partial(datetime.datetime.now, datetime.timezone.utc)
)

The following traceback is from SQLAlchemy 1.1.0 (and I checked the same error on 1.1.1 as well):

Traceback (most recent call last):
  File "/.../ads/ad.py", line 112, in <module>
    class AdRevision(Base):
  File "/.../ads/ad.py", line 120, in AdRevision
    default=functools.partial(datetime.datetime.now, datetime.timezone.utc)
  File "/.../.env/lib/python3.5/site-packages/sqlalchemy/sql/schema.py", line 1210, in __init__
    args.append(ColumnDefault(self.default))
  File "/.../.env/lib/python3.5/site-packages/sqlalchemy/sql/schema.py", line 2016, in __init__
    arg = self._maybe_wrap_callable(arg)
  File "/.../.env/lib/python3.5/site-packages/sqlalchemy/sql/schema.py", line 2043, in _maybe_wrap_callable
    return util.wrap_callable(lambda ctx: fn(), fn)
  File "/.../.env/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", line 1401, in wrap_callable
    _f.__module__ = fn.__module__
AttributeError: 'functools.partial' object has no attribute '__module__'

Comments (5)

  1. Mike Bayer repo owner

    Check for module not present in util.wrap_callable()

    The newly added wrap_callable() function assumes module is present when this is not the case for objects such as functools.partial.

    Change-Id: Ia226260e9a65419e26d5c1f7187512f7fd4bb7c1 Fixes: #3823

    → <<cset 665b92d83f3a>>

  2. Log in to comment