[Sqlalchemy-tickets] Issue #3848: returning None (or anything) from @declared_attr means it won't b
Brought to you by:
zzzeek
From: Michael B. <iss...@bi...> - 2016-11-08 02:03:50
|
New issue 3848: returning None (or anything) from @declared_attr means it won't be called further, though this usage may be intuitive on AbstractConcreteBase https://bitbucket.org/zzzeek/sqlalchemy/issues/3848/returning-none-or-anything-from Michael Bayer: this is a conceptual one that might not be possible to fix. With AbstractConcreteBase, we're both "abstract" and "mapped", so the scheme below is intuitive. But because it returns None, the logic in _extract_mappable_attributes ("# using @declared_attr for some object that isn't Column/MapperProperty; remove from the dict_ and place the evaluated value onto the class.") blows it away. need to re-identify the rationale for the "remove the attribute" logic, determine if the "might return None" style is likely only with AbstractConcreteBase or in a more general sense, then identify a potential warning, or even behavioral change, to make this more intuitive. A special symbol like "don't map" might serve as a more explicit replacement for None. ``` #!python from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declared_attr from sqlalchemy.ext.declarative import AbstractConcreteBase Base = declarative_base() class FooBase(AbstractConcreteBase, Base): @declared_attr def foo(cls): print "FOO! ", cls.__name__ if cls.__name__ != 'A': #return Column(String) # gets discarded, but affects A return None # causes attr to not be called again else: return Column(Integer) class A(FooBase): __tablename__ = 'a' id = Column(Integer, primary_key=True) e = create_engine("sqlite://", echo=True) Base.metadata.create_all(e) ``` output: ``` #!python CREATE TABLE a ( id INTEGER NOT NULL, PRIMARY KEY (id) ) ``` 1.1 for the moment but this might be a more long term thing. |