Repository: ioos/compliance-checker
Pull request #1206 (2025-06-05 09:34:42)

remove nargs for criteria in argparse
Rather insignificant bugfix: I accidentally let the compliance-checker run with `-c` without setting an actual criteria-level, which resulted in a crash with a rather ugly exception instead of pointing out the actual usage.

I am not sure whether there is some reasoning I don't understand, but having `nargs="?"` set for `criteria` results in `criteria=None`, ignoring the default, when starting the compliance checker like I did. I couldn't find any good reason why `nargs="?"` should be used here, so I removed it.


Patch:
diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml
index 84f0af4..0a228f5 100644
--- a/.github/workflows/codecov.yml
+++ b/.github/workflows/codecov.yml
@@ -14,7 +14,7 @@ jobs:
         persist-credentials: false
 
     - name: Setup Micromamba
-      uses: mamba-org/setup-micromamba@0dea6379afdaffa5d528b3d1dabc45da37f443fc # v2.0.4
+      uses: mamba-org/setup-micromamba@b09ef9b599704322748535812ca03efb2625677b # v2.0.5
       with:
         environment-name: TEST
         init-shell: bash
diff --git a/.github/workflows/deploy-docs.yml b/.github/workflows/deploy-docs.yml
index c838441..d8a5ab7 100644
--- a/.github/workflows/deploy-docs.yml
+++ b/.github/workflows/deploy-docs.yml
@@ -19,7 +19,7 @@ jobs:
         persist-credentials: false
 
     - name: Setup Micromamba
-      uses: mamba-org/setup-micromamba@0dea6379afdaffa5d528b3d1dabc45da37f443fc # v2.0.4
+      uses: mamba-org/setup-micromamba@b09ef9b599704322748535812ca03efb2625677b # v2.0.5
       with:
         environment-name: TEST
         init-shell: bash
diff --git a/cchecker.py b/cchecker.py
index 1327a3b..a738ea7 100755
--- a/cchecker.py
+++ b/cchecker.py
@@ -72,7 +72,6 @@ def main():
             "Define the criteria for the checks. "
             "Either Strict, Normal, or Lenient.  Defaults to Normal."
         ),
-        nargs="?",
         default="normal",
         choices=["lenient", "normal", "strict"],
     )
diff --git a/compliance_checker/cf/cf_1_6.py b/compliance_checker/cf/cf_1_6.py
index 885ff44..c2c617f 100644
--- a/compliance_checker/cf/cf_1_6.py
+++ b/compliance_checker/cf/cf_1_6.py
@@ -1185,7 +1185,10 @@ class CF1_6Check(CFNCCheck):
                 # IMPLEMENTATION CONFORMANCE 3.5 RECOMMENDED 1/1
                 # If shapes aren't equal, we can't do proper elementwise
                 # comparison
-                if vals_arr.size != masks_arr.size:
+                if vals_arr.size != masks_arr.size or not (
+                    np.issubdtype(vals_arr.dtype, np.integer)
+                    and np.issubdtype(masks_arr.dtype, np.integer)
+                ):
                     allv = False
                 else:
                     allv = np.all(vals_arr & masks_arr == vals_arr)
@@ -1235,9 +1238,14 @@ class CF1_6Check(CFNCCheck):
 
         # IMPLEMENTATION CONFORMANCE 3.5 REQUIRED 1/8
         # the data type for flag_values should be the same as the variable
+        flag_values_type = (
+            flag_values.dtype.type
+            if hasattr(flag_values, "dtype")
+            else type(flag_values)
+        )
         valid_values.assert_true(
-            variable.dtype.type == flag_values.dtype.type,
-            f"flag_values ({flag_values.dtype.type}) must be the same data type as {name} ({variable.dtype.type})"
+            variable.dtype.type == flag_values_type,
+            f"flag_values ({flag_values_type}) must be the same data type as {name} ({variable.dtype.type})"
             "",
         )
 
@@ -1272,9 +1280,12 @@ class CF1_6Check(CFNCCheck):
 
         valid_masks = TestCtx(BaseCheck.HIGH, self.section_titles["3.5"])
 
+        flag_masks_type = (
+            flag_masks.dtype.type if hasattr(flag_masks, "dtype") else type(flag_masks)
+        )
         valid_masks.assert_true(
-            variable.dtype.type == flag_masks.dtype.type,
-            f"flag_masks ({flag_masks.dtype.type}) must be the same data type as {name} ({variable.dtype.type})"
+            variable.dtype.type == flag_masks_type,
+            f"flag_masks ({flag_masks_type}) must be the same data type as {name} ({variable.dtype.type})"
             "",
         )
 


Test patch:
diff --git a/.github/workflows/cc-plugin-cc6-test.yml b/.github/workflows/cc-plugin-cc6-test.yml
index 451b8db..44dfed5 100644
--- a/.github/workflows/cc-plugin-cc6-test.yml
+++ b/.github/workflows/cc-plugin-cc6-test.yml
@@ -17,7 +17,7 @@ jobs:
         persist-credentials: false
 
     - name: Setup Micromamba
-      uses: mamba-org/setup-micromamba@0dea6379afdaffa5d528b3d1dabc45da37f443fc # v2.0.4
+      uses: mamba-org/setup-micromamba@b09ef9b599704322748535812ca03efb2625677b # v2.0.5
       with:
         environment-name: TEST
         init-shell: bash
diff --git a/.github/workflows/cc-plugin-glider-test.yml b/.github/workflows/cc-plugin-glider-test.yml
index cb58836..4e9df0a 100644
--- a/.github/workflows/cc-plugin-glider-test.yml
+++ b/.github/workflows/cc-plugin-glider-test.yml
@@ -17,7 +17,7 @@ jobs:
         persist-credentials: false
 
     - name: Setup Micromamba
-      uses: mamba-org/setup-micromamba@0dea6379afdaffa5d528b3d1dabc45da37f443fc # v2.0.4
+      uses: mamba-org/setup-micromamba@b09ef9b599704322748535812ca03efb2625677b # v2.0.5
       with:
         environment-name: TEST
         init-shell: bash
diff --git a/.github/workflows/cc-plugin-ncei-test.yml b/.github/workflows/cc-plugin-ncei-test.yml
index 64043a8..105451d 100644
--- a/.github/workflows/cc-plugin-ncei-test.yml
+++ b/.github/workflows/cc-plugin-ncei-test.yml
@@ -17,7 +17,7 @@ jobs:
         persist-credentials: false
 
     - name: Setup Micromamba
-      uses: mamba-org/setup-micromamba@0dea6379afdaffa5d528b3d1dabc45da37f443fc # v2.0.4
+      uses: mamba-org/setup-micromamba@b09ef9b599704322748535812ca03efb2625677b # v2.0.5
       with:
         environment-name: TEST
         init-shell: bash
diff --git a/.github/workflows/cc-plugin-og-test.yml b/.github/workflows/cc-plugin-og-test.yml
index dcac6c8..468a202 100644
--- a/.github/workflows/cc-plugin-og-test.yml
+++ b/.github/workflows/cc-plugin-og-test.yml
@@ -17,7 +17,7 @@ jobs:
         persist-credentials: false
 
     - name: Setup Micromamba
-      uses: mamba-org/setup-micromamba@0dea6379afdaffa5d528b3d1dabc45da37f443fc # v2.0.4
+      uses: mamba-org/setup-micromamba@b09ef9b599704322748535812ca03efb2625677b # v2.0.5
       with:
         environment-name: TEST
         init-shell: bash
diff --git a/.github/workflows/cc-plugin-sgrid-test.yml b/.github/workflows/cc-plugin-sgrid-test.yml
index fb6a56d..24d0be9 100644
--- a/.github/workflows/cc-plugin-sgrid-test.yml
+++ b/.github/workflows/cc-plugin-sgrid-test.yml
@@ -17,7 +17,7 @@ jobs:
         persist-credentials: false
 
     - name: Setup Micromamba
-      uses: mamba-org/setup-micromamba@0dea6379afdaffa5d528b3d1dabc45da37f443fc # v2.0.4
+      uses: mamba-org/setup-micromamba@b09ef9b599704322748535812ca03efb2625677b # v2.0.5
       with:
         environment-name: TEST
         init-shell: bash
diff --git a/.github/workflows/cc-plugin-ugrid-test.yml b/.github/workflows/cc-plugin-ugrid-test.yml
index c4d5b48..bab91d3 100644
--- a/.github/workflows/cc-plugin-ugrid-test.yml
+++ b/.github/workflows/cc-plugin-ugrid-test.yml
@@ -17,7 +17,7 @@ jobs:
         persist-credentials: false
 
     - name: Setup Micromamba
-      uses: mamba-org/setup-micromamba@0dea6379afdaffa5d528b3d1dabc45da37f443fc # v2.0.4
+      uses: mamba-org/setup-micromamba@b09ef9b599704322748535812ca03efb2625677b # v2.0.5
       with:
         environment-name: TEST
         init-shell: bash
diff --git a/.github/workflows/default-tests.yml b/.github/workflows/default-tests.yml
index 6d5759c..7aed1b2 100644
--- a/.github/workflows/default-tests.yml
+++ b/.github/workflows/default-tests.yml
@@ -22,7 +22,7 @@ jobs:
         persist-credentials: false
 
     - name: Setup Micromamba ${{ matrix.python-version }}
-      uses: mamba-org/setup-micromamba@0dea6379afdaffa5d528b3d1dabc45da37f443fc # v2.0.4
+      uses: mamba-org/setup-micromamba@b09ef9b599704322748535812ca03efb2625677b # v2.0.5
       with:
         environment-name: TEST
         init-shell: bash
diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml
index 5338699..e4086a7 100644
--- a/.github/workflows/integration-tests.yml
+++ b/.github/workflows/integration-tests.yml
@@ -14,7 +14,7 @@ jobs:
         persist-credentials: false
 
     - name: Setup Micromamba
-      uses: mamba-org/setup-micromamba@0dea6379afdaffa5d528b3d1dabc45da37f443fc # v2.0.4
+      uses: mamba-org/setup-micromamba@b09ef9b599704322748535812ca03efb2625677b # v2.0.5
       with:
         environment-name: TEST
         init-shell: bash
diff --git a/compliance_checker/tests/test_cf.py b/compliance_checker/tests/test_cf.py
index 39ab418..3146974 100644
--- a/compliance_checker/tests/test_cf.py
+++ b/compliance_checker/tests/test_cf.py
@@ -866,6 +866,9 @@ class TestCF1_6(BaseTestCase):
         # Test with single element.  Will fail, but should not throw exception.
         dataset.variables["conductivity_qc"].flag_values = np.array([1], dtype=np.int8)
         results = self.cf.check_flags(dataset)
+        # Test with wrong type.  Will fail, but should not throw exception.
+        dataset.variables["conductivity_qc"].flag_values = "[1 2]"
+        results = self.cf.check_flags(dataset)
 
     def test_check_flag_masks(self):
         dataset = self.load_dataset(STATIC_FILES["ghrsst"])
@@ -892,6 +895,14 @@ class TestCF1_6(BaseTestCase):
             "flag_masks for variable flags must not contain zero as an "
             "element" in messages
         )
+        # test wrong type for flag_masks
+        flags_var.flag_masks = "[0 1]"
+        results = self.cf.check_flags(dataset)
+        score, out_of, messages = get_results(results)
+        assert (
+            "flag_masks (<class 'str'>) must be the same data type as "
+            "flags (<class 'numpy.float64'>)" in messages
+        )
         # IMPLEMENTATION 3.5 REQUIRED 1/1
         flags_var.flag_masks = np.array([1], dtype="i2")
         flags_var.flag_values = np.array([2], dtype="i2")