NCAR / ccpp-framework

Common Community Physics Package (CCPP)
http://www.dtcenter.org/community-code/common-community-physics-package-ccpp/
Other
26 stars 63 forks source link

feature/capgen: bug in `standard_name_to_long_name` (in `var_props.py`: 'VariableProperty' object is not subscriptable #505

Closed climbfuji closed 9 months ago

climbfuji commented 9 months ago

Description

Looks like there is a bug in routine standard_name_to_long_name in var_props.py. When the standard name exists in prop_dict, the following exception is thrown:

> /Users/heinzell/work/ccpp-framework/ccpp-framework-debug-20231012/scripts/ccpp_capgen.py --host-files test_host_data.meta,test_host_mod.meta,test_host.meta --scheme-files temp_scheme_files.txt,ddt_suite_files.txt --suites ddt_suite.xml,temp_suite.xml --host-name test_host --output-root /Users/heinzell/work/ccpp-framework/ccpp-framework-debug-20231012/test/ct_build/ccpp
Traceback (most recent call last):
  File "/Users/heinzell/work/ccpp-framework/ccpp-framework-debug-20231012/scripts/ccpp_capgen.py", line 18, in <module>
    from ccpp_database_obj import CCPPDatabaseObj
  File "/Users/heinzell/work/ccpp-framework/ccpp-framework-debug-20231012/scripts/ccpp_database_obj.py", line 8, in <module>
    from host_model import HostModel
  File "/Users/heinzell/work/ccpp-framework/ccpp-framework-debug-20231012/scripts/host_model.py", line 8, in <module>
    from metavar import VarDictionary
  File "/Users/heinzell/work/ccpp-framework/ccpp-framework-debug-20231012/scripts/metavar.py", line 2019, in <module>
    variables=[ccpp_standard_var('ccpp_constant_one', 'module',
  File "/Users/heinzell/work/ccpp-framework/ccpp-framework-debug-20231012/scripts/metavar.py", line 1158, in ccpp_standard_var
    newvar = Var(vdict, psource, run_env)
  File "/Users/heinzell/work/ccpp-framework/ccpp-framework-debug-20231012/scripts/metavar.py", line 275, in __init__
    print(f"propdict: {prop}, {mstr_propdict[prop].get_default_val(mstr_propdict)}") #", mstr_propdict[prop].get_default_val()}")
  File "/Users/heinzell/work/ccpp-framework/ccpp-framework-debug-20231012/scripts/var_props.py", line 646, in get_default_val
    return self._default_fn(prop_dict, context)
  File "/Users/heinzell/work/ccpp-framework/ccpp-framework-debug-20231012/scripts/var_props.py", line 159, in standard_name_to_long_name
    long_name = standard_name[0].upper() + re.sub("_", " ",
TypeError: 'VariableProperty' object is not subscriptable

Steps to Reproduce

You can trigger this by adding some debugging code in metavar.py and then run the capgen command shown in the above box (part of the capgen unit tests):

diff --git a/scripts/metavar.py b/scripts/metavar.py
index dc088fd..05c378a 100755
--- a/scripts/metavar.py
+++ b/scripts/metavar.py
@@ -270,6 +270,9 @@ class Var:
             self.__required_props = Var.__required_spec_props
 # XXgoldyXX: v don't fill in default properties?
             mstr_propdict = Var.__spec_propdict
+            for prop in mstr_propdict.keys():
+                if mstr_propdict[prop].optional:
+                    print(f"propdict: {prop}, {mstr_propdict[prop].get_default_val(mstr_propdict)}") #", mstr_propdict[prop].get_default_val()}")

Additional Context

The code in standard_name_to_long_name needs to access the value of the variable property standard_name, not the VariableProperty object. Unfortunately, VariableProperty is a complicated construct and it's not immediately clear to me how to do that.

Output

See above

climbfuji commented 9 months ago

This turned out to be a dumb user error, not a bug in the script. Closing (but I'll open another one that led to this fake problem in a minute)