CDCgov / forecasttools-py

A Python package for common pre- and post-processing operations done by CFA Predict for short term forecasting, nowcasting, and scenario modeling.
Apache License 2.0
4 stars 0 forks source link

Duck-Typing In Certain Functions #41

Open AFg6K7h4fhy2 opened 1 week ago

AFg6K7h4fhy2 commented 1 week ago

While Python does not require strict typing and while strict typing is useful in certain instances, there are some instances within forecasttool-py where the author's strict-typing approaches are overly pedantic. As a replacement for these situations, such as in add_time_coords_to_idata_dimension,

inputs = [
        (idata, az.InferenceData, "idata"),
        (group, str, "group"),
        (variable, str, "variable"),
        (dimension, str, "dimension"),
        (time_step, timedelta, "time_step"),
    ]
for value, expected_type, param_name in inputs:
    forecasttools.validate_input_type(
        value=value, expected_type=expected_type, param_name=param_name
    )

DHM suggests adhering to Duck typing.

AFg6K7h4fhy2 commented 4 days ago

From DHM comments in #35 :

        (groups, (str, list), "groups"),
        (variables, (str, list), "variables"),
        (dimensions, (str, list), "dimensions"),

Again, I think this can be duck-typed, especially as you're going to call ensure_listlike on them

Why error if the user provides a numpy.array of strings, for instance?

and

    forecasttools.validate_iter_has_expected_types(groups, str, "groups")
    forecasttools.validate_iter_has_expected_types(variables, str, "variables")
    forecasttools.validate_iter_has_expected_types(
        dimensions, str, "dimensions"
    )

Again, this feels like unnecessarily strong typing for a duck-y language, but I am less opposed here than I am above.