object_session() raises instead of returning None for objects not in a session

Issue #1881 resolved
Former user created an issue

from sqlalchemy.orm import object_session object_session(object()) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "sqlalchemy/orm/session.py", line 1614, in object_session return _state_session(attributes.instance_state(instance)) AttributeError: 'object' object has no attribute '_sa_instance_state'

Comments (5)

  1. Former user Account Deleted

    Sorry, that should be:

    >>> from sqlalchemy.orm import object_session
    >>> object_session(object())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "sqlalchemy/orm/session.py", line 1614, in object_session
        return _state_session(attributes.instance_state(instance))
    AttributeError: 'object' object has no attribute '_sa_instance_state'
    
  2. Former user Account Deleted

    I'm another sqla user, but just my 2 cents is that this is behaving how I would expect it to. If you pass an object that isn't even a sqla object, I would hope for an exception.

    If you want to catch that exception and pass on it, then you have the flexibility.

  3. Former user Account Deleted

    The docstring promises that it returns None if the object is not associated, though.

  4. Log in to comment