Skip to content

Commit

Permalink
Merge branch 'main' into update-doc-deps-2024-04
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer authored Apr 12, 2024
2 parents 7281478 + cc5818b commit 354a17b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
7 changes: 4 additions & 3 deletions ops/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,10 @@ def restore(self, snapshot: Dict[str, Any]):
(s for s in storages if s.index == storage_index),
None) # type: ignore
if self.storage is None:
msg = 'failed loading storage (name={!r}, index={!r}) from snapshot' \
.format(storage_name, storage_index)
raise RuntimeError(msg)
raise RuntimeError(
f'failed loading storage (name={storage_name!r}, '
f'index={storage_index!r}) from snapshot'
)
if storage_location is None:
raise RuntimeError(
'failed loading storage location from snapshot.'
Expand Down
17 changes: 8 additions & 9 deletions ops/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,9 @@ class BoundEvent:
"""Event bound to an Object."""

def __repr__(self):
return '<BoundEvent {} bound to {}.{} at {}>'.format(
self.event_type.__name__,
type(self.emitter).__name__,
self.event_kind,
hex(id(self)),
return (
f'<BoundEvent {self.event_type.__name__} bound to '
f'{type(self.emitter).__name__}.{self.event_kind} at {hex(id(self))}>'
)

def __init__(self, emitter: 'Object',
Expand Down Expand Up @@ -1106,8 +1104,8 @@ def __setattr__(self, key: str, value: Any):

if not isinstance(unwrapped, (type(None), int, float, str, bytes, list, dict, set)):
raise AttributeError(
'attribute {!r} cannot be a {}: must be int/float/dict/list/etc'.format(
key, type(unwrapped).__name__))
f'attribute {key!r} cannot be a {type(unwrapped).__name__}: '
'must be int/float/dict/list/etc')

self._data[key] = unwrapped

Expand Down Expand Up @@ -1191,8 +1189,9 @@ def __get__(self,
if bound is not None:
# the StoredState instance is being stored in two different
# attributes -> unclear what is expected of us -> bail out
raise RuntimeError("StoredState shared by {0}.{1} and {0}.{2}".format(
cls.__name__, self.attr_name, attr_name))
raise RuntimeError(
f'StoredState shared by {cls.__name__}.{self.attr_name} and '
f'{cls.__name__}.{attr_name}')
# we've found ourselves for the first time; save where, and bind the object
self.attr_name = attr_name
self.parent_type = cls
Expand Down
50 changes: 26 additions & 24 deletions ops/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,8 @@ def set_workload_version(self, version: str) -> None:
shown in the output of 'juju status'.
"""
if not isinstance(version, str):
raise TypeError("workload version must be a str, not {}: {!r}".format(
type(version).__name__, version))
raise TypeError(
f'workload version must be a str, not {type(version).__name__}: {version!r}')
self._backend.application_version_set(version)

@property
Expand Down Expand Up @@ -862,9 +862,9 @@ def _invalidate(self, relation_name: str):
def _get_unique(self, relation_name: str, relation_id: Optional[int] = None):
if relation_id is not None:
if not isinstance(relation_id, int):
raise ModelError('relation id {} must be int or None not {}'.format(
relation_id,
type(relation_id).__name__))
raise ModelError(
f'relation id {relation_id} must be int or None, '
f'not {type(relation_id).__name__}')
for relation in self[relation_name]:
if relation.id == relation_id:
return relation
Expand Down Expand Up @@ -908,8 +908,9 @@ def get(self, binding_key: Union[str, 'Relation']) -> 'Binding':
binding_name = binding_key
relation_id = None
else:
raise ModelError('binding key must be str or relation instance, not {}'
''.format(type(binding_key).__name__))
raise ModelError(
f'binding key must be str or relation instance, not {type(binding_key).__name__}'
)
binding = self._data.get(binding_key)
if binding is None:
binding = Binding(binding_name, relation_id, self._backend)
Expand Down Expand Up @@ -1629,9 +1630,8 @@ def _validate_read(self):
if self._backend.app_name == self._entity.name:
# minions can't read local app databags
raise RelationDataAccessError(
"{} is not leader and cannot read its own application databag".format(
self._backend.unit_name
)
f'{self._backend.unit_name} is not leader and cannot read its own '
f'application databag'
)

return True
Expand Down Expand Up @@ -1660,9 +1660,8 @@ def _validate_write(self, key: str, value: str):
is_our_app: bool = self._backend.app_name == self._entity.name
if not is_our_app:
raise RelationDataAccessError(
"{} cannot write the data of remote application {}".format(
self._backend.app_name, self._entity.name
))
f'{self._backend.app_name} cannot write the data of remote application '
f'{self._entity.name}')
# Whether the application data bag is mutable or not depends on
# whether this unit is a leader or not, but this is not guaranteed
# to be always true during the same hook execution.
Expand All @@ -1676,9 +1675,8 @@ def _validate_write(self, key: str, value: str):
# is it OUR UNIT's?
if self._backend.unit_name != self._entity.name:
raise RelationDataAccessError(
"{} cannot write databag of {}: not the same unit.".format(
self._backend.unit_name, self._entity.name
)
f'{self._backend.unit_name} cannot write databag of {self._entity.name}: '
f'not the same unit.'
)

def __setitem__(self, key: str, value: str):
Expand Down Expand Up @@ -2867,8 +2865,9 @@ class TooManyRelatedAppsError(ModelError):
"""Raised by :meth:`Model.get_relation` if there is more than one integrated application."""

def __init__(self, relation_name: str, num_related: int, max_supported: int):
super().__init__('Too many remote applications on {} ({} > {})'.format(
relation_name, num_related, max_supported))
super().__init__(
f'Too many remote applications on {relation_name} ({num_related} > {max_supported})'
)
self.relation_name = relation_name
self.num_related = num_related
self.max_supported = max_supported
Expand Down Expand Up @@ -2950,8 +2949,9 @@ def _format_action_result_dict(input: Dict[str, Any],
# other exceptions raised on key validation...
raise ValueError(f'invalid key {key!r}; must be a string')
if not _ACTION_RESULT_KEY_REGEX.match(key):
raise ValueError("key '{!r}' is invalid: must be similar to 'key', 'some-key2', or "
"'some.key'".format(key))
raise ValueError(
f"key {key!r} is invalid: must be similar to 'key', 'some-key2', "
f"or 'some.key'")

if parent_key:
key = f"{parent_key}.{key}"
Expand All @@ -2960,8 +2960,9 @@ def _format_action_result_dict(input: Dict[str, Any],
value = typing.cast(Dict[str, Any], value)
output_ = _format_action_result_dict(value, key, output_)
elif key in output_:
raise ValueError("duplicate key detected in dictionary passed to 'action-set': {!r}"
.format(key))
raise ValueError(
f"duplicate key detected in dictionary passed to 'action-set': {key!r}"
)
else:
output_[key] = value # type: ignore

Expand Down Expand Up @@ -3531,8 +3532,9 @@ def validate_metric_key(cls, key: str):
def validate_metric_label(cls, label_name: str):
if cls.METRIC_KEY_REGEX.match(label_name) is None:
raise ModelError(
'invalid metric label name {!r}: must match {}'.format(
label_name, cls.METRIC_KEY_REGEX.pattern))
f'invalid metric label name {label_name!r}: '
f'must match {cls.METRIC_KEY_REGEX.pattern}'
)

@classmethod
def format_metric_value(cls, value: Union[int, float]):
Expand Down
8 changes: 5 additions & 3 deletions ops/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,9 +990,11 @@ def add_relation_unit(self, relation_id: int, remote_unit_name: str) -> None:
app = relation.app
if not remote_unit_name.startswith(app.name):
warnings.warn(
'Remote unit name invalid: the remote application of {} is called {!r}; '
'the remote unit name should be {}/<some-number>, not {!r}.'
''.format(relation_name, app.name, app.name, remote_unit_name))
f'Remote unit name invalid: '
f'the remote application of {relation_name} is called {app.name!r}; '
f'the remote unit name should be {app.name}/<some-number>, '
f'not {remote_unit_name!r}.'
)
app_and_units = self._backend._relation_app_and_units
app_and_units[relation_id]["units"].append(remote_unit_name)
# Make sure that the Model reloads the relation_list for this relation_id, as well as
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ ignore = [
# No explicit `stacklevel` keyword argument found
"B028",

# Return condition directly, prefer readability.
"SIM103",
# Use contextlib.suppress() instead of try/except: pass
"SIM105",
# Use a single `with` statement with multiple contexts instead of nested `with` statements
Expand Down Expand Up @@ -170,6 +172,10 @@ ignore = [
"RUF001", # String contains ambiguous `µ` (MICRO SIGN). Did you mean `μ` (GREEK SMALL LETTER MU)?
"RUF002", # Docstring contains ambiguous `µ` (MICRO SIGN). Did you mean `μ` (GREEK SMALL LETTER MU)?
]
"test/test_helpers.py" = [
"S605", # Starting a process with a shell: seems safe, but may be changed in the future; consider rewriting without `shell`
"S607", # Starting a process with a partial executable path
]

[tool.ruff.lint.pydocstyle]
convention = "google"
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ commands =
[testenv:lint]
description = Check code against coding style standards
deps =
ruff~=0.2.2
ruff~=0.3.5
commands =
ruff check --preview

Expand Down

0 comments on commit 354a17b

Please sign in to comment.