Skip to content
12 changes: 4 additions & 8 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,10 @@ And as Python module:
```python
def export_config():
return [
{"files": ["**/*.zarr", "**/*.nc"]},
{
"plugins": {
"xcube": "xrlint.plugins.xcube"
}
},
"recommended",
"xcube/recommended"
{"files": ["**/*.zarr", "**/*.nc"]},
{"plugins": {"xcube": "xrlint.plugins.xcube"}},
"recommended",
"xcube/recommended",
]
```

Expand Down
2 changes: 1 addition & 1 deletion docs/mkruleref.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# This software is distributed under the terms and conditions of the
# MIT license (https://mit-license.org/).

from xrlint.config import plugins_from_entry_points
from xrlint.plugin import Plugin
from xrlint.rule import RuleConfig
from xrlint.config import plugins_from_entry_points

# for icons, see
# https://squidfunk.github.io/mkdocs-material/reference/icons-emojis/
Expand Down
5 changes: 2 additions & 3 deletions examples/rule_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from xrlint.rule import RuleContext, RuleOp, define_rule
from xrlint.testing import RuleTest, RuleTester


# ----------------------------------------------------
# Place the rule implementation code in its own module
# ----------------------------------------------------
Expand Down Expand Up @@ -42,8 +41,8 @@ def validate_dataset(self, ctx: RuleContext, node: DatasetNode):

tester = RuleTester()

valid_dataset = xr.Dataset(attrs=dict(title="Hello World!"))
invalid_dataset = xr.Dataset(attrs=dict(title="Hello Hamburg!"))
valid_dataset = xr.Dataset(attrs={"title": "Hello World!"})
invalid_dataset = xr.Dataset(attrs={"title": "Hello Hamburg!"})

# You can use the tester to run a test directly
#
Expand Down
18 changes: 9 additions & 9 deletions notebooks/mkdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def make_dataset() -> xr.Dataset:
"""Create a dataset that passes xrlint core rules."""

return xr.Dataset(
attrs=dict(
Conventions="CF-1.10",
title="SST-Climatology Subset",
history="2025-01-31 17:31:00 - created;",
institution="BC",
source="SST CCI L4",
references="https://climate.esa.int/en/projects/sea-surface-temperature/",
comment="Demo dataset",
),
attrs={
"Conventions": "CF-1.10",
"title": "SST-Climatology Subset",
"history": "2025-01-31 17:31:00 - created;",
"institution": "BC",
"source": "SST CCI L4",
"references": "https://climate.esa.int/en/projects/sea-surface-temperature/",
"comment": "Demo dataset",
},
coords={
"x": xr.DataArray(
np.linspace(-180, 180, nx),
Expand Down
70 changes: 42 additions & 28 deletions tests/cli/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,36 +101,42 @@ def test_read_config_invalid_arg(self):
read_config(None)

def test_read_config_json_with_format_error(self):
with text_file("config.json", "{") as config_path:
with pytest.raises(
with (
text_file("config.json", "{") as config_path,
pytest.raises(
ConfigError,
match=(
"config.json:"
" Expecting property name enclosed in double quotes:"
" line 1 column 2 \\(char 1\\)"
),
):
read_config(config_path)
),
):
read_config(config_path)

def test_read_config_yaml_with_format_error(self):
with text_file("config.yaml", "}") as config_path:
with pytest.raises(
with (
text_file("config.yaml", "}") as config_path,
pytest.raises(
ConfigError,
match="config.yaml: while parsing a block node",
):
read_config(config_path)
),
):
read_config(config_path)

def test_read_config_yaml_with_type_error(self):
with text_file("config.yaml", "97") as config_path:
with pytest.raises(
with (
text_file("config.yaml", "97") as config_path,
pytest.raises(
ConfigError,
match=(
r"config\.yaml\: config must be of type"
r" Config \| ConfigObjectLike \| str \| Sequence\[ConfigObjectLike \| str\],"
r" but got int"
),
):
read_config(config_path)
),
):
read_config(config_path)

def test_read_config_with_unknown_format(self):
with pytest.raises(
Expand All @@ -141,47 +147,55 @@ def test_read_config_with_unknown_format(self):

def test_read_config_py_no_export(self):
py_code = "x = 42\n"
with text_file(self.new_config_py(), py_code) as config_path:
with pytest.raises(
with (
text_file(self.new_config_py(), py_code) as config_path,
pytest.raises(
ConfigError,
match=(
"config_1002.py: attribute 'export_config'"
" not found in module 'config_1002'"
),
):
read_config(config_path)
),
):
read_config(config_path)

def test_read_config_py_with_value_error(self):
py_code = "def export_config():\n raise ValueError('value is useless!')\n"
with text_file(self.new_config_py(), py_code) as config_path:
with pytest.raises(
with (
text_file(self.new_config_py(), py_code) as config_path,
pytest.raises(
ValueError,
match="value is useless!",
):
read_config(config_path)
),
):
read_config(config_path)

def test_read_config_py_with_os_error(self):
py_code = "def export_config():\n raise OSError('where is my hat?')\n"
with text_file(self.new_config_py(), py_code) as config_path:
with pytest.raises(
with (
text_file(self.new_config_py(), py_code) as config_path,
pytest.raises(
ConfigError,
match="where is my hat?",
):
read_config(config_path)
),
):
read_config(config_path)

def test_read_config_py_with_invalid_config_list(self):
py_code = "def export_config():\n return 42\n"
with text_file(self.new_config_py(), py_code) as config_path:
with pytest.raises(
with (
text_file(self.new_config_py(), py_code) as config_path,
pytest.raises(
ConfigError,
match=(
r"\.py: failed converting value of 'config_1003:export_config':"
r" config must be of type"
r" Config \| ConfigObjectLike \| str \| Sequence\[ConfigObjectLike \| str\],"
r" but got int"
),
):
read_config(config_path)
),
):
read_config(config_path)


class CliConfigResolveTest(unittest.TestCase):
Expand Down
10 changes: 5 additions & 5 deletions tests/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@

# noinspection PyTypeChecker
class CliMainTest(TestCase):
files = ["dataset1.zarr", "dataset1.nc", "dataset2.zarr", "dataset2.nc"]
files = ["dataset1.zarr", "dataset1.nc", "dataset2.zarr", "dataset2.nc"] # noqa: RUF012

ok_config_yaml = "- rules:\n var-units: error\n"
fail_config_yaml = "- rules:\n conventions: error\n"
# noinspection SpellCheckingInspection
invalid_config_yaml = "- recommentet\n"

datasets = dict(
dataset1=xr.Dataset(attrs={"title": "Test 1"}),
dataset2=xr.Dataset(
datasets = { # noqa: RUF012
"dataset1": xr.Dataset(attrs={"title": "Test 1"}),
"dataset2": xr.Dataset(
attrs={"title": "Test 2"},
data_vars={"v": xr.DataArray([1, 2, 3], attrs={"units": "m/s"})},
),
)
}

temp_dir: str
last_cwd: str
Expand Down
4 changes: 2 additions & 2 deletions tests/formatters/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class SimpleTest(TestCase):
errors_and_warnings = [
errors_and_warnings = [ # noqa: RUF012
Result(
file_path="test1.nc",
config_object=ConfigObject(),
Expand All @@ -23,7 +23,7 @@ class SimpleTest(TestCase):
)
]

warnings_only = [
warnings_only = [ # noqa: RUF012
Result(
file_path="test2.nc",
config_object=ConfigObject(),
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/acdd/rules/test_attributes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import xarray as xr
from xrlint.testing import RuleTest, RuleTester

from xrlint.plugins.acdd.rules.attributes import (
Attributes_1_3_Highly_Recommended,
)
from xrlint.testing import RuleTest, RuleTester

valid_1_3_highly_rec_dataset = xr.Dataset(
attrs={
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/acdd/rules/test_conventions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import xarray as xr
from xrlint.testing import RuleTest, RuleTester

from xrlint.plugins.acdd.rules.conventions import Conventions
from xrlint.testing import RuleTest, RuleTester

valid_dataset_0 = xr.Dataset(attrs={"Conventions": "ACDD-1.3"})

Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/acdd/rules/test_id_blanks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import xarray as xr
from xrlint.testing import RuleTest, RuleTester

from xrlint.plugins.acdd.rules.no_id_blanks import NoBlanksInID
from xrlint.testing import RuleTest, RuleTester

valid_dataset_0 = xr.Dataset(attrs={"id": "testing_dataset"})

Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/acdd/rules/test_iso_dates.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import xarray as xr
from xrlint.testing import RuleTest, RuleTester

from xrlint.plugins.acdd.rules.iso_dates import IsoDates
from xrlint.testing import RuleTest, RuleTester

valid_dataset_0 = xr.Dataset(attrs={"date_created": "2023-10-05T12:34:56Z"})
valid_dataset_1 = xr.Dataset(attrs={"date_modified": "2023-10-05"})
Expand Down
2 changes: 1 addition & 1 deletion tests/plugins/acdd/rules/test_metadata_link.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import xarray as xr
from xrlint.testing import RuleTest, RuleTester

from xrlint.plugins.acdd.rules.metadata_link import MetadataLink
from xrlint.testing import RuleTest, RuleTester

valid_dataset_0 = xr.Dataset(attrs={"metadata_link": "http://example.com/metadata"})
valid_dataset_1 = xr.Dataset(attrs={"metadata_link": "https://example.com/metadata"})
Expand Down
Loading
Loading