From 40357495e41aa21e8e1f6310b6fb27f871e25e0b Mon Sep 17 00:00:00 2001 From: James Davies Date: Mon, 12 Aug 2019 18:58:50 -0400 Subject: [PATCH 1/9] Add F and W flags to automated flake8 checks in setup.cfg --- asdf/__init__.py | 19 ++-------- asdf/_internal_init.py | 4 --- asdf/asdf.py | 2 -- asdf/asdftypes.py | 3 ++ asdf/block.py | 1 - asdf/commands/__init__.py | 25 ++++++++----- asdf/commands/defragment.py | 3 -- asdf/commands/diff.py | 4 --- asdf/commands/extension.py | 1 + asdf/commands/extract.py | 3 -- asdf/commands/remove_hdu.py | 2 -- asdf/commands/tests/test_defragment.py | 8 ++--- asdf/commands/tests/test_diff.py | 9 ++--- asdf/conftest.py | 11 ++---- asdf/extension.py | 2 -- asdf/fits_embed.py | 1 - asdf/schema.py | 3 -- asdf/tags/__init__.py | 6 ---- asdf/tags/core/__init__.py | 18 ++++++---- asdf/tags/core/ndarray.py | 1 - asdf/tags/core/tests/test_complex.py | 4 +-- asdf/tags/core/tests/test_history.py | 14 ++++---- asdf/tags/core/tests/test_ndarray.py | 49 ++++++++++++-------------- asdf/tests/conftest.py | 1 - asdf/tests/test_array_blocks.py | 12 +++---- asdf/tests/test_asdftypes.py | 25 +++++-------- asdf/tests/test_file_format.py | 2 +- asdf/tests/test_fits_embed.py | 6 ++-- asdf/tests/test_generic_io.py | 13 ++++--- asdf/tests/test_reference_files.py | 4 +-- asdf/tests/test_schema.py | 14 +++----- asdf/tests/test_versioning.py | 2 +- asdf/tests/test_yaml.py | 4 +-- asdf/treeutil.py | 1 - asdf/types.py | 1 - asdf/util.py | 2 -- asdf/version.py | 2 ++ compatibility_tests/common.py | 2 +- docs/conf.py | 2 +- docs/sphinxext/__init__.py | 1 - docs/sphinxext/example.py | 4 +-- pytest_asdf/plugin.py | 1 - setup.cfg | 4 ++- 43 files changed, 115 insertions(+), 181 deletions(-) diff --git a/asdf/__init__.py b/asdf/__init__.py index 01d384894..b483e1b7a 100644 --- a/asdf/__init__.py +++ b/asdf/__init__.py @@ -10,28 +10,15 @@ # Affiliated packages may add whatever they like to this file, but # should keep this content at the top. # ---------------------------------------------------------------------------- -from ._internal_init import * +from ._internal_init import __version__, __githash__, test # ---------------------------------------------------------------------------- __all__ = [ 'AsdfFile', 'CustomType', 'AsdfExtension', 'Stream', 'open', 'test', - 'commands', 'IntegerType', 'ExternalArrayReference', 'info' + 'commands', 'IntegerType', 'ExternalArrayReference', 'info', '__version__', + '__githash__', 'ValidationError' ] -try: - import yaml as _ -except ImportError: - raise ImportError("asdf requires pyyaml") - -try: - import jsonschema as _ -except ImportError: - raise ImportError("asdf requires jsonschema") - -try: - import numpy as _ -except ImportError: - raise ImportError("asdf requires numpy") from .asdf import AsdfFile, open_asdf from .types import CustomType diff --git a/asdf/_internal_init.py b/asdf/_internal_init.py index 8877766fa..ce92d8512 100644 --- a/asdf/_internal_init.py +++ b/asdf/_internal_init.py @@ -97,10 +97,6 @@ def test(package=None, test_path=None, args=None, plugins=None, explicitly updating the package template. """ - try: - import astropy - except ImportError: - raise ImportError("Running the tests requires astropy") test_runner = _get_test_runner() return test_runner.run_tests( diff --git a/asdf/asdf.py b/asdf/asdf.py index 1319ed842..e1f8b8aff 100644 --- a/asdf/asdf.py +++ b/asdf/asdf.py @@ -5,11 +5,9 @@ import io import os import time -import re import copy import datetime import warnings -import importlib from pkg_resources import parse_version import numpy as np diff --git a/asdf/asdftypes.py b/asdf/asdftypes.py index 505abf498..62f9ee682 100644 --- a/asdf/asdftypes.py +++ b/asdf/asdftypes.py @@ -9,6 +9,9 @@ from .types import (AsdfType, CustomType, format_tag, ExtensionTypeMeta, _all_asdftypes) +__all__ = ["join_tag_version", "split_tag_version", "AsdfType", "CustomType", + "format_tag", "ExtensionTypeMeta", "_all_asdftypes"] + warnings.warn( "The module asdf.asdftypes has been deprecated and will be removed in 3.0. " "Use asdf.types instead.", AsdfDeprecationWarning) diff --git a/asdf/block.py b/asdf/block.py index d0cc37a5a..69e4af7d7 100644 --- a/asdf/block.py +++ b/asdf/block.py @@ -20,7 +20,6 @@ from .compat.numpycompat import NUMPY_LT_1_7 from . import constants from . import generic_io -from . import stream from . import treeutil from . import util from . import yamlutil diff --git a/asdf/commands/__init__.py b/asdf/commands/__init__.py index 24d70157d..9dac5af64 100644 --- a/asdf/commands/__init__.py +++ b/asdf/commands/__init__.py @@ -3,15 +3,22 @@ import importlib -from .exploded import * -from .to_yaml import * -from .defragment import * -from .diff import * -from .tags import * -from .extension import * -from .info import * +from .exploded import implode, explode +from .to_yaml import to_yaml +from .defragment import defragment +from .diff import diff +from .tags import list_tags +from .extension import find_extensions +from .info import info + + +__all__ = ['implode', 'explode', 'to_yaml', 'defragment', 'diff', 'list_tags', + 'find_extensions', 'info'] + # Extracting ASDF-in-FITS files requires Astropy if importlib.util.find_spec('astropy'): - from .extract import * - from .remove_hdu import * + from .extract import extract_file + from .remove_hdu import remove_hdu + + __all__ += ['extract_file', 'remove_hdu'] diff --git a/asdf/commands/defragment.py b/asdf/commands/defragment.py index 9ca2bb1d1..f841d2ca5 100644 --- a/asdf/commands/defragment.py +++ b/asdf/commands/defragment.py @@ -5,9 +5,6 @@ Defragment command. """ - -import os - import asdf from .main import Command from .. import AsdfFile diff --git a/asdf/commands/diff.py b/asdf/commands/diff.py index 4a94ad4c8..aaa468a80 100644 --- a/asdf/commands/diff.py +++ b/asdf/commands/diff.py @@ -5,8 +5,6 @@ Implementation of command for displaying differences between two ASDF files. """ - -import os import sys from numpy import array_equal try: @@ -30,8 +28,6 @@ import asdf from .main import Command -from .. import AsdfFile -from .. import treeutil from ..tagged import Tagged from ..util import human_list from ..tags.core.ndarray import NDArrayType diff --git a/asdf/commands/extension.py b/asdf/commands/extension.py index f3777e29d..cb5b38347 100644 --- a/asdf/commands/extension.py +++ b/asdf/commands/extension.py @@ -10,6 +10,7 @@ from .main import Command +__all__ = ['find_extensions'] class QueryExtension(Command): # pragma: no cover """This class is the plugin implementation for the asdftool runner.""" diff --git a/asdf/commands/extract.py b/asdf/commands/extract.py index 1adb0b43e..94bb7014f 100644 --- a/asdf/commands/extract.py +++ b/asdf/commands/extract.py @@ -5,10 +5,7 @@ Implementation of command for converting ASDF-in-FITS to standalone ASDF file. """ -import sys - import asdf -from asdf import AsdfFile from asdf.fits_embed import AsdfInFits from .main import Command diff --git a/asdf/commands/remove_hdu.py b/asdf/commands/remove_hdu.py index 13e1df19e..50ebc4c29 100644 --- a/asdf/commands/remove_hdu.py +++ b/asdf/commands/remove_hdu.py @@ -5,8 +5,6 @@ Implementation of command for removing ASDF HDU from ASDF-in-FITS file. """ -import sys - from astropy.io import fits from .main import Command diff --git a/asdf/commands/tests/test_defragment.py b/asdf/commands/tests/test_defragment.py index 77eee448f..cce841c51 100644 --- a/asdf/commands/tests/test_defragment.py +++ b/asdf/commands/tests/test_defragment.py @@ -1,17 +1,15 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst # -*- coding: utf-8 -*- - import os -import sys import numpy as np import pytest import asdf -from ... import AsdfFile -from .. import main -from ...tests.helpers import get_file_sizes, assert_tree_match +from asdf import AsdfFile +from asdf.commands import main +from asdf.tests.helpers import get_file_sizes, assert_tree_match def _test_defragment(tmpdir, codec): diff --git a/asdf/commands/tests/test_diff.py b/asdf/commands/tests/test_diff.py index cb6daad81..6f38c4879 100644 --- a/asdf/commands/tests/test_diff.py +++ b/asdf/commands/tests/test_diff.py @@ -2,18 +2,13 @@ # -*- coding: utf-8 -*- -import os import io from functools import partial -import numpy as np import pytest -from ... import AsdfFile -from ...tests import helpers - -from .. import main, diff - +from asdf.tests import helpers +from asdf.commands import main, diff from . import data as test_data get_test_data_path = partial(helpers.get_test_data_path, module=test_data) diff --git a/asdf/conftest.py b/asdf/conftest.py index 916b88a7c..742d5b727 100644 --- a/asdf/conftest.py +++ b/asdf/conftest.py @@ -1,18 +1,13 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst # -*- coding: utf-8 -*- -# this contains imports plugins that configure py.test for asdf tests. -# by importing them here in conftest.py they are discoverable by py.test + +# This contains pytest fixtures used in asdf tests. +# by importing them here in conftest.py they are discoverable by pytest # no matter how it is invoked within the source tree. -import os import pytest -from .extern.RangeHTTPServer import RangeHTTPRequestHandler - -# This is to figure out the affiliated package version, rather than -# using Astropy's -from . import version from .tests.httpserver import HTTPServer, RangeHTTPServer diff --git a/asdf/extension.py b/asdf/extension.py index d2ba98578..d9d859c0c 100644 --- a/asdf/extension.py +++ b/asdf/extension.py @@ -6,8 +6,6 @@ import warnings from pkg_resources import iter_entry_points -import importlib - from . import types from . import resolver from .util import get_class_name diff --git a/asdf/fits_embed.py b/asdf/fits_embed.py index 49ac7ea3d..8a7f44065 100644 --- a/asdf/fits_embed.py +++ b/asdf/fits_embed.py @@ -17,7 +17,6 @@ try: from astropy.io import fits - from astropy.io.fits import Column, BinTableHDU except ImportError: raise ImportError("AsdfInFits requires astropy") diff --git a/asdf/schema.py b/asdf/schema.py index 1df1658e1..13c3a8972 100644 --- a/asdf/schema.py +++ b/asdf/schema.py @@ -1,7 +1,6 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst # -*- coding: utf-8 -*- -import os import json import datetime import warnings @@ -9,7 +8,6 @@ from numbers import Integral from functools import lru_cache from collections import OrderedDict -from collections.abc import Mapping from urllib import parse as urlparse from jsonschema import validators as mvalidators @@ -23,7 +21,6 @@ from . import reference from . import treeutil from . import util -from .extension import default_extensions from .compat.jsonschemacompat import JSONSCHEMA_LT_3 from . import extension from .exceptions import AsdfDeprecationWarning diff --git a/asdf/tags/__init__.py b/asdf/tags/__init__.py index 055bc2cf8..e69de29bb 100644 --- a/asdf/tags/__init__.py +++ b/asdf/tags/__init__.py @@ -1,6 +0,0 @@ -# -*- coding: utf-8 -*- - - -# TODO: Import entire tree automatically and make these work like "plugins"? - -from . import core diff --git a/asdf/tags/core/__init__.py b/asdf/tags/core/__init__.py index 56e99f505..cc331a735 100644 --- a/asdf/tags/core/__init__.py +++ b/asdf/tags/core/__init__.py @@ -6,6 +6,17 @@ class AsdfObject(dict): pass +from .constant import ConstantType +from .ndarray import NDArrayType +from .complex import ComplexType +from .integer import IntegerType +from .external_reference import ExternalArrayReference + + +__all__ = ['AsdfObject', 'Software', 'HistoryEntry', 'ExtensionMetadata', + 'SubclassMetadata', 'ConstantType', 'NDArrayType', 'ComplexType', + 'IntegerType', 'ExternalArrayReference'] + class AsdfObjectType(AsdfType): name = 'core/asdf' @@ -56,10 +67,3 @@ def to_tree(cls, node, ctx): class SubclassMetadata(dict, AsdfType): name = 'core/subclass_metadata' version = '1.0.0' - - -from .constant import ConstantType -from .ndarray import NDArrayType -from .complex import ComplexType -from .integer import IntegerType -from .external_reference import ExternalArrayReference diff --git a/asdf/tags/core/ndarray.py b/asdf/tags/core/ndarray.py index 66eaa2369..b57b302c3 100644 --- a/asdf/tags/core/ndarray.py +++ b/asdf/tags/core/ndarray.py @@ -12,7 +12,6 @@ from ...types import AsdfType from ... import schema from ... import util -from ... import yamlutil _datatype_names = { diff --git a/asdf/tags/core/tests/test_complex.py b/asdf/tags/core/tests/test_complex.py index 04c1f31ef..6375f05ad 100644 --- a/asdf/tags/core/tests/test_complex.py +++ b/asdf/tags/core/tests/test_complex.py @@ -45,9 +45,7 @@ def test_valid_complex(valid): 'nan+4j' ]) def test_valid_nan_complex(valid): - - with asdf.open(make_complex_asdf(valid)) as af: - # Don't compare values since NANs are never equal + with asdf.open(make_complex_asdf(valid)): pass diff --git a/asdf/tags/core/tests/test_history.py b/asdf/tags/core/tests/test_history.py index 796286ca8..a153176f8 100644 --- a/asdf/tags/core/tests/test_history.py +++ b/asdf/tags/core/tests/test_history.py @@ -165,7 +165,7 @@ def test_missing_extension_warning(): buff = yaml_to_asdf(yaml) with pytest.warns(None) as warnings: - with asdf.open(buff) as af: + with asdf.open(buff): pass assert len(warnings) == 1, display_warnings(warnings) @@ -187,7 +187,7 @@ def test_extension_version_warning(): buff = yaml_to_asdf(yaml) with pytest.warns(None) as warnings: - with asdf.open(buff) as af: + with asdf.open(buff): pass assert len(warnings) == 1, display_warnings(warnings) @@ -199,7 +199,7 @@ def test_extension_version_warning(): # Make sure suppressing the warning works too with pytest.warns(None) as warnings: - with asdf.open(buff, ignore_missing_extensions=True) as af: + with asdf.open(buff, ignore_missing_extensions=True): pass assert len(warnings) == 0, display_warnings(warnings) @@ -219,12 +219,12 @@ def test_strict_extension_check(): buff = yaml_to_asdf(yaml) with pytest.raises(RuntimeError): - with asdf.open(buff, strict_extension_check=True) as af: + with asdf.open(buff, strict_extension_check=True): pass # Make sure to test for incompatibility with ignore_missing_extensions with pytest.raises(ValueError): - with asdf.open(buff, strict_extension_check=True, ignore_missing_extensions=True) as af: + with asdf.open(buff, strict_extension_check=True, ignore_missing_extensions=True): pass @@ -263,7 +263,7 @@ def types(self): assert len(af['history']['extensions']) == 2 with pytest.warns(None) as warnings: - with asdf.open(tmpfile, ignore_unrecognized_tag=True) as af: + with asdf.open(tmpfile, ignore_unrecognized_tag=True): pass # Since we're ignoring the unrecognized tag warning, we should only get @@ -282,7 +282,7 @@ def types(self): assert len(af['history']['extensions']) == 1 with pytest.warns(None) as warnings: - with asdf.open(tmpfile2) as af: + with asdf.open(tmpfile2): pass assert len(warnings) == 0 diff --git a/asdf/tags/core/tests/test_ndarray.py b/asdf/tags/core/tests/test_ndarray.py index e1a2b3a1b..746e3e314 100644 --- a/asdf/tags/core/tests/test_ndarray.py +++ b/asdf/tags/core/tests/test_ndarray.py @@ -187,8 +187,7 @@ def check_raw_yaml(content): def test_auto_inline_recursive(tmpdir): - astropy = pytest.importorskip('astropy') - from astropy.modeling import models + models = pytest.importorskip('astropy.modeling.models') aff = models.AffineTransformation2D(matrix=[[1, 2], [3, 4]]) tree = {'test': aff} @@ -313,7 +312,6 @@ def check_asdf(asdf): tree = asdf.tree m = tree['masked_array'] - x = tree['unmasked_array'] print(m) print(m.mask) @@ -529,7 +527,7 @@ def test_mask_datatype(tmpdir): """ buff = helpers.yaml_to_asdf(content) - with asdf.open(buff) as ff: + with asdf.open(buff): pass @@ -544,7 +542,7 @@ def test_invalid_mask_datatype(tmpdir): buff = helpers.yaml_to_asdf(content) with pytest.raises(jsonschema.ValidationError): - with asdf.open(buff) as ff: + with asdf.open(buff): pass @@ -557,7 +555,7 @@ def test_ndim_validation(tmpdir): buff = helpers.yaml_to_asdf(content) with pytest.raises(jsonschema.ValidationError): - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -567,7 +565,7 @@ def test_ndim_validation(tmpdir): """ buff = helpers.yaml_to_asdf(content) - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -578,7 +576,7 @@ def test_ndim_validation(tmpdir): """ buff = helpers.yaml_to_asdf(content) - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -588,7 +586,7 @@ def test_ndim_validation(tmpdir): """ buff = helpers.yaml_to_asdf(content) - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -598,7 +596,7 @@ def test_ndim_validation(tmpdir): """ buff = helpers.yaml_to_asdf(content) - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -609,7 +607,7 @@ def test_ndim_validation(tmpdir): buff = helpers.yaml_to_asdf(content) with pytest.raises(jsonschema.ValidationError): - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass @@ -622,7 +620,7 @@ def test_datatype_validation(tmpdir): """ buff = helpers.yaml_to_asdf(content) - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -634,7 +632,7 @@ def test_datatype_validation(tmpdir): buff = helpers.yaml_to_asdf(content) with pytest.raises(jsonschema.ValidationError): - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -645,7 +643,7 @@ def test_datatype_validation(tmpdir): """ buff = helpers.yaml_to_asdf(content) - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -657,7 +655,7 @@ def test_datatype_validation(tmpdir): buff = helpers.yaml_to_asdf(content) with pytest.raises(jsonschema.ValidationError): - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -673,7 +671,7 @@ def test_datatype_validation(tmpdir): buff = helpers.yaml_to_asdf(content) with pytest.raises(jsonschema.ValidationError): - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass @@ -690,7 +688,7 @@ def test_structured_datatype_validation(tmpdir): """ buff = helpers.yaml_to_asdf(content) - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -706,7 +704,7 @@ def test_structured_datatype_validation(tmpdir): buff = helpers.yaml_to_asdf(content) with pytest.raises(jsonschema.ValidationError): - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -724,7 +722,7 @@ def test_structured_datatype_validation(tmpdir): buff = helpers.yaml_to_asdf(content) with pytest.raises(jsonschema.ValidationError): - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -735,7 +733,7 @@ def test_structured_datatype_validation(tmpdir): buff = helpers.yaml_to_asdf(content) with pytest.raises(jsonschema.ValidationError): - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -751,7 +749,7 @@ def test_structured_datatype_validation(tmpdir): buff = helpers.yaml_to_asdf(content) with pytest.raises(jsonschema.ValidationError): - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass content = """ @@ -766,7 +764,7 @@ def test_structured_datatype_validation(tmpdir): """ buff = helpers.yaml_to_asdf(content) - with asdf.open(buff, extensions=CustomExtension()) as ff: + with asdf.open(buff, extensions=CustomExtension()): pass @@ -787,7 +785,7 @@ def test_inline_shape_mismatch(): buff = helpers.yaml_to_asdf(content) with pytest.raises(ValueError): - with asdf.open(buff) as ff: + with asdf.open(buff): pass @@ -808,12 +806,11 @@ def test_simple_object_array(tmpdir): def test_tagged_object_array(tmpdir): # See https://github.com/spacetelescope/asdf/issues/383 for feature # request - astropy = pytest.importorskip('astropy') - from astropy.units.quantity import Quantity + quantity = pytest.importorskip('astropy.units.quantity') objdata = np.empty((3, 3), dtype=object) for i, _ in enumerate(objdata.flat): - objdata.flat[i] = Quantity(i, 'angstrom') + objdata.flat[i] = quantity.Quantity(i, 'angstrom') helpers.assert_roundtrip_tree({'bizbaz': objdata}, tmpdir) diff --git a/asdf/tests/conftest.py b/asdf/tests/conftest.py index f28078517..41a973ec5 100644 --- a/asdf/tests/conftest.py +++ b/asdf/tests/conftest.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- import pytest -import numpy as np from . import create_small_tree, create_large_tree diff --git a/asdf/tests/test_array_blocks.py b/asdf/tests/test_array_blocks.py index 5199eacd8..9905f10cf 100644 --- a/asdf/tests/test_array_blocks.py +++ b/asdf/tests/test_array_blocks.py @@ -324,8 +324,6 @@ def test_update_add_array(tmpdir): ff = asdf.AsdfFile(tree) ff.write_to(path, pad_blocks=True) - original_size = os.stat(path).st_size - with asdf.open(os.path.join(tmpdir, "test.asdf"), mode="rw") as ff: ff.tree['arrays'].append(np.arange(32)) ff.update() @@ -504,8 +502,8 @@ def test_deferred_block_loading(small_tree): buff.seek(0) with asdf.open(buff) as ff2: assert len([x for x in ff2.blocks.blocks if isinstance(x, block.Block)]) == 1 - x = ff2.tree['science_data'] * 2 - x = ff2.tree['not_shared'] * 2 + ff2.tree['science_data'] * 2 + ff2.tree['not_shared'] * 2 assert len([x for x in ff2.blocks.blocks if isinstance(x, block.Block)]) == 2 with pytest.raises(ValueError): @@ -535,7 +533,8 @@ def test_block_index(): assert isinstance(ff2.blocks._internal_blocks[99], block.Block) # Force the loading of one array - x = ff2.tree['arrays'][50] * 2 + ff2.tree['arrays'][50] * 2 + for i in range(2, 99): if i == 50: assert isinstance(ff2.blocks._internal_blocks[i], block.Block) @@ -777,14 +776,13 @@ def test_open_no_memmap(tmpdir): with asdf.open(tmpfile) as af: array = af.tree['array'] # Make sure to access the block so that it gets loaded - x = array[0] + array[0] assert array.block._memmapped == True assert isinstance(array.block._data, np.memmap) # Test that if we ask for copy, we do not get memmapped arrays with asdf.open(tmpfile, copy_arrays=True) as af: array = af.tree['array'] - x = array[0] assert array.block._memmapped == False # We can't just check for isinstance(..., np.array) since this will # be true for np.memmap as well diff --git a/asdf/tests/test_asdftypes.py b/asdf/tests/test_asdftypes.py index 6c125aeaf..4ffd87420 100644 --- a/asdf/tests/test_asdftypes.py +++ b/asdf/tests/test_asdftypes.py @@ -1,11 +1,9 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst # -*- coding: utf-8 -*- - import io import os import sys -import copy from fractions import Fraction import pytest @@ -277,8 +275,7 @@ def types(self): """ buff = helpers.yaml_to_asdf(yaml) with pytest.warns(None) as w: - data = asdf.open( - buff, ignore_version_mismatch=False, + asdf.open(buff, ignore_version_mismatch=False, extensions=CustomFlowExtension()) assert len(w) == 1, helpers.display_warnings(w) assert str(w[0].message) == ( @@ -467,7 +464,7 @@ def from_tree(cls, tree, ctx): @classmethod def to_tree(cls, data, ctx): - tree = dict(c=data.c, d=data.d) + return dict(c=data.c, d=data.d) class CustomFlowExtension(CustomExtension): @property @@ -552,7 +549,7 @@ class TestType5(types.CustomType): class TestType6(types.CustomType): supported_versions = 'blue' with pytest.raises(ValueError): - class TestType6(types.CustomType): + class TestType7(types.CustomType): supported_versions = ['1.1.0', '2.2.0', 'blue'] def test_supported_versions(): @@ -576,14 +573,13 @@ def from_tree(cls, tree, ctx): return CustomFlow(c=tree['a'], d=tree['b']) else: return CustomFlow(**tree) - return CustomFlow(**kwargs) @classmethod def to_tree(cls, data, ctx): if cls.version == '1.0.0': - tree = dict(a=data.c, b=data.d) + return dict(a=data.c, b=data.d) else: - tree = dict(c=data.c, d=data.d) + return dict(c=data.c, d=data.d) class CustomFlowExtension(CustomExtension): @property @@ -636,7 +632,7 @@ def types(self): buff = helpers.yaml_to_asdf(yaml) with pytest.warns(None) as _warnings: - data = asdf.open(buff, extensions=CustomFlowExtension()) + asdf.open(buff, extensions=CustomFlowExtension()) assert len(_warnings) == 1 assert str(_warnings[0].message) == ( @@ -667,8 +663,7 @@ def test_extension_override(tmpdir): def test_extension_override_subclass(tmpdir): gwcs = pytest.importorskip('gwcs', '0.12.0') - astropy = pytest.importorskip('astropy', '4.0.0') - from astropy.modeling import models + pytest.importorskip('astropy', '4.0.0') from asdf.extension import default_extensions default_extensions.reset() @@ -871,9 +866,7 @@ def other(self): assert af['coord'].other == [1,2,3,4] -def test_subclass_decorator_warning(tmpdir): - - tmpfile = str(tmpdir.join('fraction.asdf')) +def test_subclass_decorator_warning(): FractionType = fractiontype_factory() @@ -898,7 +891,7 @@ def custom_attribute(self): tree = dict(fraction=MyFraction(7, 9, custom='TESTING!')) with pytest.warns(UserWarning) as w: - with asdf.AsdfFile(tree, extensions=FractionExtension()) as af: + with asdf.AsdfFile(tree, extensions=FractionExtension()): pass assert len(w) == 1, helpers.display_warnings(w) assert str(w[0].message).startswith("Failed to add subclass attribute(s)") diff --git a/asdf/tests/test_file_format.py b/asdf/tests/test_file_format.py index 1fc0fd579..5fccf802b 100644 --- a/asdf/tests/test_file_format.py +++ b/asdf/tests/test_file_format.py @@ -216,7 +216,7 @@ def test_invalid_version(tmpdir): ...""" buff = io.BytesIO(content) with pytest.raises(ValueError): - with asdf.open(buff) as ff: + with asdf.open(buff): pass diff --git a/asdf/tests/test_fits_embed.py b/asdf/tests/test_fits_embed.py index bebc708a5..0e8c70f51 100644 --- a/asdf/tests/test_fits_embed.py +++ b/asdf/tests/test_fits_embed.py @@ -410,7 +410,7 @@ def test_extension_check(): testfile = get_test_data_path('extension_check.fits') with pytest.warns(None) as warnings: - with asdf.open(testfile) as ff: + with asdf.open(testfile): pass assert len(warnings) == 1, display_warnings(warnings) @@ -419,13 +419,13 @@ def test_extension_check(): # Make sure that suppressing the warning works as well with pytest.warns(None) as warnings: - with asdf.open(testfile, ignore_missing_extensions=True) as ff: + with asdf.open(testfile, ignore_missing_extensions=True): pass assert len(warnings) == 0, display_warnings(warnings) with pytest.raises(RuntimeError): - with asdf.open(testfile, strict_extension_check=True) as ff: + with asdf.open(testfile, strict_extension_check=True): pass def test_verify_with_astropy(tmpdir): diff --git a/asdf/tests/test_generic_io.py b/asdf/tests/test_generic_io.py index 5bc2cc96c..bbf60f8ee 100644 --- a/asdf/tests/test_generic_io.py +++ b/asdf/tests/test_generic_io.py @@ -414,21 +414,21 @@ def test_invalid_obj(tmpdir): path = os.path.join(str(tmpdir), 'test.asdf') with generic_io.get_file(path, 'w') as fd: with pytest.raises(ValueError): - fd2 = generic_io.get_file(fd, 'r') + generic_io.get_file(fd, 'r') with pytest.raises(ValueError): - fd2 = generic_io.get_file("http://www.google.com", "w") + generic_io.get_file("http://www.google.com", "w") with pytest.raises(TypeError): - fd2 = generic_io.get_file(io.StringIO()) + generic_io.get_file(io.StringIO()) with open(path, 'rb') as fd: with pytest.raises(ValueError): - fd2 = generic_io.get_file(fd, 'w') + generic_io.get_file(fd, 'w') with io.open(path, 'rb') as fd: with pytest.raises(ValueError): - fd2 = generic_io.get_file(fd, 'w') + generic_io.get_file(fd, 'w') with generic_io.get_file(sys.__stdout__, 'w'): pass @@ -772,8 +772,7 @@ def test_truncated_reader(): def test_is_asdf(tmpdir): # test fits - astropy = pytest.importorskip('astropy') - from astropy.io import fits + fits = pytest.importorskip('astropy.io.fits') hdul = fits.HDUList() phdu= fits.PrimaryHDU() diff --git a/asdf/tests/test_reference_files.py b/asdf/tests/test_reference_files.py index 8bf4add67..a920e2305 100644 --- a/asdf/tests/test_reference_files.py +++ b/asdf/tests/test_reference_files.py @@ -9,7 +9,7 @@ from asdf import open as asdf_open from asdf import versioning -from .helpers import assert_tree_match, display_warnings +from .helpers import assert_tree_match _REFFILE_PATH = os.path.join(os.path.dirname(__file__), '..', '..', @@ -50,7 +50,7 @@ def _compare_func(): # Make sure to only suppress warnings when they are expected. # However, there's still a chance of missing warnings that we # actually care about here. - with pytest.warns(RuntimeWarning) as w: + with pytest.warns(RuntimeWarning): _compare_func() else: _compare_func() diff --git a/asdf/tests/test_schema.py b/asdf/tests/test_schema.py index 74f18b914..900b1b868 100644 --- a/asdf/tests/test_schema.py +++ b/asdf/tests/test_schema.py @@ -2,25 +2,19 @@ # -*- coding: utf-8 -*- import io -import re -import warnings from jsonschema import ValidationError - -import yaml -import pytest - import numpy as np from numpy.testing import assert_array_equal +import pytest import asdf -from asdf import types from asdf import extension from asdf import resolver from asdf import schema +from asdf import types from asdf import util from asdf import yamlutil - from asdf.tests import helpers, CustomExtension @@ -43,7 +37,7 @@ def from_tree(cls, tree, ctx): def test_tagging_scalars(): - astropy = pytest.importorskip('astropy', '3.0.0') + pytest.importorskip('astropy', '3.0.0') from astropy import units as u yaml = """ @@ -602,7 +596,7 @@ def test_nested_array_yaml(tmpdir): def test_type_missing_dependencies(): - astropy = pytest.importorskip('astropy', '3.0.0') + pytest.importorskip('astropy', '3.0.0') class MissingType(types.CustomType): name = 'missing' diff --git a/asdf/tests/test_versioning.py b/asdf/tests/test_versioning.py index 83664023c..37b605967 100644 --- a/asdf/tests/test_versioning.py +++ b/asdf/tests/test_versioning.py @@ -1,9 +1,9 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst # -*- coding: utf-8 -*- +from itertools import combinations import pytest -from itertools import combinations from asdf.versioning import ( AsdfVersion, diff --git a/asdf/tests/test_yaml.py b/asdf/tests/test_yaml.py index 430d7bdea..9ecf3490c 100644 --- a/asdf/tests/test_yaml.py +++ b/asdf/tests/test_yaml.py @@ -180,11 +180,11 @@ def test_implicit_conversion_warning(): } with pytest.warns(UserWarning, match="Failed to serialize instance"): - with asdf.AsdfFile(tree) as af: + with asdf.AsdfFile(tree): pass with pytest.warns(None) as w: - with asdf.AsdfFile(tree, ignore_implicit_conversion=True) as af: + with asdf.AsdfFile(tree, ignore_implicit_conversion=True): assert len(w) == 0 diff --git a/asdf/treeutil.py b/asdf/treeutil.py index 781a32238..e36b98a17 100644 --- a/asdf/treeutil.py +++ b/asdf/treeutil.py @@ -4,7 +4,6 @@ Utility functions for managing tree-like data structures. """ -import inspect import warnings import types from contextlib import contextmanager diff --git a/asdf/types.py b/asdf/types.py index 707388728..f1435d07d 100644 --- a/asdf/types.py +++ b/asdf/types.py @@ -311,7 +311,6 @@ def to_tree_tagged(cls, node, ctx): An instance of `asdf.tagged.Tagged`. """ obj = cls.to_tree(node, ctx) - yaml_tag = cls.yaml_tag node_cls = type(node) cls_name = node_cls.__name__ diff --git a/asdf/util.py b/asdf/util.py index ba6e8d967..c72db9801 100644 --- a/asdf/util.py +++ b/asdf/util.py @@ -12,8 +12,6 @@ import numpy as np -from .extern.decorators import add_common_docstring - __all__ = ['human_list', 'get_array_base', 'get_base_uri', 'filepath_to_url', 'iter_subclasses', 'calculate_padding', 'resolve_name', 'NotSet', diff --git a/asdf/version.py b/asdf/version.py index 95d757da1..ba1cf1f1f 100644 --- a/asdf/version.py +++ b/asdf/version.py @@ -5,3 +5,5 @@ except DistributionNotFound: # package is not installed version = "unknown" + +__all__ = ['version'] diff --git a/compatibility_tests/common.py b/compatibility_tests/common.py index d9487bbe8..bf2c070fe 100644 --- a/compatibility_tests/common.py +++ b/compatibility_tests/common.py @@ -1,5 +1,5 @@ import asdf -from asdf.versioning import supported_versions, AsdfVersion +from asdf.versioning import supported_versions import numpy as np diff --git a/docs/conf.py b/docs/conf.py index 0988cb12c..cb14fb21e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -37,7 +37,7 @@ pass try: - from sphinx_astropy.conf.v1 import * # noqa + from sphinx_astropy.conf.v1 import exclude_patterns, rst_epilog, extensions except ImportError: print('ERROR: the documentation requires the sphinx-astropy package to be installed') sys.exit(1) diff --git a/docs/sphinxext/__init__.py b/docs/sphinxext/__init__.py index ca2bcad67..ccbc6cb99 100644 --- a/docs/sphinxext/__init__.py +++ b/docs/sphinxext/__init__.py @@ -1,3 +1,2 @@ # Licensed under a 3-clause BSD style license - see LICENSE.rst # -*- coding: utf-8 -*- - diff --git a/docs/sphinxext/example.py b/docs/sphinxext/example.py index 6790d2657..59537774c 100644 --- a/docs/sphinxext/example.py +++ b/docs/sphinxext/example.py @@ -18,7 +18,7 @@ import asdf from asdf import AsdfFile from asdf.constants import ASDF_MAGIC, BLOCK_FLAG_STREAMED -from asdf import versioning, util +from asdf import versioning version_string = str(versioning.default_version) @@ -49,7 +49,7 @@ def run(self): try: try: exec(code, GLOBALS) - except: + except Exception: print(code) raise diff --git a/pytest_asdf/plugin.py b/pytest_asdf/plugin.py index 2b6ec3152..af54334cf 100644 --- a/pytest_asdf/plugin.py +++ b/pytest_asdf/plugin.py @@ -175,7 +175,6 @@ def runtest(self): try: with pytest.warns(None) as w: - import warnings ff._open_impl(ff, buff, mode='rw') # Do not tolerate any warnings that occur during schema validation assert len(w) == 0, helpers.display_warnings(w) diff --git a/setup.cfg b/setup.cfg index d1ff58b12..88ec68db9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -43,6 +43,7 @@ docs = astropy graphviz matplotlib + docutils tests = pytest astropy @@ -92,4 +93,5 @@ addopts = --doctest-rst [flake8] exclude = extern -select = E101 W191 W291 W292 W293 W391 E11 E502 E722 E901 E902 +select = F, W, E101 W191 W291 W292 W293 W391 E11 E502 E722 E901 E902 +ignore = E203, W503, W504, W605 From b148a95713d288346472c972b05bf95baaeb9a7c Mon Sep 17 00:00:00 2001 From: James Davies Date: Thu, 14 May 2020 23:38:27 -0400 Subject: [PATCH 2/9] Remove redundant flake8 codes from setup.py --- setup.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index 88ec68db9..128fd4d29 100644 --- a/setup.cfg +++ b/setup.cfg @@ -93,5 +93,5 @@ addopts = --doctest-rst [flake8] exclude = extern -select = F, W, E101 W191 W291 W292 W293 W391 E11 E502 E722 E901 E902 -ignore = E203, W503, W504, W605 +select = F,W,E101,E111,E502,E722,E901,E902 +ignore = W503,W504 From bf349526d1da549117d75bfd20ff3ad414aec1fc Mon Sep 17 00:00:00 2001 From: James Davies Date: Thu, 14 May 2020 23:38:55 -0400 Subject: [PATCH 3/9] Remove private function from asdftypes --- asdf/asdftypes.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/asdf/asdftypes.py b/asdf/asdftypes.py index 62f9ee682..1fc3024f2 100644 --- a/asdf/asdftypes.py +++ b/asdf/asdftypes.py @@ -6,11 +6,10 @@ from .exceptions import AsdfDeprecationWarning # This is not exhaustive, but represents the public API from .versioning import join_tag_version, split_tag_version -from .types import (AsdfType, CustomType, format_tag, ExtensionTypeMeta, - _all_asdftypes) +from .types import (AsdfType, CustomType, format_tag, ExtensionTypeMeta) __all__ = ["join_tag_version", "split_tag_version", "AsdfType", "CustomType", - "format_tag", "ExtensionTypeMeta", "_all_asdftypes"] + "format_tag", "ExtensionTypeMeta"] warnings.warn( "The module asdf.asdftypes has been deprecated and will be removed in 3.0. " From 8fb2493b4134a70291b1521bce93d61b0c761e61 Mon Sep 17 00:00:00 2001 From: James Davies Date: Fri, 15 May 2020 09:28:11 -0400 Subject: [PATCH 4/9] Fix doc build issue --- asdf/__init__.py | 4 ++-- docs/conf.py | 2 +- setup.cfg | 2 +- tox.ini | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/asdf/__init__.py b/asdf/__init__.py index b483e1b7a..677a15766 100644 --- a/asdf/__init__.py +++ b/asdf/__init__.py @@ -16,7 +16,7 @@ __all__ = [ 'AsdfFile', 'CustomType', 'AsdfExtension', 'Stream', 'open', 'test', 'commands', 'IntegerType', 'ExternalArrayReference', 'info', '__version__', - '__githash__', 'ValidationError' + '__githash__' ] @@ -29,7 +29,7 @@ from .tags.core.external_reference import ExternalArrayReference from ._convenience import info -from jsonschema import ValidationError +from jsonschema import ValidationError # noqa: F401 open = open_asdf # Avoid redundancy/confusion in the top-level namespace diff --git a/docs/conf.py b/docs/conf.py index cb14fb21e..4b17159c1 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -37,7 +37,7 @@ pass try: - from sphinx_astropy.conf.v1 import exclude_patterns, rst_epilog, extensions + from sphinx_astropy.conf.v1 import * except ImportError: print('ERROR: the documentation requires the sphinx-astropy package to be installed') sys.exit(1) diff --git a/setup.cfg b/setup.cfg index 128fd4d29..36f4153aa 100644 --- a/setup.cfg +++ b/setup.cfg @@ -92,6 +92,6 @@ asdf_schema_ignore_unrecognized_tag = true addopts = --doctest-rst [flake8] -exclude = extern +exclude = extern, docs/conf.py select = F,W,E101,E111,E502,E722,E901,E902 ignore = W503,W504 diff --git a/tox.ini b/tox.ini index 47e36e2f2..760cfe59f 100644 --- a/tox.ini +++ b/tox.ini @@ -78,7 +78,7 @@ basepython= python3.8 deps= flake8 commands= - flake8 asdf compatibility_tests --count + flake8 --count [testenv:coverage] basepython= python3.8 From f9177b8c9957a4e456a9f566b363dbc5de6208fd Mon Sep 17 00:00:00 2001 From: James Davies Date: Fri, 15 May 2020 09:53:25 -0400 Subject: [PATCH 5/9] Add .tox to flake8 ignore dirs --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 36f4153aa..74aa54186 100644 --- a/setup.cfg +++ b/setup.cfg @@ -92,6 +92,6 @@ asdf_schema_ignore_unrecognized_tag = true addopts = --doctest-rst [flake8] -exclude = extern, docs/conf.py +exclude = extern, docs/conf.py, .tox select = F,W,E101,E111,E502,E722,E901,E902 ignore = W503,W504 From db0cb21423f03113bada12d69fe8aed2e6b92140 Mon Sep 17 00:00:00 2001 From: James Davies Date: Fri, 15 May 2020 10:26:09 -0400 Subject: [PATCH 6/9] Add .eggs to ignore --- setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index 74aa54186..75a0521bf 100644 --- a/setup.cfg +++ b/setup.cfg @@ -92,6 +92,6 @@ asdf_schema_ignore_unrecognized_tag = true addopts = --doctest-rst [flake8] -exclude = extern, docs/conf.py, .tox +exclude = extern, docs/conf.py, .tox, .eggs select = F,W,E101,E111,E502,E722,E901,E902 ignore = W503,W504 From 0d463d1d7ce519c177c14a85d34d626f7a3ea047 Mon Sep 17 00:00:00 2001 From: James Davies Date: Fri, 15 May 2020 10:39:01 -0400 Subject: [PATCH 7/9] Remove extern/decorators.py --- asdf/extern/decorators.py | 43 --------------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 asdf/extern/decorators.py diff --git a/asdf/extern/decorators.py b/asdf/extern/decorators.py deleted file mode 100644 index f5054d8dd..000000000 --- a/asdf/extern/decorators.py +++ /dev/null @@ -1,43 +0,0 @@ -# -*- coding: utf-8 -*- -# This code was taken from sunpy, which is licensed under a 3-clause BSD style -# license - see licenses/SUNPY_LICENSE.rst -"""Sundry function and class decorators.""" - - -__all__ = ['add_common_docstring'] - - -class add_common_docstring(object): - """ - A function decorator that will append and/or prepend an addendum - to the docstring of the target function. - - - Parameters - ---------- - append : `str`, optional - A string to append to the end of the functions docstring. - - prepend : `str`, optional - A string to prepend to the start of the functions docstring. - - **kwargs : `dict`, optional - A dictionary to format append and prepend strings. - """ - - def __init__(self, append=None, prepend=None, **kwargs): - if kwargs: - append = append.format(**kwargs) - prepend = prepend.format(**kwargs) - self.append = append - self.prepend = prepend - - def __call__(self, func): - func.__doc__ = func.__doc__ if func.__doc__ else '' - self.append = self.append if self.append else '' - self.prepend = self.prepend if self.prepend else '' - if self.append and isinstance(func.__doc__, str): - func.__doc__ += self.append - if self.prepend and isinstance(func.__doc__, str): - func.__doc__ = self.prepend + func.__doc__ - return func From fa3d330ad88ce3af2e3ecd18da1998d949ea3c54 Mon Sep 17 00:00:00 2001 From: James Davies Date: Fri, 15 May 2020 11:00:17 -0400 Subject: [PATCH 8/9] Fix doc build for ValidationError from jsonschema --- asdf/__init__.py | 4 ++-- docs/asdf/user_api.rst | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/asdf/__init__.py b/asdf/__init__.py index 677a15766..b483e1b7a 100644 --- a/asdf/__init__.py +++ b/asdf/__init__.py @@ -16,7 +16,7 @@ __all__ = [ 'AsdfFile', 'CustomType', 'AsdfExtension', 'Stream', 'open', 'test', 'commands', 'IntegerType', 'ExternalArrayReference', 'info', '__version__', - '__githash__' + '__githash__', 'ValidationError' ] @@ -29,7 +29,7 @@ from .tags.core.external_reference import ExternalArrayReference from ._convenience import info -from jsonschema import ValidationError # noqa: F401 +from jsonschema import ValidationError open = open_asdf # Avoid redundancy/confusion in the top-level namespace diff --git a/docs/asdf/user_api.rst b/docs/asdf/user_api.rst index 6d743470b..e434f5108 100644 --- a/docs/asdf/user_api.rst +++ b/docs/asdf/user_api.rst @@ -5,5 +5,6 @@ User API .. automodapi:: asdf :include-all-objects: :inherited-members: + :skip: ValidationError .. automodapi:: asdf.search From 26039d8e2d4ef0ed0ed1ddca1d75150e7524a1d9 Mon Sep 17 00:00:00 2001 From: James Davies Date: Fri, 15 May 2020 11:28:28 -0400 Subject: [PATCH 9/9] Update CHANGES.rst --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 5ddfb42be..644853eea 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,8 @@ - Add option to ``asdf.open`` and ``fits_embed.AsdfInFits.open`` that disables validation on read. [#792] +- Fix bugs and code style found by adding F and W ``flake8`` checks. [#797] + 2.6.1 (unreleased) ------------------