Repository: ioos/compliance-checker

Issue:
3.5 Flags and flag attributes - flag_values attr does not have same type as var
Ran the new compliance-checker-web on the file referenced in #171 and got a number of "Corrective Actions" with the following text:

```
3.5 Flags and flag attributes   3   flag_values attr does not have same type as var (fv: int8, v: int16)
```

This is partially useful as it tells me what types were wrong (cryptically, though), but it does not tell me what variable.  Especially as there's a ton of these messages, all the same.



Discussion:
Actually, this may be an issue with compliance-checker-web's output formatting, @lukecampbell ?

@daf I've come across this as well and thought it should be fixed. I think it's a result of the new naming system for CF checks. For a lot of them the variable name (and sometimes useful - IMHO - third-level check names) were removed, leading to many check results being combined into one. Some other checks have the same issue:
- 2.4 Dimension order
- 3.1 Variables contain valid CF Units
- 3.3 Standard Names

Sure, sometimes the variable name is included in the reasoning message, but that's not very easy to read when there's a whole bunch of them with the same error. e.g.

```
3.1 Variables contain valid CF Units   :3:     0/ 6 : unknown units type
                                                      (Degrees clockwise from
                                                      true North) for CDIR,
                                                      unknown units type
                                                      (Decibels) for ABSI1,
                                                      unknown units type
                                                      (Decibels) for ABSI2,
                                                      unknown units type
                                                      (Decibels) for ABSI3,
                                                      ... etc ...
```

"Someone needs to revisit the 3.5 test and make sure the variable is being passed " - Luke

@daf file referenced in link is currently 404.  Can make a minimal example to reproduce but would prefer using the original file to reproduce.


Resolving patch:
diff --git a/compliance_checker/cf/cf.py b/compliance_checker/cf/cf.py
--- a/compliance_checker/cf/cf.py
+++ b/compliance_checker/cf/cf.py
@@ -779,7 +779,9 @@ def check_flags(self, ds):
             if flag_values is not None:
                 # flag_values must be a list (array), not just a single value
                 result_name = '§3.5 Flags and flag attributes'
-                fvlist = Result(BaseCheck.HIGH, isinstance(flag_values, np.ndarray), result_name)
+                fvlist = Result(BaseCheck.HIGH, isinstance(flag_values,
+                                                           np.ndarray),
+                                result_name)
                 if not fvlist.value:
                     fvlist.msgs = ['flag_values must be a list']
                     # convert to an array so the remaining checks can be applied
@@ -787,9 +789,13 @@ def check_flags(self, ds):
                 ret_val.append(fvlist)
 
                 # 1) flags_values attribute must have same type as variable to which it is attached
-                fvr = Result(BaseCheck.HIGH, flag_values.dtype == v.dtype, name='§3.5 Flags and flag attributes')
+                fvr = Result(BaseCheck.HIGH, flag_values.dtype == v.dtype,
+                             name='§3.5 Flags and flag attributes')
                 if not fvr.value:
-                    fvr.msgs = ['flag_values attr does not have same type as var (fv: %s, v: %s)' % (flag_values.dtype, v.dtype)]
+                    fvr.msgs = [("'flag_values' attribute for variable '%s'" +\
+                                " does not have same type " +\
+                                "(fv: %s, v: %s)")
+                                % (v.name, flag_values.dtype, v.dtype)]
 
                 ret_val.append(fvr)
 


Test patch:
diff --git a/compliance_checker/tests/test_cf.py b/compliance_checker/tests/test_cf.py
--- a/compliance_checker/tests/test_cf.py
+++ b/compliance_checker/tests/test_cf.py
@@ -285,8 +285,10 @@ def test_check_flags(self):
         self.assertEqual(scored, 46)
         self.assertEqual(out_of, 59)
         self.assertEqual(messages.count('flag_values must be a list'), 6)
-        self.assertEqual(messages.count('flag_values attr does not have same type as var (fv: int8, v: int16)'), 6)
-        self.assertEqual(messages.count('flag_values attr does not have same type as var (fv: <U1, v: int16)'), 1)
+        m_str = r"'flag_values' attribute for variable '\w+' does not have same type \(fv: [<>]?\w+, v: [<>]?\w+\)"
+        # make sure flag_values attribute where not equal to variable type
+        # has the proper message
+        self.assertEqual(sum(bool(re.match(m_str, msg)) for msg in messages), 7)
 
     def test_check_bad_units(self):