From: Mike Bayer Date: Wed, 11 Oct 2017 21:17:19 +0000 (-0400) Subject: Clean up all Python 3.6 warnings X-Git-Tag: rel_0_9_6~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6c2934661f38833ec2a325bb43f1dd5cdfec9ca1;p=thirdparty%2Fsqlalchemy%2Falembic.git Clean up all Python 3.6 warnings Fixed a few Python3.6 deprecation warnings by replacing ``StopIteration`` with ``return``, as well as using ``getfullargspec()`` instead of ``getargspec()`` under Python 3. Additionally fixed docstrings with backslashes needing r'', filehandles not explicitly closed, accommodate for .pyc files not necessarily present. Change-Id: Id9791c5fa8b4b1f3e4e36f237a8a8ebcef4aaaba Fixes: #458 --- diff --git a/alembic/config.py b/alembic/config.py index 3774cd32..46ccb91a 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -432,7 +432,7 @@ class CommandLine(object): fn.__name__[0] != '_' and \ fn.__module__ == 'alembic.command': - spec = inspect.getargspec(fn) + spec = compat.inspect_getargspec(fn) if spec[3]: positional = spec[0][1:-len(spec[3])] kwarg = spec[0][-len(spec[3]):] diff --git a/alembic/operations/base.py b/alembic/operations/base.py index 22d601de..ef6ecf8f 100644 --- a/alembic/operations/base.py +++ b/alembic/operations/base.py @@ -5,6 +5,7 @@ from ..util import sqla_compat from . import batch from . import schemaobj from ..util.compat import exec_ +from ..util.compat import inspect_getargspec import textwrap import inspect @@ -91,7 +92,7 @@ class Operations(util.ModuleClsProxy): fn = getattr(op_cls, sourcename) source_name = fn.__name__ - spec = inspect.getargspec(fn) + spec = inspect_getargspec(fn) name_args = spec[0] assert name_args[0:2] == ['cls', 'operations'] diff --git a/alembic/operations/ops.py b/alembic/operations/ops.py index 64a508e3..43001fed 100644 --- a/alembic/operations/ops.py +++ b/alembic/operations/ops.py @@ -794,7 +794,7 @@ class CreateIndexOp(MigrateOperation): cls, operations, index_name, table_name, columns, schema=None, unique=False, **kw): - """Issue a "create index" instruction using the current + r"""Issue a "create index" instruction using the current migration context. e.g.:: @@ -927,7 +927,7 @@ class DropIndexOp(MigrateOperation): ]) def drop_index(cls, operations, index_name, table_name=None, schema=None, **kw): - """Issue a "drop index" instruction using the current + r"""Issue a "drop index" instruction using the current migration context. e.g.:: @@ -1027,7 +1027,7 @@ class CreateTableOp(MigrateOperation): @classmethod @util._with_legacy_names([('name', 'table_name')]) def create_table(cls, operations, table_name, *columns, **kw): - """Issue a "create table" instruction using the current migration + r"""Issue a "create table" instruction using the current migration context. This directive receives an argument list similar to that of the @@ -1157,7 +1157,7 @@ class DropTableOp(MigrateOperation): @classmethod @util._with_legacy_names([('name', 'table_name')]) def drop_table(cls, operations, table_name, schema=None, **kw): - """Issue a "drop table" instruction using the current + r"""Issue a "drop table" instruction using the current migration context. diff --git a/alembic/runtime/environment.py b/alembic/runtime/environment.py index 613b7459..ce9be637 100644 --- a/alembic/runtime/environment.py +++ b/alembic/runtime/environment.py @@ -81,7 +81,7 @@ class EnvironmentContext(util.ModuleClsProxy): """ def __init__(self, config, script, **kw): - """Construct a new :class:`.EnvironmentContext`. + r"""Construct a new :class:`.EnvironmentContext`. :param config: a :class:`.Config` instance. :param script: a :class:`.ScriptDirectory` instance. diff --git a/alembic/runtime/migration.py b/alembic/runtime/migration.py index 65b4855d..17cc2265 100644 --- a/alembic/runtime/migration.py +++ b/alembic/runtime/migration.py @@ -287,7 +287,7 @@ class MigrationContext(object): head_maintainer.update_to_step(step) def run_migrations(self, **kw): - """Run the migration scripts established for this + r"""Run the migration scripts established for this :class:`.MigrationContext`, if any. The commands in :mod:`alembic.command` will set up a function diff --git a/alembic/script/revision.py b/alembic/script/revision.py index 1e68a7c3..3d9a332d 100644 --- a/alembic/script/revision.py +++ b/alembic/script/revision.py @@ -656,7 +656,7 @@ class RevisionMap(object): uppers = util.dedupe_tuple(self.get_revisions(upper)) if not uppers and not requested_lowers: - raise StopIteration() + return upper_ancestors = set(self._get_ancestor_nodes(uppers, check=True)) @@ -716,7 +716,7 @@ class RevisionMap(object): # if the requested start is one of those branch points, # then just return empty set if start_from.intersection(upper_ancestors): - raise StopIteration() + return else: # otherwise, they requested nodes out of # order diff --git a/alembic/testing/exclusions.py b/alembic/testing/exclusions.py index 75348d4e..7d33a5b3 100644 --- a/alembic/testing/exclusions.py +++ b/alembic/testing/exclusions.py @@ -302,7 +302,7 @@ class SpecPredicate(Predicate): class LambdaPredicate(Predicate): def __init__(self, lambda_, description=None, args=None, kw=None): - spec = inspect.getargspec(lambda_) + spec = compat.inspect_getargspec(lambda_) if not spec[0]: self.lambda_ = lambda db: lambda_() else: diff --git a/alembic/testing/fixtures.py b/alembic/testing/fixtures.py index b13edaeb..e6c16dd5 100644 --- a/alembic/testing/fixtures.py +++ b/alembic/testing/fixtures.py @@ -111,7 +111,7 @@ def op_fixture( # the impl produces soft tabs, # so search for blocks of 4 spaces msg = re.sub(r' ', '', msg) - msg = re.sub('\;\n*$', '', msg) + msg = re.sub(r'\;\n*$', '', msg) self.lines.append(msg) diff --git a/alembic/testing/plugin/plugin_base.py b/alembic/testing/plugin/plugin_base.py index 0ed8f847..83b30e38 100644 --- a/alembic/testing/plugin/plugin_base.py +++ b/alembic/testing/plugin/plugin_base.py @@ -420,11 +420,10 @@ def generate_sub_tests(cls, module): # pytest junit plugin, which is tripped up by the brackets # and periods, so sanitize - alpha_name = re.sub('[_\[\]\.]+', '_', cfg.name) + alpha_name = re.sub(r'[_\[\]\.]+', '_', cfg.name) alpha_name = re.sub('_+$', '', alpha_name) name = "%s_%s" % (cls.__name__, alpha_name) - subcls = type( name, (cls, ), diff --git a/alembic/util/compat.py b/alembic/util/compat.py index 4d7e6fad..a754f2a8 100644 --- a/alembic/util/compat.py +++ b/alembic/util/compat.py @@ -48,6 +48,21 @@ else: range = xrange +if py3k: + import collections + ArgSpec = collections.namedtuple( + "ArgSpec", + ["args", "varargs", "keywords", "defaults"]) + + from inspect import getfullargspec as inspect_getfullargspec + + def inspect_getargspec(func): + return ArgSpec( + *inspect_getfullargspec(func)[0:4] + ) +else: + from inspect import getargspec as inspect_getargspec # noqa + if py3k: from configparser import ConfigParser as SafeConfigParser import configparser diff --git a/alembic/util/langhelpers.py b/alembic/util/langhelpers.py index a556e943..aa016f04 100644 --- a/alembic/util/langhelpers.py +++ b/alembic/util/langhelpers.py @@ -6,8 +6,7 @@ import collections from .compat import callable, exec_, string_types, with_metaclass -from sqlalchemy.util import format_argspec_plus, update_wrapper -from sqlalchemy.util.compat import inspect_getfullargspec +from .compat import inspect_getargspec class _ModuleClsMeta(type): @@ -73,7 +72,7 @@ class ModuleClsProxy(with_metaclass(_ModuleClsMeta)): @classmethod def _create_method_proxy(cls, name, globals_, locals_): fn = getattr(cls, name) - spec = inspect.getargspec(fn) + spec = inspect_getargspec(fn) if spec[0] and spec[0][0] == 'self': spec[0].pop(0) args = inspect.formatargspec(*spec) diff --git a/docs/build/unreleased/458.rst b/docs/build/unreleased/458.rst new file mode 100644 index 00000000..bbf2da46 --- /dev/null +++ b/docs/build/unreleased/458.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, commands + :tickets: 458 + + Fixed a few Python3.6 deprecation warnings by replacing ``StopIteration`` + with ``return``, as well as using ``getfullargspec()`` instead of + ``getargspec()`` under Python 3. \ No newline at end of file diff --git a/tests/test_script_consumption.py b/tests/test_script_consumption.py index 7a8f0dc3..b394784b 100644 --- a/tests/test_script_consumption.py +++ b/tests/test_script_consumption.py @@ -536,7 +536,7 @@ def downgrade(): """) pyc_path = util.pyc_file_from_path(path) - if os.access(pyc_path, os.F_OK): + if pyc_path is not None and os.access(pyc_path, os.F_OK): os.unlink(pyc_path) assert_raises_message( diff --git a/tests/test_script_production.py b/tests/test_script_production.py index 588b9d1f..7ab46ab6 100644 --- a/tests/test_script_production.py +++ b/tests/test_script_production.py @@ -549,7 +549,8 @@ def downgrade(): head="model1@head", process_revision_directives=process_revision_directives) - result = open(rev.path).read() + with open(rev.path) as handle: + result = handle.read() assert (""" def upgrade(): # ### commands auto generated by Alembic - please adjust! ### @@ -985,7 +986,8 @@ context.configure(dialect_name='sqlite', template_args={"somearg":"somevalue"}) str(ce) ) assert m, "Command error did not produce a file" - contents = open(m.group(1)).read() + with open(m.group(1)) as handle: + contents = handle.read() os.remove(m.group(1)) assert "<% z = x + y %>" in contents