Python API
This chapter provides a plain reference for the XRLint Python API.
Overview
- The top-level API component is the class XRLint which encapsulates the functionality of the XRLint CLI.
- The
linter
module provides the functionality for linting a single dataset: new_linter() factory function and the Linter class. - The
plugin
module provides plugin related components: A factory new_plugin to create instances of the Plugin class that comprises plugin metadata represented by PluginMeta. - The
config
module provides classes that represent configuration information and provide related functionality: Config and ConfigObject. - The
rule
module provides rule related classes and functions: Rule comprising rule metadata, RuleMeta, the rule validation operations in RuleOp, as well as related to the latter RuleContext and RuleExit. Decorator define_rule allows defining rules. - The
node
module defines the nodes passed to RuleOp: base classes None, XarrayNode, and the specific nodes DataTreeNode, DatasetNode, VariableNode, AttrsNode, and AttrNode. - The
processor
module provides processor related classes and functions: Processor comprising processor metadata ProcessorMeta, and the processor operation ProcessorOp. Decorator define_processor allows defining processors. - The
result
module provides data classes that are used to represent validation results: Result composed of Messages, which again may contain Suggestions. - Finally, the
testing
module provides classes for rule testing: RuleTester that is made up of RuleTests.
Note:
the xrlint.all
convenience module exports all of the above from a
single module.
CLI API
xrlint.cli.engine.XRLint
Bases: FormatterContext
This class provides the engine behind the XRLint CLI application. It represents the highest level component in the Python API.
Source code in xrlint\cli\engine.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|
Attributes
xrlint.cli.engine.XRLint.max_warnings_exceeded
property
True
if the maximum number of warnings has been exceeded.
xrlint.cli.engine.XRLint.result_stats
property
Get current result statistics.
Functions
xrlint.cli.engine.XRLint.init_config(*extra_configs)
Initialize configuration. The function will load the configuration list from a specified configuration file, if any. Otherwise, it will search for the default configuration files in the current working directory.
Parameters:
-
*extra_configs
(ConfigLike
, default:()
) –Variable number of configuration-like arguments. For more information see the ConfigLike type alias. If provided,
extra_configs
will be appended to configuration read from configration files and passed as command line options.
Source code in xrlint\cli\engine.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
xrlint.cli.engine.XRLint.compute_config_for_file(file_path)
Compute the configuration object for the given file.
Parameters:
-
file_path
(str
) –A file path or URL.
Returns:
-
ConfigObject | None
–A configuration object or
None
if no item in the configuration list applies.
Source code in xrlint\cli\engine.py
133 134 135 136 137 138 139 140 141 142 143 |
|
xrlint.cli.engine.XRLint.print_config_for_file(file_path)
Print computed configuration object for the given file.
Parameters:
-
file_path
(str
) –A file path or URL.
Source code in xrlint\cli\engine.py
145 146 147 148 149 150 151 152 153 |
|
xrlint.cli.engine.XRLint.validate_files(files)
Validate given files or directories which may also be given as URLs. The function produces a validation result for each file.
Parameters:
-
files
(Iterable[str]
) –Iterable of files.
Returns:
-
Iterator[Result]
–Iterator of reports.
Source code in xrlint\cli\engine.py
155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
xrlint.cli.engine.XRLint.get_files(file_paths)
Provide an iterator for the list of files or directories and their computed configurations.
Directories in files
that are not ignored and not recognized by any
file pattern will be recursively traversed.
Parameters:
-
file_paths
(Iterable[str]
) –Iterable of files or directories.
Returns:
-
Iterator[tuple[str, ConfigObject]]
–An iterator of pairs comprising a file or directory path and its computed configuration.
Source code in xrlint\cli\engine.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
xrlint.cli.engine.XRLint.format_results(results)
Format the given results.
Parameters:
-
results
(Iterable[Result]
) –Iterable of results.
Returns:
-
str
–A report in plain text.
Source code in xrlint\cli\engine.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
xrlint.cli.engine.XRLint.write_report(report)
Write the validation report provided as plain text.
Source code in xrlint\cli\engine.py
252 253 254 255 256 257 258 259 |
|
xrlint.cli.engine.XRLint.init_config_file()
classmethod
Write an initial configuration file. The file is written into the current working directory.
Source code in xrlint\cli\engine.py
261 262 263 264 265 266 267 268 269 270 271 |
|
Linter API
xrlint.linter.new_linter(*configs, **config_props)
Create a new Linter
with the core plugin included and the
given additional configuration.
Parameters:
-
*configs
(ConfigLike
, default:()
) –Variable number of configuration-like arguments. For more information see the ConfigLike type alias.
-
**config_props
(Any
, default:{}
) –Individual configuration object properties. For more information refer to the properties of a ConfigObject.
Returns:
-
Linter
–A new linter instance
Source code in xrlint\linter.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
xrlint.linter.Linter
The linter.
Using the constructor directly creates an empty linter
with no configuration - even without the core plugin and
its predefined rule configurations.
If you want a linter with core plugin included use the
new_linter()
function.
Parameters:
-
*configs
(ConfigLike
, default:()
) –Variable number of configuration-like arguments. For more information see the ConfigLike type alias.
-
**config_props
(Any
, default:{}
) –Individual configuration object properties. For more information refer to the properties of a ConfigObject.
Source code in xrlint\linter.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
Attributes
xrlint.linter.Linter.config
property
Get this linter's configuration.
Functions
xrlint.linter.Linter.validate(dataset, *, file_path=None, config=None, **config_props)
Validate a dataset against applicable rules.
Parameters:
-
dataset
(Any
) –The dataset. Can be a
xr.Dataset
orxr.DataTree
instance or a file path, or any dataset source that can be opened usingxarray.open_dataset()
orxarray.open_datatree()
. -
file_path
(str | None
, default:None
) –Optional file path used for formatting messages. Useful if
dataset
is not a file path. -
config
(ConfigLike
, default:None
) –Optional configuration-like value. For more information see the ConfigLike type alias.
-
**config_props
(Any
, default:{}
) –Individual configuration object properties. For more information refer to the properties of a ConfigObject.
Returns:
-
Result
–Result of the validation.
Source code in xrlint\linter.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
Plugin API
xrlint.plugin.new_plugin(name, version='0.0.0', ref=None, docs_url=None, rules=None, processors=None, configs=None)
Create a new plugin object that can contribute rules, processors, and predefined configurations to XRLint.
Parameters:
-
name
(str
) –Plugin name. Required.
-
version
(str
, default:'0.0.0'
) –Plugin version. Defaults to
"0.0.0"
. -
ref
(str | None
, default:None
) –Plugin reference. Optional.
-
docs_url
(str | None
, default:None
) –Plugin documentation URL. Optional.
-
rules
(dict[str, Rule] | None
, default:None
) –A dictionary containing the definitions of custom rules. Optional.
-
processors
(dict[str, Processor] | None
, default:None
) –A dictionary containing custom processors. Optional.
-
configs
(dict[str, ConfigObject] | None
, default:None
) –A dictionary containing predefined configurations. Optional.
Source code in xrlint\plugin.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
xrlint.plugin.Plugin
dataclass
Bases: MappingConstructible
, JsonSerializable
A plugin that can contribute rules, processors, and predefined configurations to XRLint.
Use the factory new_plugin to create plugin instances.
Source code in xrlint\plugin.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
Attributes
xrlint.plugin.Plugin.meta
instance-attribute
Information about the plugin.
xrlint.plugin.Plugin.rules = field(default_factory=dict)
class-attribute
instance-attribute
A dictionary containing the definitions of custom rules.
xrlint.plugin.Plugin.processors = field(default_factory=dict)
class-attribute
instance-attribute
A dictionary containing named processors.
xrlint.plugin.Plugin.configs = field(default_factory=dict)
class-attribute
instance-attribute
A dictionary containing named configuration lists.
Functions
xrlint.plugin.Plugin.define_rule(name, version='0.0.0', schema=None, type='problem', description=None, docs_url=None, op_class=None)
Decorator to define a plugin rule. The method registers a new rule with the plugin.
Refer to define_rule for details.
Source code in xrlint\plugin.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
xrlint.plugin.Plugin.define_processor(name=None, version='0.0.0', op_class=None)
Decorator to define a plugin processor. The method registers a new processor with the plugin.
Refer to define_processor for details.
Source code in xrlint\plugin.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
xrlint.plugin.Plugin.define_config(name, config)
Define a named configuration.
Parameters:
-
name
(str
) –The name of the configuration.
-
config
(ConfigLike
) –A configuration-like value. For more information see the ConfigLike type alias.
Returns:
-
Config
–The configuration.
Source code in xrlint\plugin.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
xrlint.plugin.PluginMeta
dataclass
Bases: MappingConstructible
, JsonSerializable
Plugin metadata.
Source code in xrlint\plugin.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
Attributes
xrlint.plugin.PluginMeta.name
instance-attribute
Plugin name.
xrlint.plugin.PluginMeta.version = '0.0.0'
class-attribute
instance-attribute
Plugin version.
xrlint.plugin.PluginMeta.ref = None
class-attribute
instance-attribute
Plugin reference.
Specifies the location from where the plugin can be
dynamically imported.
Must have the form "
xrlint.plugin.PluginMeta.docs_url = None
class-attribute
instance-attribute
Plugin documentation URL.
Configuration API
xrlint.config.Config
dataclass
Bases: ValueConstructible
, JsonSerializable
Represents a XRLint configuration.
A Config
instance basically manages a sequence of
configuration objects.
You should not use the class constructor directly.
Instead, use the Config.from_value()
function.
Source code in xrlint\config.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
|
Attributes
xrlint.config.Config.objects = field(default_factory=list)
class-attribute
instance-attribute
The configuration objects.
Functions
xrlint.config.Config.split_global_filter(default=None)
Get a global file filter for this configuration list.
Source code in xrlint\config.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
|
xrlint.config.Config.compute_config_object(file_path)
Compute the configuration object for the given file path.
Parameters:
-
file_path
(str
) –A dataset file path.
Returns:
-
ConfigObject | None
–A
Config
object which may be empty, orNone
iffile_path
is not included by anyfiles
pattern or intentionally ignored by globalignores
.
Source code in xrlint\config.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
xrlint.config.Config.from_config(*configs, value_name=None)
classmethod
Convert variable arguments of configuration-like objects
into a new Config
instance.
Parameters:
-
*configs
(ConfigLike
, default:()
) –Variable number of configuration-like arguments. For more information see the ConfigLike type alias.
-
value_name
(str | None
, default:None
) –The value's name used for reporting errors.
Returns:
-
Config
–A new
Config
instance.
Source code in xrlint\config.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
|
xrlint.config.Config.from_value(value, value_name=None)
classmethod
Convert given value
into a Config
object.
If value
is already a Config
then it is returned as-is.
Parameters:
-
value
(ConfigLike
) –A configuration-like value. For more information see the ConfigLike type alias.
-
value_name
(str | None
, default:None
) –The value's name used for reporting errors.
Returns:
A Config
object.
Source code in xrlint\config.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
|
xrlint.config.ConfigObject
dataclass
Bases: MappingConstructible
, JsonSerializable
Configuration object.
Configuration objects are the items managed by a configuration.
You should not use the class constructor directly.
Instead, use the ConfigObject.from_value()
function.
Source code in xrlint\config.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
|
Attributes
xrlint.config.ConfigObject.name = None
class-attribute
instance-attribute
A name for the configuration object. This is used in error messages and config inspector to help identify which configuration object is being used.
xrlint.config.ConfigObject.files = None
class-attribute
instance-attribute
An array of glob patterns indicating the files that the configuration object should apply to. If not specified, the configuration object applies to all files matched by any other configuration object.
When a configuration object contains only the files property
without accompanying rules or settings, it effectively acts as
a global file filter. This means that XRLint will recognize
and process only the files matching these patterns, thereby
limiting its scope to the specified files. The inbuilt
global file filters are ["**/*.zarr", "**/*.nc"]
.
xrlint.config.ConfigObject.ignores = None
class-attribute
instance-attribute
An array of glob patterns indicating the files that the
configuration object should not apply to. If not specified,
the configuration object applies to all files matched by files
.
If ignores
is used without any other keys in the configuration
object, then the patterns act as global ignores.
xrlint.config.ConfigObject.linter_options = None
class-attribute
instance-attribute
A dictionary containing options related to the linting process.
xrlint.config.ConfigObject.opener_options = None
class-attribute
instance-attribute
A dictionary containing options that are passed to the dataset opener.
xrlint.config.ConfigObject.processor = None
class-attribute
instance-attribute
Either an object compatible with the ProcessorOp
interface or a string indicating the name of a processor inside
of a plugin (i.e., "pluginName/processorName"
).
xrlint.config.ConfigObject.plugins = None
class-attribute
instance-attribute
A dictionary containing a name-value mapping of plugin names to
plugin objects. When files
is specified, these plugins are only
available to the matching files.
xrlint.config.ConfigObject.rules = None
class-attribute
instance-attribute
A dictionary containing the configured rules.
When files
or ignores
are specified, these rule configurations
are only available to the matching files.
xrlint.config.ConfigObject.settings = None
class-attribute
instance-attribute
A dictionary containing name-value pairs of information that should be available to all rules.
xrlint.config.ConfigObject.file_filter
cached
property
The file filter specified by this configuration. May be empty.
xrlint.config.ConfigObject.empty
cached
property
True
if this configuration object does not configure anything.
Note, it could still contribute to a global file filter if its
files
and ignores
options are set.
Functions
xrlint.config.ConfigObject.get_plugin(plugin_name)
Get the plugin for given plugin name plugin_name
.
Source code in xrlint\config.py
163 164 165 166 167 168 |
|
xrlint.config.ConfigObject.get_rule(rule_id)
Get the rule for the given rule identifier rule_id
.
Parameters:
-
rule_id
(str
) –The rule identifier including plugin namespace, if any. Format
<rule-name>
(builtin rules) or<plugin-name>/<rule-name>
.
Returns:
-
Rule
–A
Rule
object.
Raises:
-
ValueError
–If either the plugin is unknown in this configuration or the rule name is unknown.
Source code in xrlint\config.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
xrlint.config.ConfigObject.get_processor_op(processor_spec)
Get the processor operation for the given
processor identifier processor_spec
.
Source code in xrlint\config.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
xrlint.config.ConfigLike = Union['Config', ConfigObjectLike, str, Sequence[ConfigObjectLike | str]]
module-attribute
Type alias for values that can represent configurations. Can be either a Config instance, or a configuration object like value, or a named plugin configuration, or a sequence of the latter two.
xrlint.config.ConfigObjectLike = Union['ConfigObject', Mapping[str, Any], None]
module-attribute
Type alias for values that can represent configuration objects.
Can be either a ConfigObject instance,
or a mapping (e.g. dict
) with properties defined by ConfigObject
,
or None
(empty configuration object).
Rule API
xrlint.rule.define_rule(name=None, version='0.0.0', type='problem', description=None, docs_url=None, schema=None, registry=None, op_class=None)
Define a rule.
This function can be used to decorate your rule operation class
definitions. When used as a decorator, the decorated operator class
will receive a meta
attribute of type RuleMeta.
In addition, the registry
if given, will be updated using name
as key and a new Rule as value.
Parameters:
-
name
(str | None
, default:None
) –Rule name, see RuleMeta.
-
version
(str
, default:'0.0.0'
) –Rule version, see RuleMeta.
-
type
(Literal['problem', 'suggestion', 'layout']
, default:'problem'
) –Rule type, see RuleMeta.
-
description
(str | None
, default:None
) –Rule description, see RuleMeta.
-
docs_url
(str | None
, default:None
) –Rule documentation URL, see RuleMeta.
-
schema
(dict[str, Any] | list[dict[str, Any]] | bool | None
, default:None
) –Rule operation arguments schema, see RuleMeta.
-
registry
(MutableMapping[str, Rule] | None
, default:None
) –Rule registry. Can be provided to register the defined rule using its
name
. -
op_class
(Type[RuleOp] | None
, default:None
) –Rule operation class. Must not be provided if this function is used as a class decorator.
Returns:
-
Callable[[Any], Type[RuleOp]] | Rule
–A decorator function, if
op_class
isNone
otherwise the value ofop_class
.
Raises:
-
TypeError
–If either
op_class
or the decorated object is not a class derived from RuleOp.
Source code in xrlint\rule.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
xrlint.rule.Rule
dataclass
Bases: Operation
A rule comprises rule metadata and a reference to the class that implements the rule's logic.
Instances of this class can be easily created and added to a plugin
by using the decorator @define_rule
of the Plugin
class.
Parameters:
-
meta
(RuleMeta
) –the rule's metadata
-
op_class
(Type[RuleOp]
) –the class that implements the rule's logic
Source code in xrlint\rule.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
Attributes
xrlint.rule.Rule.meta
instance-attribute
Rule metadata of type RuleMeta
.
xrlint.rule.Rule.op_class
instance-attribute
The class the implements the rule's validation operation.
The class must implement the RuleOp
interface.
xrlint.rule.RuleMeta
dataclass
Bases: OperationMeta
Rule metadata.
Source code in xrlint\rule.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
Attributes
xrlint.rule.RuleMeta.name
instance-attribute
Rule name. Mandatory.
xrlint.rule.RuleMeta.version = '0.0.0'
class-attribute
instance-attribute
Rule version. Defaults to 0.0.0
.
xrlint.rule.RuleMeta.description = None
class-attribute
instance-attribute
Rule description.
xrlint.rule.RuleMeta.docs_url = None
class-attribute
instance-attribute
Rule documentation URL.
xrlint.rule.RuleMeta.schema = None
class-attribute
instance-attribute
JSON Schema used to specify and validate the rule operation options.
It can take the following values:
- Use
None
(the default) to indicate that the rule operation as no options at all. - Use a schema to indicate that the rule operation
takes keyword arguments only.
The schema's type must be
"object"
. - Use a list of schemas to indicate that the rule operation takes positional arguments only. If given, the number of schemas in the list specifies the number of positional arguments that must be configured.
xrlint.rule.RuleMeta.type = 'problem'
class-attribute
instance-attribute
Rule type. Defaults to "problem"
.
The type field can have one of the following values:
"problem"
: Indicates that the rule addresses datasets that are likely to cause errors or unexpected behavior during runtime. These issues usually represent real bugs or potential runtime problems."suggestion"
: Used for rules that suggest structural improvements or enforce best practices. These issues are not necessarily bugs, but following the suggestions may lead to more readable, maintainable, or consistent datasets."layout"
: Specifies that the rule enforces consistent stylistic aspects of dataset formatting, e.g., whitespaces in names. Issues with layout rules are often automatically fixable (not supported yet).
Primarily serves to categorize the rule's purpose for the benefit of developers and tools that consume XRLint output. It doesn’t directly affect the linting logic - that part is handled by the rule’s implementation and its configured severity.
xrlint.rule.RuleOp
Bases: ABC
Define the specific rule validation operations.
Source code in xrlint\rule.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
Functions
xrlint.rule.RuleOp.validate_datatree(ctx, node)
Validate the given datatree node.
Parameters:
-
ctx
(RuleContext
) –The current rule context.
-
node
(DataTreeNode
) –The datatree node.
Raises:
-
RuleExit
–to exit rule logic and further node traversal
Source code in xrlint\rule.py
89 90 91 92 93 94 95 96 97 98 |
|
xrlint.rule.RuleOp.validate_dataset(ctx, node)
Validate the given dataset node.
Parameters:
-
ctx
(RuleContext
) –The current rule context.
-
node
(DatasetNode
) –The dataset node.
Raises:
-
RuleExit
–to exit rule logic and further node traversal
Source code in xrlint\rule.py
100 101 102 103 104 105 106 107 108 109 |
|
xrlint.rule.RuleOp.validate_variable(ctx, node)
Validate the given data array (variable) node.
Parameters:
-
ctx
(RuleContext
) –The current rule context.
-
node
(VariableNode
) –The data array (variable) node.
Raises:
-
RuleExit
–to exit rule logic and further node traversal
Source code in xrlint\rule.py
111 112 113 114 115 116 117 118 119 120 |
|
xrlint.rule.RuleOp.validate_attrs(ctx, node)
Validate the given attributes node.
Parameters:
-
ctx
(RuleContext
) –The current rule context.
-
node
(AttrsNode
) –The attributes node.
Raises:
-
RuleExit
–to exit rule logic and further node traversal
Source code in xrlint\rule.py
122 123 124 125 126 127 128 129 130 131 |
|
xrlint.rule.RuleOp.validate_attr(ctx, node)
Validate the given attribute node.
Parameters:
-
ctx
(RuleContext
) –The current rule context.
-
node
(AttrNode
) –The attribute node.
Raises:
-
RuleExit
–to exit rule logic and further node traversal
Source code in xrlint\rule.py
133 134 135 136 137 138 139 140 141 142 |
|
xrlint.rule.RuleContext
Bases: ABC
The context passed to a RuleOp instance.
Instances of this interface are passed to the validation
methods of your RuleOp
.
There should be no reason to create instances of this class
yourself.
Source code in xrlint\rule.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
Attributes
xrlint.rule.RuleContext.file_path
abstractmethod
property
The current dataset's file path.
xrlint.rule.RuleContext.settings
abstractmethod
property
Applicable subset of settings from configuration settings
.
xrlint.rule.RuleContext.dataset
abstractmethod
property
The current dataset.
xrlint.rule.RuleContext.access_latency
abstractmethod
property
The time in seconds that it took for opening the dataset.
None
if the dataset has not been opened from file_path
.
Functions
xrlint.rule.RuleContext.report(message, *, fatal=None, suggestions=None)
abstractmethod
Report an issue.
Parameters:
-
message
(str
) –mandatory message text
-
fatal
(bool | None
, default:None
) –True, if a fatal error is reported.
-
suggestions
(list[Suggestion | str] | None
, default:None
) –A list of suggestions for the user on how to fix the reported issue. Items may be of type
Suggestion
orstr
.
Source code in xrlint\rule.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
xrlint.rule.RuleExit
Bases: Exception
The RuleExit
is an exception that can be raised to
immediately cancel dataset node validation with the current rule.
Raise it from any of your RuleOp
method implementations if further
node traversal doesn't make sense. Typical usage:
if something_is_not_ok:
ctx.report("Something is not ok.")
raise RuleExit
Source code in xrlint\rule.py
71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
Dataset Node API
xrlint.node.Node
dataclass
Bases: ABC
Abstract base class for nodes passed to the methods of a rule operation xrlint.rule.RuleOp.
Source code in xrlint\node.py
12 13 14 15 16 17 18 19 20 21 |
|
Attributes
xrlint.node.Node.path
instance-attribute
Node path. So users find where in the tree the issue occurred.
xrlint.node.Node.parent
instance-attribute
Node parent. None
for root nodes.
xrlint.node.XarrayNode
dataclass
Bases: Node
Base class for xr.Dataset
nodes.
Source code in xrlint\node.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Functions
xrlint.node.XarrayNode.in_coords()
Return True
if this node is in xr.Dataset.coords
.
Source code in xrlint\node.py
28 29 30 |
|
xrlint.node.XarrayNode.in_data_vars()
Return True
if this node is a xr.Dataset.data_vars
.
Source code in xrlint\node.py
32 33 34 |
|
xrlint.node.XarrayNode.in_root()
Return True
if this node is a direct child of the dataset.
Source code in xrlint\node.py
36 37 38 |
|
xrlint.node.DataTreeNode
dataclass
Bases: XarrayNode
DataTree node.
Source code in xrlint\node.py
41 42 43 44 45 46 47 48 49 |
|
Attributes
xrlint.node.DataTreeNode.name
instance-attribute
The name of the datatree.
xrlint.node.DataTreeNode.datatree
instance-attribute
The xarray.DataTree
instance.
xrlint.node.DatasetNode
dataclass
Bases: XarrayNode
Dataset node.
Source code in xrlint\node.py
52 53 54 55 56 57 58 59 60 |
|
Attributes
xrlint.node.DatasetNode.name
instance-attribute
The name of the dataset.
xrlint.node.DatasetNode.dataset
instance-attribute
The xarray.Dataset
instance.
xrlint.node.VariableNode
dataclass
Bases: XarrayNode
Variable node.
Could be a coordinate or data variable.
If you need to distinguish, you can use expression
node.name in ctx.dataset.coords
.
Source code in xrlint\node.py
63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
Attributes
xrlint.node.VariableNode.name
instance-attribute
The name of the variable.
xrlint.node.VariableNode.array
instance-attribute
The xarray.DataArray
instance.
xrlint.node.AttrsNode
dataclass
Bases: XarrayNode
Attributes node.
Source code in xrlint\node.py
78 79 80 81 82 83 |
|
Attributes
xrlint.node.AttrsNode.attrs
instance-attribute
Attributes dictionary.
xrlint.node.AttrNode
dataclass
Bases: XarrayNode
Attribute node.
Source code in xrlint\node.py
86 87 88 89 90 91 92 93 94 |
|
Attributes
xrlint.node.AttrNode.name
instance-attribute
Attribute name.
xrlint.node.AttrNode.value
instance-attribute
Attribute value.
Processor API
xrlint.processor.define_processor(name=None, version='0.0.0', registry=None, op_class=None)
Define a processor.
This function can be used to decorate your processor operation class
definitions. When used as a decorator, the decorated operator class
will receive a meta
attribute of type
ProcessorMeta.
In addition, the registry
if given, will be updated using name
as key and a new Processor as value.
Parameters:
-
name
(str | None
, default:None
) –Processor name, see ProcessorMeta.
-
version
(str
, default:'0.0.0'
) –Processor version, see ProcessorMeta.
-
registry
(dict[str, Processor] | None
, default:None
) –Processor registry. Can be provided to register the defined processor using its
name
. -
op_class
(Type[ProcessorOp] | None
, default:None
) –Processor operation class. Must be
None
if this function is used as a class decorator.
Returns:
-
Callable[[Any], Type[ProcessorOp]] | Processor
–A decorator function, if
op_class
isNone
otherwise the value ofop_class
.
Raises:
-
TypeError
–If either
op_class
or the decorated object is not a a class derived from ProcessorOp.
Source code in xrlint\processor.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
xrlint.processor.Processor
dataclass
Bases: Operation
Processors tell XRLint how to process files other than standard xarray datasets.
Source code in xrlint\processor.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
Attributes
xrlint.processor.Processor.meta
instance-attribute
Information about the processor.
xrlint.processor.Processor.op_class
instance-attribute
A class that implements the processor operations.
xrlint.processor.ProcessorMeta
dataclass
Bases: OperationMeta
Processor metadata.
Source code in xrlint\processor.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
Attributes
xrlint.processor.ProcessorMeta.name
instance-attribute
Processor name.
xrlint.processor.ProcessorMeta.version = '0.0.0'
class-attribute
instance-attribute
Processor version.
xrlint.processor.ProcessorMeta.ref = None
class-attribute
instance-attribute
Processor reference.
Specifies the location from where the processor can be
dynamically imported.
Must have the form "
xrlint.processor.ProcessorOp
Bases: ABC
Implements the processor operations.
Source code in xrlint\processor.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
Functions
xrlint.processor.ProcessorOp.preprocess(file_path, opener_options)
abstractmethod
Pre-process a dataset given by its file_path
and opener_options
.
In this method you use the file_path
to read zero, one, or more
datasets to lint.
Parameters:
-
file_path
(str
) –A file path
-
opener_options
(dict[str, Any]
) –The configuration's
opener_options
.
Returns:
-
list[tuple[Dataset | DataTree, str]]
–A list of (dataset or datatree, file_path) pairs
Source code in xrlint\processor.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
xrlint.processor.ProcessorOp.postprocess(messages, file_path)
abstractmethod
Post-process the outputs of each dataset from preprocess()
.
Parameters:
-
messages
(list[list[Message]]
) –contains two-dimensional array of ´Message´ objects where each top-level array item contains array of lint messages related to the dataset that was returned in array from
preprocess()
method -
file_path
(str
) –The corresponding file path
Returns:
-
list[Message]
–A one-dimensional array (list) of the messages you want to keep
Source code in xrlint\processor.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
Result API
xrlint.result.Result
dataclass
Bases: JsonSerializable
The aggregated information of linting a dataset.
Source code in xrlint\result.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
Attributes
xrlint.result.Result.file_path
instance-attribute
The absolute path to the file of this result.
This is the string "file_path
option to the
xrlint.lint_dataset()
method).
xrlint.result.Result.config_object = None
class-attribute
instance-attribute
The configuration object that produced this result
together with file_path
.
xrlint.result.Result.messages = field(default_factory=list)
class-attribute
instance-attribute
The array of message objects.
xrlint.result.Result.warning_count
cached
property
The number of warnings. This includes fixable warnings.
xrlint.result.Result.error_count
cached
property
The number of errors. This includes fixable errors and fatal errors.
xrlint.result.Result.fatal_error_count
cached
property
The number of fatal errors.
xrlint.result.Message
dataclass
Bases: JsonSerializable
Source code in xrlint\result.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
Attributes
xrlint.result.Message.message
instance-attribute
The error message.
xrlint.result.Message.node_path = None
class-attribute
instance-attribute
Node path within the dataset. This property is None if the message does not apply to a certain dataset node.
xrlint.result.Message.rule_id = None
class-attribute
instance-attribute
The rule name that generated this lint message. If this message is generated by the xrlint core rather than rules, this is None.
xrlint.result.Message.severity = None
class-attribute
instance-attribute
The severity of this message.
1
means warning and 2
means error.
xrlint.result.Message.fatal = None
class-attribute
instance-attribute
True if this is a fatal error unrelated to a rule, like a parsing error.
xrlint.result.Message.fix = None
class-attribute
instance-attribute
The EditInfo object of auto-fix. This property is None if this message is not fixable.
Not used yet.
xrlint.result.Message.suggestions = None
class-attribute
instance-attribute
The list of suggestions. Each suggestion is the pair of a description and an EditInfo object to fix the dataset. API users such as editor integrations can choose one of them to fix the problem of this message. This property is None if this message does not have any suggestions.
xrlint.result.Suggestion
dataclass
Bases: ValueConstructible
, JsonSerializable
Source code in xrlint\result.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
Attributes
xrlint.result.Suggestion.desc
instance-attribute
Description of the suggestion.
xrlint.result.Suggestion.data = None
class-attribute
instance-attribute
Data that can be referenced in the description.
xrlint.result.Suggestion.fix = None
class-attribute
instance-attribute
Not used yet.
Testing API
xrlint.testing.RuleTester
Utility that helps testing rules.
Parameters:
-
config
(ConfigLike
, default:None
) –Optional configuration-like value. For more information see the ConfigLike type alias.
-
**config_props
(Any
, default:{}
) –Individual configuration object properties. For more information refer to the properties of a ConfigObject.
Source code in xrlint\testing.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
Functions
xrlint.testing.RuleTester.run(rule_name, rule_op_class, *, valid=None, invalid=None)
Run the given tests in valid
and invalid
against the given rule.
Parameters:
-
rule_name
(str
) –the rule's name
-
rule_op_class
(Type[RuleOp]
) –a class derived from
RuleOp
-
valid
(list[RuleTest] | None
, default:None
) –list of tests that expect no reported problems
-
invalid
(list[RuleTest] | None
, default:None
) –list of tests that expect reported problems
Raises:
-
AssertionError
–if one of the checks fails
Source code in xrlint\testing.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
xrlint.testing.RuleTester.define_test(rule_name, rule_op_class, *, valid=None, invalid=None, config=None, **config_props)
classmethod
Create a unittest.TestCase
class for the given rule and tests.
The returned class is derived from unittest.TestCase
and contains a test method for each of the tests in
valid
and invalid
.
Parameters:
-
rule_name
(str
) –the rule's name
-
rule_op_class
(Type[RuleOp]
) –the class derived from
RuleOp
-
valid
(list[RuleTest] | None
, default:None
) –list of tests that expect no reported problems
-
invalid
(list[RuleTest] | None
, default:None
) –list of tests that expect reported problems
-
config
(ConfigLike
, default:None
) –Optional configuration-like value. For more information see the ConfigLike type alias.
-
**config_props
(Any
, default:{}
) –Individual configuration object properties. For more information refer to the properties of a ConfigObject.
Returns:
-
Type[TestCase]
–A new class derived from
unittest.TestCase
.
Source code in xrlint\testing.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
xrlint.testing.RuleTest
dataclass
A rule test case.
Source code in xrlint\testing.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
Attributes
xrlint.testing.RuleTest.dataset
instance-attribute
The dataset to verify.
xrlint.testing.RuleTest.name = None
class-attribute
instance-attribute
A name that helps identifying the test case.
xrlint.testing.RuleTest.args = None
class-attribute
instance-attribute
Optional positional arguments passed to the rule operation's constructor.
xrlint.testing.RuleTest.kwargs = None
class-attribute
instance-attribute
Optional keyword arguments passed to the rule operation's constructor.
xrlint.testing.RuleTest.expected = None
class-attribute
instance-attribute
Expected messages. Either a list of expected message objects or the number of expected messages. Must not be provided for valid checks and must be provided for invalid checks.