Skip to content

Commit

Permalink
Enable all ruff rules and fix new lints
Browse files Browse the repository at this point in the history
  • Loading branch information
jodal committed Jul 17, 2024
1 parent cb5451e commit 28aa286
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 64 deletions.
58 changes: 14 additions & 44 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,82 +65,52 @@ warn_unused_configs = true
target-version = "py38"

[tool.ruff.lint]
select = [
"A", # flake8-builtins
"ANN", # flake8-annotations
"ARG", # flake8-unused-arguments
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"C90", # mccabe
"D", # pydocstyle
"DTZ", # flake8-datetimez
"E", # pycodestyle
"ERA", # eradicate
"F", # pyflakes
"FBT", # flake8-boolean-trap
"I", # isort
"INP", # flake8-no-pep420
"ISC", # flake8-implicit-str-concat
"N", # pep8-naming
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PLC", # pylint convention
"PLE", # pylint error
"PLR", # pylint refactor
"PLW", # pylint warning
"PT", # flake8-pytest-style
"PTH", # flake8-use-pathlib
"Q", # flake8-quotes
"RET", # flake8-return
"RSE", # flake8-raise
"RUF", # ruff
"SIM", # flake8-simplify
"SLF", # flake8-self
"T20", # flake8-print
"TCH", # flake8-type-checking
"TID", # flake8-tidy-imports
"TRY", # tryceratops
"UP", # pyupgrade
"W", # pycodestyle
]
select = ["ALL"]
ignore = [
"A003", # builtin-attribute-shadowing
"ANN101", # missing-type-self
"ANN102", # missing-type-cls
"ANN401", # any-type
"D203", # one-blank-line-before-class
"D213", # multi-line-summary-second-line
"ISC001", # single-line-implicit-string-concatenation
"G004", # logging-f-string
"PLR2004", # magic-value-comparison
"RET504", # unnecessary-assign
"S101", # assert
"TRY003", # raise-vanilla-args
"UP007", # typing-union
#
# Equivalent to `pyupgrade --keep-runtime-typing`:
"UP006", # deprecated-collection-type
"UP007", # typing-union
# These rules interfere with `ruff format`
"COM812", # missing-trailing-comma
"ISC001", # single-line-implicit-string-concatenation
]

[tool.ruff.lint.per-file-ignores]
"docs/*" = [
"D", # pydocstyle
"INP001", # flake8-no-pep420
"INP001", # implicit-namespace-package
]
"examples/*" = [
"ANN", # flake8-annotations
"BLE001", # blind-except
"D", # pydocstyle
"INP001", # flake8-no-pep420
"INP001", # implicit-namespace-package
"T20", # flake8-print
]
"tests/*" = [
"ANN", # flake8-annotations
"ARG", # flake8-unused-arguments
"D", # pydocstyle
"EM101", # raw-string-in-exception
"TRY002", # raise-vanilla-class
]

[tool.ruff.lint.isort]
known-first-party = ["pykka"]

[tool.ruff.lint.pyupgrade]
keep-runtime-typing = true

[tool.pyright]
pythonVersion = "3.8"
venvPath = "."
Expand Down
19 changes: 11 additions & 8 deletions src/pykka/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,25 @@ def _create_actor_inbox() -> ActorInbox:
Internal method for implementors of new actor types.
"""
raise NotImplementedError("Use a subclass of Actor")
msg = "Use a subclass of Actor"
raise NotImplementedError(msg)

@staticmethod
def _create_future() -> Future[Any]:
"""Create a future for the actor.
Internal method for implementors of new actor types.
"""
raise NotImplementedError("Use a subclass of Actor")
msg = "Use a subclass of Actor"
raise NotImplementedError(msg)

def _start_actor_loop(self) -> None:
"""Create and start the actor's event loop.
Internal method for implementors of new actor types.
"""
raise NotImplementedError("Use a subclass of Actor")
msg = "Use a subclass of Actor"
raise NotImplementedError(msg)

#: The actor URN string is a universally unique identifier for the actor.
#: It may be used for looking up a specific actor using
Expand Down Expand Up @@ -210,7 +213,7 @@ def _stop(self) -> None:
logger.debug(f"Stopped {self}")
try:
self.on_stop()
except Exception:
except Exception: # noqa: BLE001
self._handle_failure(*sys.exc_info())

def _actor_loop(self) -> None:
Expand All @@ -225,7 +228,7 @@ def _actor_loop(self) -> None:
def _actor_loop_setup(self) -> None:
try:
self.on_start()
except Exception:
except Exception: # noqa: BLE001
self._handle_failure(*sys.exc_info())

def _actor_loop_running(self) -> None:
Expand All @@ -235,7 +238,7 @@ def _actor_loop_running(self) -> None:
response = self._handle_receive(envelope.message)
if envelope.reply_to is not None:
envelope.reply_to.set(response)
except Exception:
except Exception: # noqa: BLE001
if envelope.reply_to is not None:
logger.info(
f"Exception returned from {self} to caller:",
Expand All @@ -246,9 +249,9 @@ def _actor_loop_running(self) -> None:
self._handle_failure(*sys.exc_info())
try:
self.on_failure(*sys.exc_info())
except Exception:
except Exception: # noqa: BLE001
self._handle_failure(*sys.exc_info())
except BaseException:
except BaseException: # noqa: BLE001
exception_value = sys.exc_info()[1]
logger.debug(f"{exception_value!r} in {self}. Stopping all actors.")
self._stop()
Expand Down
10 changes: 6 additions & 4 deletions src/pykka/_introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ def introspect_attrs(
result[attr_path] = attr_info

if attr_info.traversable:
for attr_name in dir(attr):
attr_paths_to_visit.append((*attr_path, attr_name))
attr_paths_to_visit.extend(
[(*attr_path, attr_name) for attr_name in dir(attr)]
)

return result

Expand All @@ -71,10 +72,11 @@ def get_attr_from_parent(
try:
return parent_attrs[attr_name]
except KeyError:
raise AttributeError(
msg = (
f"type object {parent.__class__.__name__!r} "
f"has no attribute {attr_name!r}"
) from None
)
raise AttributeError(msg) from None


def get_attr_directly(
Expand Down
9 changes: 6 additions & 3 deletions src/pykka/_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ def __init__(
attr_path: Optional[AttrPath] = None,
) -> None:
if not actor_ref.is_alive():
raise ActorDeadError(f"{actor_ref} not found")
msg = f"{actor_ref} not found"
raise ActorDeadError(msg)
self.actor_ref = actor_ref
self._actor = actor_ref._actor # noqa: SLF001
self._attr_path = attr_path or ()
Expand Down Expand Up @@ -174,7 +175,8 @@ def __getattr__(self, name: str) -> Any:

attr_info = self._known_attrs.get(attr_path)
if attr_info is None:
raise AttributeError(f"{self} has no attribute {name!r}")
msg = f"{self} has no attribute {name!r}"
raise AttributeError(msg)

if attr_info.callable:
if attr_path not in self._callable_proxies:
Expand Down Expand Up @@ -330,9 +332,10 @@ def play(self):
.. versionadded:: 2.0
"""
if hasattr(obj, "__slots__"):
raise ValueError(
msg = (
"pykka.traversable() cannot be used to mark "
"an object using slots as traversable."
)
raise ValueError(msg)
obj._pykka_traversable = True # type: ignore[attr-defined] # noqa: SLF001
return obj
6 changes: 4 additions & 2 deletions src/pykka/_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def tell(
:return: nothing
"""
if not self.is_alive():
raise ActorDeadError(f"{self} not found")
msg = f"{self} not found"
raise ActorDeadError(msg)
self.actor_inbox.put(Envelope(message))

@overload
Expand Down Expand Up @@ -160,7 +161,8 @@ def ask(

try:
if not self.is_alive():
raise ActorDeadError(f"{self} not found") # noqa: TRY301
msg = f"{self} not found"
raise ActorDeadError(msg) # noqa: TRY301
except ActorDeadError:
future.set_exception()
else:
Expand Down
3 changes: 2 additions & 1 deletion src/pykka/_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def get(
raise exc_value.with_traceback(exc_traceback)
raise exc_value
except queue.Empty:
raise Timeout(f"{timeout} seconds") from None
msg = f"{timeout} seconds"
raise Timeout(msg) from None
else:
return self._result.value

Expand Down
3 changes: 2 additions & 1 deletion tests/log_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ def wait_for_message(
return
self.events[level].clear()
self.events[level].wait(1)
raise Exception(f"Timeout: Waited {timeout:d}s for log message")
msg = f"Timeout: Waited {timeout:d}s for log message"
raise Exception(msg)
2 changes: 1 addition & 1 deletion tests/proxy/test_mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_actor_with_callable_mock_property_does_not_work(
actor_class.a_rw_property = mock # type: ignore[method-assign]
proxy = actor_class.start().proxy()

# XXX Because Mock and MagicMock are callable by default, they cause the
# Because Mock and MagicMock are callable by default, they cause the
# property to be wrapped in a `CallableProxy`. Thus, the property no
# longer behaves as a property when mocked and accessed through a proxy.
with pytest.raises(AttributeError) as exc_info:
Expand Down

0 comments on commit 28aa286

Please sign in to comment.