Skip to content

Commit

Permalink
Refactor code formatting for better readability
Browse files Browse the repository at this point in the history
Reformatted function calls and comments across `tests/test_tenacity.py` and `tenacity/config.py` for consistent indentation and alignment. This improves the code's readability and maintainability.
  • Loading branch information
SalvatoreZagaria committed Sep 28, 2024
1 parent 58b9993 commit 506a573
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
11 changes: 7 additions & 4 deletions tenacity/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class DictConfig:
- __contains__: Checks if a configuration attribute exists.
- __repr__: Returns a string representation of the configuration object.
"""

_instance = None
_lock = Lock() # For thread safety
_lock = Lock() # For thread safety

def __new__(cls):
if cls._instance is None:
Expand All @@ -60,9 +61,11 @@ def delete_attribute(self, name: str) -> None:
if name in self._config:
del self._config[name]
else:
raise KeyError(f'Attribute {name} not found in configuration.')
raise KeyError(f"Attribute {name} not found in configuration.")

def get_config(self, override: t.Optional[t.Dict[str, t.Any]] = None) -> t.Dict[str, t.Any]:
def get_config(
self, override: t.Optional[t.Dict[str, t.Any]] = None
) -> t.Dict[str, t.Any]:
"""
Retrieves the configuration dictionary.
Expand Down Expand Up @@ -93,7 +96,7 @@ def __contains__(self, name: str) -> bool:
return name in self._config

def __repr__(self) -> str:
return f'<DictConfig {self._config}>'
return f"<DictConfig {self._config}>"


dict_config = DictConfig()
23 changes: 17 additions & 6 deletions tests/test_tenacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,16 +1800,22 @@ def setUp(self):

def test_set_and_get_config(self):
# Set new configuration attributes
dict_config.set_config(stop=tenacity.stop_after_attempt(3), wait=tenacity.wait_fixed(1))
dict_config.set_config(
stop=tenacity.stop_after_attempt(3), wait=tenacity.wait_fixed(1)
)
self.assertIsInstance(dict_config.get("stop"), tenacity.stop_after_attempt)
self.assertIsInstance(dict_config.get("wait"), tenacity.wait_fixed)

def test_override_config(self):
# Set initial configuration
dict_config.set_config(stop=tenacity.stop_after_attempt(3), wait=tenacity.wait_fixed(1))
dict_config.set_config(
stop=tenacity.stop_after_attempt(3), wait=tenacity.wait_fixed(1)
)

# Override specific attribute
custom_config = dict_config.get_config(override={"wait": tenacity.wait_fixed(2)})
custom_config = dict_config.get_config(
override={"wait": tenacity.wait_fixed(2)}
)
self.assertIsInstance(custom_config["wait"], tenacity.wait_fixed)
self.assertIsInstance(custom_config["stop"], tenacity.stop_after_attempt)

Expand All @@ -1824,17 +1830,22 @@ def test_delete_config(self):

def test_retry_with_default_config(self):
# Set default configuration
dict_config.set_config(stop=tenacity.stop_after_attempt(2), wait=tenacity.wait_fixed(0.1))
dict_config.set_config(
stop=tenacity.stop_after_attempt(2), wait=tenacity.wait_fixed(0.1)
)

@retry
def failing_func():
raise ValueError("This should trigger retries")

with self.assertRaises(tenacity.RetryError):
failing_func() # Should raise a RetryError
failing_func() # Should raise a RetryError

def test_retry_with_override(self):
# Set default configuration
dict_config.set_config(stop=tenacity.stop_after_attempt(2), wait=tenacity.wait_fixed(0.1))
dict_config.set_config(
stop=tenacity.stop_after_attempt(2), wait=tenacity.wait_fixed(0.1)
)

@retry(reraise=True)
def failing_func():
Expand Down

0 comments on commit 506a573

Please sign in to comment.