mozilla / metric-hub

Central hub for metric definitions that are considered the source of truth
https://mozilla.github.io/metric-hub
Mozilla Public License 2.0
13 stars 13 forks source link

Types can differ from annotations at runtime #545

Open danielkberry opened 2 months ago

danielkberry commented 2 months ago

Attrs' type annotations do not validate or enforce types. This can result in certain parameters such as DataSource.client_id_column being typed as as str, but being None at runtime. This issue proposes enforcing runtime types using attrs' validator & converter methods. For example:

class DataSource: 
  ...
  client_id_column = attr.ib(
      default="client_id" # if nothing is passed to DataSource.__init__
      type=str,
      validator=[attr.validators.instance_of(str), attr.validators.min_len(1)],
      converter=attr.converters.default_if_none("client_id") 
      # ^ handles case where `client_id_column = None` passed to DataSource.__init__
  )

This will allow callers to instantiate a data source object by passing nothing, a None, or a str as the client_id_column parameter.