Re: [sqlalchemy] testing for an association proxy (possible bug and patch included)

2013-08-26 Thread Michael Bayer

On Aug 23, 2013, at 6:34 PM, jason kirtland j...@discorporate.us wrote:

 
 The patch seems like surprising Python behavior to me. Traversing across a 
 None is almost certainly a bug in regular code, and quashing that error by 
 default feels dangerous. I would want this to raise by default (and I have 
 found bugs because it did.)  I think you could opt into this behavior by 
 supplying an alternate, custom getter function that quashed None when 
 creating the proxy.

Looking at the tests in this area, I can see that they expect an error to be 
raised on scalar access of a None; though the tests in this area have 
apparently been long broken since we moved to nosetests, where we've lost 
access to the fail() method.

I find it surprising that we would want to raise on a None, because the purpose 
of association proxy is to proxy access to an attribute.  If the underlying 
attribute is returning None and that's not an error, then the association proxy 
itself should return None.I've spent some time trying to figure a 
legitimate error case I'd want raised here, can you provide one ?   Otherwise 
I'm leaning towards having the proxy faithfully proxy out the underlying None 
and I'd backport that at least to 0.8 if not 0.7.   


signature.asc
Description: Message signed with OpenPGP using GPGMail


[sqlalchemy] testing for an association proxy (possible bug and patch included)

2013-08-23 Thread Jonathan Vanasco
I have this general structure:

class Person:
 # orm relationships are preceded by (o)ne or (l)ist  
 o_Person2Address_ActiveShipping = sa.orm.relationship( 
Person2Address, primaryjoin=and_( Person2Address.person_id==Person.id , 
Person2Address.role_id=='active-shipping' ), uselist=False ) 
active_shipping_address = association_proxy('
o_Person2Address_ActiveShipping', 'address')

class Person2Address:
address = sa.orm.relationship(Address, 
primaryjoin=Person2Address.address_id==Address.id)

class Address:
   pass

this works perfect when i have a Person2Address and address .  I'd imagine 
it works fine if the proxy is for an empty list too.

the problem is when o_Person2Address_ActiveShipping is an empty scalar 
(from the uselist=False argument).

   jim = dbSession.query( Person )
   active_shipping = jim.o_Person2Address_ActiveShipping 
   type(active_shipping)
None

   # this will raise an error
   if jim.active_shipping_address :

  # this will raise an error too
   if jim.active_shipping_address 
and jim.active_shipping_address.address :

  print jim.active_shipping_address

that raises an error on the .active_shipping_address

File 
/Users/jvanasco/webserver/environments/project-2.7.5/lib/python2.7/site-packages/sqlalchemy/ext/associationproxy.py,
 line 241, in __get__
return self._scalar_get(getattr(obj, self.target_collection))
AttributeError: 'NoneType' object has no attribute 'media_asset'



i think a simple fix could be something like this ( line 240, 
sqlalchemy/ext/associationproxy.py 
)

if self.scalar:
-if not getattr(obj, self.target_collection)
-return self._scalar_get(getattr(obj, self.target_collection))
else:

if self.scalar:
+proxied = getattr(obj, self.target_collection)
+if not proxied :
+return None
+return self._scalar_get(proxied)
  else:







-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] testing for an association proxy (possible bug and patch included)

2013-08-23 Thread Gombas, Gabor (IT)
On Fri, Aug 23, 2013 at 12:11:39PM -0700, Jonathan Vanasco wrote:

 i think a simple fix could be something like this ( line 240, 
 sqlalchemy/ext/associationproxy.py 
 )
 
 if self.scalar:
 -if not getattr(obj, self.target_collection)
 -return self._scalar_get(getattr(obj, self.target_collection))
 else:
 
 if self.scalar:
 +proxied = getattr(obj, self.target_collection)
 +if not proxied :
 +return None
 +return self._scalar_get(proxied)
   else:

We're monkey-patching AssociationProxy.__get__ with the same change
since SQLA 0.5.x, so it would be nice to get it applied upstream...
Maybe in 0.9?

Gabor

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] testing for an association proxy (possible bug and patch included)

2013-08-23 Thread jason kirtland
On Fri, Aug 23, 2013 at 2:31 PM, Gombas, Gabor (IT) 
gabor.gom...@morganstanley.com wrote:

 On Fri, Aug 23, 2013 at 12:11:39PM -0700, Jonathan Vanasco wrote:

  i think a simple fix could be something like this ( line 240,
 sqlalchemy/ext/associationproxy.py
  )
 
  if self.scalar:
  -if not getattr(obj, self.target_collection)
  -return self._scalar_get(getattr(obj,
 self.target_collection))
  else:
 
  if self.scalar:
  +proxied = getattr(obj, self.target_collection)
  +if not proxied :
  +return None
  +return self._scalar_get(proxied)
else:

 We're monkey-patching AssociationProxy.__get__ with the same change
 since SQLA 0.5.x, so it would be nice to get it applied upstream...
 Maybe in 0.9?


The patch seems like surprising Python behavior to me. Traversing across a
None is almost certainly a bug in regular code, and quashing that error by
default feels dangerous. I would want this to raise by default (and I have
found bugs because it did.)  I think you could opt into this behavior by
supplying an alternate, custom getter function that quashed None when
creating the proxy.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.