From: Катаев Денис Date: Fri, 17 Mar 2017 18:19:21 +0000 (-0400) Subject: New features from python 2.7 X-Git-Tag: rel_1_2_0b1~145 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8f7cf2990f9010ea4924f2525318dff0ba1028d7;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git New features from python 2.7 After bump minimum supported version to 2.7 (1da9d3752160430c91534a8868ceb8c5ad1451d4), we can use new syntax. Change-Id: Ib064c75a00562e641d132f9c57e5e69744200e05 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/347 --- diff --git a/lib/sqlalchemy/dialects/mysql/pyodbc.py b/lib/sqlalchemy/dialects/mysql/pyodbc.py index 2ec6edf5c4..96a25d639d 100644 --- a/lib/sqlalchemy/dialects/mysql/pyodbc.py +++ b/lib/sqlalchemy/dialects/mysql/pyodbc.py @@ -59,7 +59,7 @@ class MySQLDialect_pyodbc(PyODBCConnector, MySQLDialect): # If it's decided that issuing that sort of SQL leaves you SOL, then # this can prefer the driver value. rs = connection.execute("SHOW VARIABLES LIKE 'character_set%%'") - opts = dict([(row[0], row[1]) for row in self._compat_fetchall(rs)]) + opts = {row[0]: row[1] for row in self._compat_fetchall(rs)} for key in ('character_set_connection', 'character_set'): if opts.get(key, None): return opts[key] diff --git a/lib/sqlalchemy/dialects/mysql/zxjdbc.py b/lib/sqlalchemy/dialects/mysql/zxjdbc.py index 9c92be4e6b..dfbb5df431 100644 --- a/lib/sqlalchemy/dialects/mysql/zxjdbc.py +++ b/lib/sqlalchemy/dialects/mysql/zxjdbc.py @@ -82,7 +82,7 @@ class MySQLDialect_zxjdbc(ZxJDBCConnector, MySQLDialect): # If it's decided that issuing that sort of SQL leaves you SOL, then # this can prefer the driver value. rs = connection.execute("SHOW VARIABLES LIKE 'character_set%%'") - opts = dict((row[0], row[1]) for row in self._compat_fetchall(rs)) + opts = {row[0]: row[1] for row in self._compat_fetchall(rs)} for key in ('character_set_connection', 'character_set'): if opts.get(key, None): return opts[key] diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py index b15affaf44..06565a1c05 100644 --- a/lib/sqlalchemy/dialects/oracle/base.py +++ b/lib/sqlalchemy/dialects/oracle/base.py @@ -925,9 +925,9 @@ class OracleDDLCompiler(compiler.DDLCompiler): class OracleIdentifierPreparer(compiler.IdentifierPreparer): - reserved_words = set([x.lower() for x in RESERVED_WORDS]) - illegal_initial_characters = set( - (str(dig) for dig in range(0, 10))).union(["_", "$"]) + reserved_words = {x.lower() for x in RESERVED_WORDS} + illegal_initial_characters = {str(dig) for dig in range(0, 10)} \ + .union(["_", "$"]) def _bindparam_requires_quotes(self, value): """Return True if the given identifier requires quoting.""" @@ -1424,7 +1424,7 @@ class OracleDialect(default.DefaultDialect): oracle_sys_col = re.compile(r'SYS_NC\d+\$', re.IGNORECASE) def upper_name_set(names): - return set([i.upper() for i in names]) + return {i.upper() for i in names} pk_names = upper_name_set(pkeys) diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index 73cb7eeeca..dc2bd5f976 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -338,12 +338,12 @@ class DefaultDialect(interfaces.Dialect): if additional_tests: tests += additional_tests - results = set([check_unicode(test) for test in tests]) + results = {check_unicode(test) for test in tests} if results.issuperset([True, False]): return "conditional" else: - return results == set([True]) + return results == {True} def _check_unicode_description(self, connection): # all DBAPIs on Py2K return cursor.description as encoded, @@ -694,7 +694,7 @@ class DefaultExecutionContext(interfaces.ExecutionContext): self.parameters = parameters else: self.parameters = [ - dict((dialect._encoder(k)[0], d[k]) for k in d) + {dialect._encoder(k)[0]: d[k] for k in d} for d in parameters ] or [{}] else: diff --git a/lib/sqlalchemy/engine/url.py b/lib/sqlalchemy/engine/url.py index 1c16584f12..1ca5983fd5 100644 --- a/lib/sqlalchemy/engine/url.py +++ b/lib/sqlalchemy/engine/url.py @@ -222,7 +222,7 @@ def _parse_rfc1738_args(name): query = ( len(tokens) > 1 and dict(util.parse_qsl(tokens[1]))) or None if util.py2k and query is not None: - query = dict((k.encode('ascii'), query[k]) for k in query) + query = {k.encode('ascii'): query[k] for k in query} else: query = None components['query'] = query diff --git a/lib/sqlalchemy/ext/automap.py b/lib/sqlalchemy/ext/automap.py index 219bfe124d..14599fe5e5 100644 --- a/lib/sqlalchemy/ext/automap.py +++ b/lib/sqlalchemy/ext/automap.py @@ -906,7 +906,7 @@ def _relationships_for_fks(automap_base, map_config, table_to_map_config, ) o2m_kws = {} - nullable = False not in set([fk.parent.nullable for fk in fks]) + nullable = False not in {fk.parent.nullable for fk in fks} if not nullable: o2m_kws['cascade'] = "all, delete-orphan" diff --git a/lib/sqlalchemy/ext/mutable.py b/lib/sqlalchemy/ext/mutable.py index 3361c44750..79459b39cb 100644 --- a/lib/sqlalchemy/ext/mutable.py +++ b/lib/sqlalchemy/ext/mutable.py @@ -421,7 +421,7 @@ class MutableBase(object): .. versionadded:: 1.0.5 """ - return set([attribute.key]) + return {attribute.key} @classmethod def _listen_on_attribute(cls, attribute, coerce, parent_cls): @@ -605,7 +605,7 @@ class MutableComposite(MutableBase): @classmethod def _get_listen_keys(cls, attribute): - return set([attribute.key]).union(attribute.property._attribute_keys) + return {attribute.key}.union(attribute.property._attribute_keys) def changed(self): """Subclasses should call this method whenever change events occur.""" diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 962486d581..27456c35b8 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -2312,9 +2312,9 @@ class Mapper(InspectionAttr): { tablea.col1: - set([tableb.col1, tablec.col1]), + {tableb.col1, tablec.col1}, tablea.col2: - set([tabled.col2]) + {tabled.col2} } """ @@ -2555,7 +2555,7 @@ class Mapper(InspectionAttr): @_memoized_configured_property def _primary_key_propkeys(self): - return set([prop.key for prop in self._all_pk_props]) + return {prop.key for prop in self._all_pk_props} def _get_state_attr_by_column( self, state, dict_, column, diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py index ad268c1273..8e91dd6c7c 100644 --- a/lib/sqlalchemy/orm/persistence.py +++ b/lib/sqlalchemy/orm/persistence.py @@ -86,7 +86,7 @@ def _bulk_update(mapper, mappings, session_transaction, search_keys = mapper._primary_key_propkeys if mapper._version_id_prop: - search_keys = set([mapper._version_id_prop.key]).union(search_keys) + search_keys = {mapper._version_id_prop.key}.union(search_keys) def _changed_dict(mapper, state): return dict( diff --git a/lib/sqlalchemy/orm/relationships.py b/lib/sqlalchemy/orm/relationships.py index dacb68b453..3b83f10cdc 100644 --- a/lib/sqlalchemy/orm/relationships.py +++ b/lib/sqlalchemy/orm/relationships.py @@ -2731,7 +2731,7 @@ class JoinCondition(object): self._gather_columns_with_annotation( self.secondaryjoin, annotation) ) - return set([x._deannotate() for x in s]) + return {x._deannotate() for x in s} def _gather_columns_with_annotation(self, clause, *annotation): annotation = set(annotation) @@ -2857,7 +2857,7 @@ class JoinCondition(object): secondaryjoin, {}, col_to_bind) lazywhere = sql.and_(lazywhere, secondaryjoin) - bind_to_col = dict((binds[col].key, col) for col in binds) + bind_to_col = {binds[col].key: col for col in binds} return lazywhere, bind_to_col, equated_columns diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index e3bef8f820..4415028988 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -50,7 +50,7 @@ RESERVED_WORDS = set([ 'using', 'verbose', 'when', 'where']) LEGAL_CHARACTERS = re.compile(r'^[A-Z0-9_$]+$', re.I) -ILLEGAL_INITIAL_CHARACTERS = set([str(x) for x in range(0, 10)]).union(['$']) +ILLEGAL_INITIAL_CHARACTERS = {str(x) for x in range(0, 10)}.union(['$']) BIND_PARAMS = re.compile(r'(? 1 and not self._allow_multiple_tables: diff --git a/lib/sqlalchemy/sql/visitors.py b/lib/sqlalchemy/sql/visitors.py index 7f09518246..99ceceed12 100644 --- a/lib/sqlalchemy/sql/visitors.py +++ b/lib/sqlalchemy/sql/visitors.py @@ -306,7 +306,7 @@ def replacement_traverse(obj, opts, replace): replacement by a given replacement function.""" cloned = {} - stop_on = set([id(x) for x in opts.get('stop_on', [])]) + stop_on = {id(x) for x in opts.get('stop_on', [])} def clone(elem, **kw): if id(elem) in stop_on or \ diff --git a/lib/sqlalchemy/testing/assertions.py b/lib/sqlalchemy/testing/assertions.py index 44551bd863..c8525f2f6c 100644 --- a/lib/sqlalchemy/testing/assertions.py +++ b/lib/sqlalchemy/testing/assertions.py @@ -384,8 +384,8 @@ class ComparesTables(object): eq_(c.type.length, reflected_c.type.length) eq_( - set([f.column.name for f in c.foreign_keys]), - set([f.column.name for f in reflected_c.foreign_keys]) + {f.column.name for f in c.foreign_keys}, + {f.column.name for f in reflected_c.foreign_keys} ) if c.server_default: assert isinstance(reflected_c.server_default, @@ -440,7 +440,7 @@ class AssertsExecutionResults(object): return id(self) found = util.IdentitySet(result) - expected = set([immutabledict(e) for e in expected]) + expected = {immutabledict(e) for e in expected} for wrong in util.itertools_filterfalse(lambda o: isinstance(o, cls), found): diff --git a/lib/sqlalchemy/testing/provision.py b/lib/sqlalchemy/testing/provision.py index 7e44544656..83b6115fea 100644 --- a/lib/sqlalchemy/testing/provision.py +++ b/lib/sqlalchemy/testing/provision.py @@ -288,7 +288,7 @@ def reap_oracle_dbs(eng, idents_file): "select u.username from all_users u where username " "like 'TEST_%' and not exists (select username " "from v$session where username=u.username)") - all_names = set([username.lower() for (username, ) in to_reap]) + all_names = {username.lower() for (username, ) in to_reap} to_drop = set() for name in all_names: if name.endswith("_ts1") or name.endswith("_ts2"): diff --git a/lib/sqlalchemy/testing/schema.py b/lib/sqlalchemy/testing/schema.py index 018a291d9f..f40654a2a3 100644 --- a/lib/sqlalchemy/testing/schema.py +++ b/lib/sqlalchemy/testing/schema.py @@ -17,8 +17,7 @@ table_options = {} def Table(*args, **kw): """A schema.Table wrapper/hook for dialect-specific tweaks.""" - test_opts = dict([(k, kw.pop(k)) for k in list(kw) - if k.startswith('test_')]) + test_opts = {k: kw.pop(k) for k in list(kw) if k.startswith('test_')} kw.update(table_options) @@ -64,8 +63,7 @@ def Table(*args, **kw): def Column(*args, **kw): """A schema.Column wrapper/hook for dialect-specific tweaks.""" - test_opts = dict([(k, kw.pop(k)) for k in list(kw) - if k.startswith('test_')]) + test_opts = {k: kw.pop(k) for k in list(kw) if k.startswith('test_')} if not config.requirements.foreign_key_ddl.enabled_for_config(config): args = [arg for arg in args if not isinstance(arg, schema.ForeignKey)] diff --git a/lib/sqlalchemy/testing/suite/test_reflection.py b/lib/sqlalchemy/testing/suite/test_reflection.py index a761c0882a..f47b34bf45 100644 --- a/lib/sqlalchemy/testing/suite/test_reflection.py +++ b/lib/sqlalchemy/testing/suite/test_reflection.py @@ -746,7 +746,7 @@ class ComponentReflectionTest(fixtures.TablesTest): ('dingalings', 'dingaling_id'), ]: cols = insp.get_columns(tname) - id_ = dict((c['name'], c) for c in cols)[cname] + id_ = {c['name']: c for c in cols}[cname] assert id_.get('autoincrement', True) diff --git a/lib/sqlalchemy/testing/suite/test_types.py b/lib/sqlalchemy/testing/suite/test_types.py index dbbe031118..ee757e1caf 100644 --- a/lib/sqlalchemy/testing/suite/test_types.py +++ b/lib/sqlalchemy/testing/suite/test_types.py @@ -340,7 +340,7 @@ class NumericTest(_LiteralRoundTripFixture, fixtures.TestBase): t.create() t.insert().execute([{'x': x} for x in input_]) - result = set([row[0] for row in t.select().execute()]) + result = {row[0] for row in t.select().execute()} output = set(output) if filter_: result = set(filter_(x) for x in result) diff --git a/lib/sqlalchemy/testing/util.py b/lib/sqlalchemy/testing/util.py index 8f91f31ed3..a37637ac02 100644 --- a/lib/sqlalchemy/testing/util.py +++ b/lib/sqlalchemy/testing/util.py @@ -173,7 +173,7 @@ def rowset(results): Useful for asserting the results of an unordered query. """ - return set([tuple(row) for row in results]) + return {tuple(row) for row in results} def fail(msg): diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py index e3e1d71dcf..bdd40fc794 100644 --- a/lib/sqlalchemy/util/_collections.py +++ b/lib/sqlalchemy/util/_collections.py @@ -107,7 +107,7 @@ class KeyedTuple(AbstractKeyedTuple): .. versionadded:: 0.8 """ - return dict((key, self.__dict__[key]) for key in self.keys()) + return {key: self.__dict__[key] for key in self.keys()} class _LW(AbstractKeyedTuple): diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 68c0f885b5..41fed882dc 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -567,7 +567,7 @@ def class_hierarchy(cls): if isinstance(cls, types.ClassType): return list() - hier = set([cls]) + hier = {cls} process = list(cls.__mro__) while process: c = process.pop()