From: Korn, Uwe Date: Mon, 9 Jul 2018 15:54:38 +0000 (-0400) Subject: Mention Properties keys in __dir__ X-Git-Tag: rel_1_3_0b1~116 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85dd7668530612e2cf7de7f575d68fc7c683a988;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Mention Properties keys in __dir__ 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 --- diff --git a/doc/build/changelog/unreleased_13/dir_keys.rst b/doc/build/changelog/unreleased_13/dir_keys.rst new file mode 100644 index 0000000000..cb9a13b423 --- /dev/null +++ b/doc/build/changelog/unreleased_13/dir_keys.rst @@ -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. diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py index cc084a7c7b..1d9383bc6f 100644 --- a/lib/sqlalchemy/util/_collections.py +++ b/lib/sqlalchemy/util/_collections.py @@ -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) diff --git a/test/base/test_utils.py b/test/base/test_utils.py index 9c36aeb9fc..4f462c0524 100644 --- a/test/base/test_utils.py +++ b/test/base/test_utils.py @@ -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()