Skip to content

Commit 20a3ffa

Browse files
committed
more consistency
1 parent fa5ec7e commit 20a3ffa

18 files changed

+81
-79
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
### Incompatible API changes:
66

7+
- Renamed nodes and node properties for consistency and clarity:
8+
- renamed `DataArrayNode` into `VariableNode`
9+
- renamed `DataArrayNode.data_array` into `VariableNode.array`
10+
711
- Changed general use of term _verify_ into _validate_:
812
- prefixed `RuleOp` methods by `validate_` for clarity.
913
- renamed `XRLint.verify_datasets()` into `validate_files()`

tests/test_linter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from xrlint.config import Config, ConfigObject
77
from xrlint.constants import CORE_PLUGIN_NAME, NODE_ROOT_NAME
88
from xrlint.linter import Linter, new_linter
9-
from xrlint.node import AttrNode, AttrsNode, DataArrayNode, DatasetNode
9+
from xrlint.node import AttrNode, AttrsNode, VariableNode, DatasetNode
1010
from xrlint.plugin import new_plugin
1111
from xrlint.processor import ProcessorOp
1212
from xrlint.result import Message, Result
@@ -101,15 +101,15 @@ def validate_attr(self, ctx: RuleContext, node: AttrNode):
101101

102102
@plugin.define_rule("no-empty-attrs")
103103
class AttrsVer(RuleOp):
104-
def attrs(self, ctx: RuleContext, node: AttrsNode):
104+
def validate_attrs(self, ctx: RuleContext, node: AttrsNode):
105105
if not node.attrs:
106106
ctx.report("Empty attributes")
107107

108108
@plugin.define_rule("data-var-dim-must-have-coord")
109109
class DataArrayVer(RuleOp):
110-
def validate_data_array(self, ctx: RuleContext, node: DataArrayNode):
110+
def validate_variable(self, ctx: RuleContext, node: VariableNode):
111111
if node.in_data_vars():
112-
for dim_name in node.data_array.dims:
112+
for dim_name in node.array.dims:
113113
if dim_name not in ctx.dataset.coords:
114114
ctx.report(
115115
f"Dimension {dim_name!r}"

xrlint/_linter/apply.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from xrlint.node import AttrNode, AttrsNode, DataArrayNode, DatasetNode
1+
from xrlint.node import AttrNode, AttrsNode, VariableNode, DatasetNode
22
from xrlint.rule import RuleConfig, RuleExit, RuleOp
33

44
from ..constants import NODE_ROOT_NAME
@@ -58,49 +58,47 @@ def _visit_dataset_node(rule_op: RuleOp, context: RuleContextImpl, node: Dataset
5858
attrs=node.dataset.attrs,
5959
),
6060
)
61-
for name, data_array in node.dataset.coords.items():
62-
_visit_data_array_node(
61+
for name, variable in node.dataset.coords.items():
62+
_visit_variable_node(
6363
rule_op,
6464
context,
65-
DataArrayNode(
65+
VariableNode(
6666
parent=node,
6767
path=f"{node.path}.coords[{name!r}]",
6868
name=name,
69-
data_array=data_array,
69+
array=variable,
7070
),
7171
)
72-
for name, data_array in node.dataset.data_vars.items():
73-
_visit_data_array_node(
72+
for name, variable in node.dataset.data_vars.items():
73+
_visit_variable_node(
7474
rule_op,
7575
context,
76-
DataArrayNode(
76+
VariableNode(
7777
parent=node,
7878
path=f"{node.path}.data_vars[{name!r}]",
7979
name=name,
80-
data_array=data_array,
80+
array=variable,
8181
),
8282
)
8383

8484

85-
def _visit_data_array_node(
86-
rule_op: RuleOp, context: RuleContextImpl, node: DataArrayNode
87-
):
85+
def _visit_variable_node(rule_op: RuleOp, context: RuleContextImpl, node: VariableNode):
8886
with context.use_state(node=node):
89-
rule_op.validate_data_array(context, node)
87+
rule_op.validate_variable(context, node)
9088
_visit_attrs_node(
9189
rule_op,
9290
context,
9391
AttrsNode(
9492
parent=node,
9593
path=f"{node.path}.attrs",
96-
attrs=node.data_array.attrs,
94+
attrs=node.array.attrs,
9795
),
9896
)
9997

10098

10199
def _visit_attrs_node(rule_op: RuleOp, context: RuleContextImpl, node: AttrsNode):
102100
with context.use_state(node=node):
103-
rule_op.attrs(context, node)
101+
rule_op.validate_attrs(context, node)
104102
for name, value in node.attrs.items():
105103
_visit_attr_node(
106104
rule_op,

xrlint/all.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
FormatterRegistry,
99
)
1010
from xrlint.linter import Linter, new_linter
11-
from xrlint.node import AttrNode, AttrsNode, DataArrayNode, DatasetNode, Node
11+
from xrlint.node import AttrNode, AttrsNode, VariableNode, DatasetNode, Node
1212
from xrlint.plugin import Plugin, PluginMeta, new_plugin
1313
from xrlint.processor import Processor, ProcessorMeta, ProcessorOp, define_processor
1414
from xrlint.result import (
@@ -50,7 +50,7 @@
5050
"FormatterRegistry",
5151
"AttrNode",
5252
"AttrsNode",
53-
"DataArrayNode",
53+
"VariableNode",
5454
"DatasetNode",
5555
"Node",
5656
"Plugin",

xrlint/node.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ class DatasetNode(XarrayNode):
4343

4444

4545
@dataclass(frozen=True, kw_only=True)
46-
class DataArrayNode(XarrayNode):
47-
"""Data array node."""
46+
class VariableNode(XarrayNode):
47+
"""Variable node.
48+
Could be a coordinate or data variable.
49+
If you need to distinguish, you can use expression
50+
`node.name in ctx.dataset.coords`.
51+
"""
4852

4953
name: Hashable
50-
"""The name of the data array."""
54+
"""The name of the variable."""
5155

52-
data_array: xr.DataArray
56+
array: xr.DataArray
5357
"""The `xarray.DataArray` instance."""
5458

5559

xrlint/plugins/core/rules/content_desc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22

3-
from xrlint.node import DataArrayNode, DatasetNode
3+
from xrlint.node import VariableNode, DatasetNode
44
from xrlint.plugins.core.plugin import plugin
55
from xrlint.rule import RuleContext, RuleExit, RuleOp
66
from xrlint.util.schema import schema
@@ -85,7 +85,7 @@ def validate_dataset(self, ctx: RuleContext, node: DatasetNode):
8585
if attr_name not in dataset_attrs:
8686
ctx.report(f"Missing attribute {attr_name!r}.")
8787

88-
def validate_data_array(self, ctx: RuleContext, node: DataArrayNode):
88+
def validate_variable(self, ctx: RuleContext, node: VariableNode):
8989
if self.skip_vars:
9090
# Since dataset() has already been processed,
9191
# no need to check other nodes.
@@ -100,7 +100,7 @@ def validate_data_array(self, ctx: RuleContext, node: DataArrayNode):
100100
# Ignored variable
101101
return
102102

103-
var_attrs = node.data_array.attrs
103+
var_attrs = node.array.attrs
104104
dataset_attrs = ctx.dataset.attrs
105105
for attr_name in self.common_attrs:
106106
if attr_name not in var_attrs and attr_name not in dataset_attrs:

xrlint/plugins/core/rules/lat_lon_coordinate.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import xarray as xr
44

5-
from xrlint.node import DataArrayNode
5+
from xrlint.node import VariableNode
66
from xrlint.plugins.core.plugin import plugin
77
from xrlint.rule import RuleContext, RuleOp
88

@@ -30,13 +30,11 @@
3030
),
3131
)
3232
class LatCoordinate(RuleOp):
33-
def validate_data_array(self, ctx: RuleContext, node: DataArrayNode):
34-
if node.name in ctx.dataset.coords and _is_lat_var(
35-
str(node.name), node.data_array
36-
):
33+
def validate_variable(self, ctx: RuleContext, node: VariableNode):
34+
if node.name in ctx.dataset.coords and _is_lat_var(str(node.name), node.array):
3735
_maybe_report(
3836
ctx,
39-
node.data_array.attrs,
37+
node.array.attrs,
4038
LAT_UNITS,
4139
LAT_UNITS_ALIASES,
4240
LAT_NAME,
@@ -55,13 +53,11 @@ def validate_data_array(self, ctx: RuleContext, node: DataArrayNode):
5553
),
5654
)
5755
class LonCoordinate(RuleOp):
58-
def validate_data_array(self, ctx: RuleContext, node: DataArrayNode):
59-
if node.name in ctx.dataset.coords and _is_lon_var(
60-
str(node.name), node.data_array
61-
):
56+
def validate_variable(self, ctx: RuleContext, node: VariableNode):
57+
if node.name in ctx.dataset.coords and _is_lon_var(str(node.name), node.array):
6258
_maybe_report(
6359
ctx,
64-
node.data_array.attrs,
60+
node.array.attrs,
6561
LON_UNITS,
6662
LON_UNITS_ALIASES,
6763
LON_NAME,

xrlint/plugins/core/rules/no_empty_attrs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
description="Every dataset element should have metadata that describes it.",
1212
)
1313
class NoEmptyAttrs(RuleOp):
14-
def attrs(self, ctx: RuleContext, node: AttrsNode):
14+
def validate_attrs(self, ctx: RuleContext, node: AttrsNode):
1515
if not node.attrs:
1616
ctx.report(
1717
"Missing metadata, attributes are empty.",

xrlint/plugins/core/rules/no_empty_chunks.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from xrlint.node import DataArrayNode
1+
from xrlint.node import VariableNode
22
from xrlint.plugins.core.plugin import plugin
33
from xrlint.rule import RuleContext, RuleExit, RuleOp
44

@@ -17,17 +17,17 @@
1717
),
1818
)
1919
class NoEmptyChunks(RuleOp):
20-
def validate_dataset(self, ctx: RuleContext, node: DataArrayNode):
20+
def validate_dataset(self, ctx: RuleContext, node: VariableNode):
2121
source = ctx.dataset.encoding.get("source")
2222
is_zarr = isinstance(source, str) and source.endswith(".zarr")
2323
if not is_zarr:
2424
# if not a Zarr, no need to check further
2525
raise RuleExit
2626

27-
def validate_data_array(self, ctx: RuleContext, node: DataArrayNode):
27+
def validate_variable(self, ctx: RuleContext, node: VariableNode):
2828
if (
29-
"write_empty_chunks" not in node.data_array.encoding
30-
and "chunks" in node.data_array.encoding
31-
and "_FillValue" in node.data_array.encoding
29+
"write_empty_chunks" not in node.array.encoding
30+
and "chunks" in node.array.encoding
31+
and "_FillValue" in node.array.encoding
3232
):
3333
ctx.report("Consider writing the dataset using 'write_empty_chunks=True'.")

xrlint/plugins/core/rules/time_coordinate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22

3-
from xrlint.node import DataArrayNode
3+
from xrlint.node import VariableNode
44
from xrlint.plugins.core.plugin import plugin
55
from xrlint.rule import RuleContext, RuleOp
66

@@ -52,8 +52,8 @@
5252
),
5353
)
5454
class TimeCoordinate(RuleOp):
55-
def validate_data_array(self, ctx: RuleContext, node: DataArrayNode):
56-
array = node.data_array
55+
def validate_variable(self, ctx: RuleContext, node: VariableNode):
56+
array = node.array
5757
attrs = array.attrs
5858
encoding = array.encoding
5959

0 commit comments

Comments
 (0)