]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Mention Properties keys in __dir__
authorKorn, Uwe <Uwe.Korn@blue-yonder.com>
Mon, 9 Jul 2018 15:54:38 +0000 (11:54 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 1 Aug 2018 16:30:57 +0000 (12:30 -0400)
The Python builtin ``dir()`` is now supported for a SQLAlchemy "properties"
object, such as that of a Core columns collection (e.g. ``.c``),
``mapper.attrs``, etc.  Allows iPython autocompletion to work as well.
Pull request courtesy Uwe Korn.

Change-Id: I8696729542d1b74a566642a3a63fd500f64588cd
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/458

Mention Properties keys in __dir__

Change-Id: I88939955857c8df5eed0b87bc27c45357780b17d

doc/build/changelog/unreleased_13/dir_keys.rst [new file with mode: 0644]
lib/sqlalchemy/util/_collections.py
test/base/test_utils.py

diff --git a/doc/build/changelog/unreleased_13/dir_keys.rst b/doc/build/changelog/unreleased_13/dir_keys.rst
new file mode 100644 (file)
index 0000000..cb9a13b
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: feature, sql
+
+    The Python builtin ``dir()`` is now supported for a SQLAlchemy "properties"
+    object, such as that of a Core columns collection (e.g. ``.c``),
+    ``mapper.attrs``, etc.  Allows iPython autocompletion to work as well.
+    Pull request courtesy Uwe Korn.
index cc084a7c7b4baa48012cf897158ca200bf782106..1d9383bc6f9d0259675351372a13b890e0fc3b60 100644 (file)
@@ -184,6 +184,9 @@ class Properties(object):
     def __iter__(self):
         return iter(list(self._data.values()))
 
+    def __dir__(self):
+        return dir(super(Properties, self)) + [str(k) for k in self._data.keys()]
+
     def __add__(self, other):
         return list(self) + list(other)
 
index 9c36aeb9fc360481481a0837a8d9da87a93a8e88..4f462c052431b12431660dcab76be5da07265e9d 100644 (file)
@@ -3,7 +3,8 @@ import sys
 
 from sqlalchemy import util, sql, exc, testing
 from sqlalchemy.testing import assert_raises, assert_raises_message, fixtures
-from sqlalchemy.testing import eq_, is_, ne_, fails_if, mock, expect_warnings
+from sqlalchemy.testing import eq_, in_, is_, ne_, fails_if, mock
+from sqlalchemy.testing import expect_warnings
 from sqlalchemy.testing.util import picklers, gc_collect
 from sqlalchemy.util import classproperty, WeakSequence, get_callable_argspec
 from sqlalchemy.sql import column
@@ -2345,6 +2346,11 @@ class TestProperties(fixtures.TestBase):
             eq_(props._data, p._data)
             eq_(props.keys(), p.keys())
 
+    def test_keys_in_dir(self):
+        data = {'hello': 'bla'}
+        props = util.Properties(data)
+        in_('hello', dir(props))
+
     def test_pickle_immuatbleprops(self):
         data = {'hello': 'bla'}
         props = util.Properties(data).as_immutable()