Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate warnings from test suite #799

Merged
merged 2 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ matrix:
# Test against an installed asdf package
- env: TOXENV=packaged

# Test with warnings converted to errors
- env: TOXENV=warnings

# Test on OS X
- env:
- TOXENV=py38
Expand Down Expand Up @@ -102,6 +105,8 @@ matrix:

- env: TOXENV=py38-numpydev

- env: TOXENV=warnings

install: pip install tox

script: tox
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

- Fix bugs and code style found by adding F and W ``flake8`` checks. [#797]

- Eliminate warnings in pytest plugin by using ``from_parent``
when available. [#799]

2.6.1 (unreleased)
------------------

Expand Down
6 changes: 6 additions & 0 deletions asdf/commands/tests/test_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from functools import partial

import pytest

from ...tests import helpers
from . import data as test_data

Expand All @@ -9,6 +11,10 @@
get_test_data_path = partial(helpers.get_test_data_path, module=test_data)


# The test file we're using here contains objects whose schemas
# have been dropped from the ASDF Standard. We should select
# a new file once the locations of schemas are more stable.
@pytest.mark.filterwarnings("ignore::asdf.exceptions.AsdfConversionWarning")
def test_info_command(capsys):
file_path = get_test_data_path("frames0.asdf")

Expand Down
2 changes: 1 addition & 1 deletion asdf/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def _assert_extension_type_correctness(extension, extension_type, resolver):

try:
with generic_io.get_file(schema_location) as f:
schema = yaml.load(f.read())
schema = yaml.safe_load(f.read())
except Exception:
assert False, (
"{} supports tag, {}, ".format(extension_type.__name__, check_type.yaml_tag) +
Expand Down
50 changes: 33 additions & 17 deletions pytest_asdf/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,24 @@ def pytest_addoption(parser):


class AsdfSchemaFile(pytest.File):
@classmethod
def from_parent(cls, parent, *, fspath, skip_examples=False, ignore_unrecognized_tag=False, **kwargs):
if hasattr(super(), "from_parent"):
result = super().from_parent(parent, fspath=fspath, **kwargs)
else:
result = AsdfSchemaFile(fspath, parent, **kwargs)

def __init__(self, *args, skip_examples=False, ignore_unrecognized_tag=False, **kwargs):
super().__init__(*args, **kwargs)
self.skip_examples = skip_examples
self.ignore_unrecognized_tag = ignore_unrecognized_tag
result.skip_examples = skip_examples
result.ignore_unrecognized_tag = ignore_unrecognized_tag
return result

def collect(self):
yield AsdfSchemaItem(str(self.fspath), self)
yield AsdfSchemaItem.from_parent(self, self.fspath)
if not self.skip_examples:
for example in self.find_examples_in_schema():
yield AsdfSchemaExampleItem(
str(self.fspath),
yield AsdfSchemaExampleItem.from_parent(
self,
self.fspath,
example,
ignore_unrecognized_tag=self.ignore_unrecognized_tag
)
Expand All @@ -71,9 +76,15 @@ def find_examples_in_schema(self):


class AsdfSchemaItem(pytest.Item):
def __init__(self, schema_path, parent):
super(AsdfSchemaItem, self).__init__(schema_path, parent)
self.schema_path = schema_path
@classmethod
def from_parent(cls, parent, schema_path, **kwargs):
if hasattr(super(), "from_parent"):
result = super().from_parent(parent, name=str(schema_path), **kwargs)
else:
result = AsdfSchemaItem(str(schema_path), parent, **kwargs)

result.schema_path = schema_path
return result

def runtest(self):
from asdf import schema
Expand Down Expand Up @@ -122,12 +133,17 @@ def parse_schema_filename(filename):


class AsdfSchemaExampleItem(pytest.Item):
def __init__(self, schema_path, parent, example, ignore_unrecognized_tag=False):
@classmethod
def from_parent(cls, parent, schema_path, example, ignore_unrecognized_tag=False, **kwargs):
test_name = "{}-example".format(schema_path)
super(AsdfSchemaExampleItem, self).__init__(test_name, parent)
self.filename = str(schema_path)
self.example = example
self.ignore_unrecognized_tag = ignore_unrecognized_tag
if hasattr(super(), "from_parent"):
result = super().from_parent(parent, name=test_name, **kwargs)
else:
result = AsdfSchemaExampleItem(test_name, parent, **kwargs)
result.filename = str(schema_path)
result.example = example
result.ignore_unrecognized_tag = ignore_unrecognized_tag
return result

def _find_standard_version(self, name, version):
from asdf import versioning
Expand Down Expand Up @@ -211,9 +227,9 @@ def pytest_collect_file(path, parent):

for root in schema_roots:
if str(path).startswith(root) and path.purebasename not in skip_names:
return AsdfSchemaFile(
path,
return AsdfSchemaFile.from_parent(
parent,
fspath=path,
skip_examples=(path.purebasename in skip_examples),
ignore_unrecognized_tag=ignore_unrecognized_tag,
)
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ norecursedirs = build docs/_build
doctest_plus = enabled
remote_data_strict = True
open_files_ignore = test.fits asdf.fits
# The asdf.asdftypes module emits a warning on import,
# which pytest trips over during collection:
filterwarnings =
ignore::asdf.exceptions.AsdfDeprecationWarning:asdf.asdftypes
# Configuration for pytest-doctestplus
text_file_format = rst
# Account for both the astropy test runner case and the native pytest case
Expand Down
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ deps=
numpy12: numpy==1.12
numpydev,astrodev,s390x: cython
extras= all,tests
# astropy will complain if the home directory is missing
passenv= HOME
commands=
pytest --remote-data

Expand All @@ -37,6 +39,11 @@ install_command= python -m pip install --no-cache-dir {opts} {packages}
basepython= python3.8
pip_pre= true

[testenv:warnings]
basepython= python3.8
commands=
pytest --remote-data -W error -W ignore::asdf.exceptions.AsdfDeprecationWarning:asdf.asdftypes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the -W ignore::asdf.exceptions.AsdfDeprecationWarning:asdf.asdftypes be removed here as we capture and ignore it elsewhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or does this override the filterwarnings directive in [tool:pytest]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that's the issue -- pytest uses the last matched rule to determine if the warning should be ignored, and filterwarnings seems to get loaded first.


[testenv:packaged]
basepython= python3.8
# The default tox working directory is in .tox in the source directory. If we
Expand Down