Column default raises AttributeError when it takes a callable without __module__ attribute
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)
-
repo owner -
repo owner here's the PR: https://github.com/zzzeek/sqlalchemy/pull/204 . this PR was one of many that inspired me to move to Gerrit for things like this so this kind of change will be better tracked in the future.
-
repo owner -
repo owner - marked as critical
-
repo owner - changed status to resolved
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>>
- Log in to comment
here's the changeset: c7d04beeac6ad54d638afb01783dee2d769aef9d I vaguely recall this but need to see if there was a PR or something.