-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model config types are incorrect #1182
Comments
It seems like pyright should be picking up at least some of these, so that should be checked as well. |
_RawConfig = TypedDict("_RawConfig", {'options': Dict[str, _ConfigOption]}) pyright doesn't pick up this issue because what we're basically doing is saying "give me a dict from this YAML string and you should believe it's this type". We don't have any schema validation for the config metadata. |
pyright can't pick up the wrong type on |
For p = ops.Port('tcp', self.config["port"]) And pyright will complain, because |
Excellent report and Pyright investigation -- thanks Tony. |
Corrects types related to the model configuration: * `LazyMapping` may have Boolean, integer, or float values, as well as strings * `_ConfigOption` may have a `secret` value for `type - also move this to [testing.py](ops/testing.py) since that's the only place it's used and it's private * `_ModelBackend.config_get` returns a dict with Boolean, integer, float, or string values The PR also sets a missing return type so that pyright would detect the `config_get` issue. There are no tests or pyright changes for the `_ConfigOption` change: we don't generally rely on pyright rather than unit tests for ensuring the typing is correct, but that doesn't seem feasible here. Let me know if you think we do need something added here to prevent regression. There are no tests or pyright changes for the `LazyMapping` change, other than making sure we do have [test_model.py](tests/test_model.py) exercise each of the config types. I don't think it's possible to have pyright detect this issue - e.g. you can get it to show an error if you try to use an integer config value as an integer (`self.config["int-opt"] + 1`), but that's still the case after the fix, since it could be a string (float, Boolean, secret) option. The test could have `typing.assert_type` calls but (a) that's only in Python 3.11, and (b) I'm not convinced we really want to do that. Let me know if you think we do need something added here to prevent regression. Fixes #1182
After operator framework fixes the incorrect types in `model.config`, it introduces some issues in our existing codebases since we used to assume them to have type of `str` which is not correct in the first place. [1] canonical/operator#1182
Model.config
is typed to return aModel.ConfigData
, which is a subclass ofModel.LazyMapping
, which is an ABC ofMapping[str, str]
. However, the config is actuallyMapping[str, int|float|str|bool]
.RelationDataContent
is also aLazyMapping
subclass, and there the data is always[str, str]
.The data is loaded into the lazy mapping via
_ModelBackend.config_get
, which is typed to return adict[str, Model._ConfigOption]
.Model._ConfigOption
has atype
literal string, a description string, and a default value (str, int, float, bool) - but that's the format for a dict loaded from the config/charmcraft YAML file. What we get fromconfig-get
, which is how the data is loaded, is:Problems:
LazyMapping
should be more loosely typed, e.g.Mapping[str, int|float|str|bool]
_ConfigOption.type
can besecret
_ModelBackend.config_get
should return adict[str, int|float|str|bool]
The text was updated successfully, but these errors were encountered: