From d648805656b759d6277bb4efce5b981a588761d9 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 4 Nov 2023 11:17:28 -0500 Subject: [PATCH 1/6] Adopt sp-repo-review --- .github/workflows/python-tests.yml | 4 +- .pre-commit-config.yaml | 80 ++++++++++++++++++++++++++++ README.md | 1 - comm/__init__.py | 11 ++-- comm/base_comm.py | 84 +++++++++++++++++------------- comm/py.typed | 0 pyproject.toml | 74 ++++++++++---------------- tests/test_comm.py | 7 +-- 8 files changed, 166 insertions(+), 95 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 comm/py.typed diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 7d10863..a409de1 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -38,8 +38,8 @@ jobs: steps: - uses: actions/checkout@v3 - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 - - name: Run Linters - run: | + - name: Run Linters + run: | hatch run typing:test hatch run lint:style pipx run 'validate-pyproject[all]' pyproject.toml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..cb318d9 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,80 @@ +ci: + autoupdate_schedule: monthly + autoupdate_commit_msg: "chore: update pre-commit hooks" + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-case-conflict + - id: check-ast + - id: check-docstring-first + - id: check-executables-have-shebangs + - id: check-added-large-files + - id: check-case-conflict + - id: check-merge-conflict + - id: check-json + - id: check-toml + - id: check-yaml + - id: debug-statements + - id: end-of-file-fixer + - id: trailing-whitespace + + - repo: https://github.com/python-jsonschema/check-jsonschema + rev: 0.27.0 + hooks: + - id: check-github-workflows + + - repo: https://github.com/executablebooks/mdformat + rev: 0.7.17 + hooks: + - id: mdformat + additional_dependencies: + [mdformat-gfm, mdformat-frontmatter, mdformat-footnote] + + - repo: https://github.com/pre-commit/mirrors-prettier + rev: "v3.0.3" + hooks: + - id: prettier + types_or: [yaml, html, json] + + - repo: https://github.com/adamchainz/blacken-docs + rev: "1.16.0" + hooks: + - id: blacken-docs + additional_dependencies: [black==23.7.0] + + - repo: https://github.com/codespell-project/codespell + rev: "v2.2.6" + hooks: + - id: codespell + args: ["-L", "sur,nd"] + + - repo: https://github.com/pre-commit/pygrep-hooks + rev: "v1.10.0" + hooks: + - id: rst-backticks + - id: rst-directive-colons + - id: rst-inline-touching-normal + + - repo: https://github.com/pre-commit/mirrors-mypy + rev: "v1.6.1" + hooks: + - id: mypy + files: comm + stages: [manual] + additional_dependencies: + ["traitlets>=5.13"] + + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.1.3 + hooks: + - id: ruff + args: ["--fix", "--show-fixes"] + - id: ruff-format + + - repo: https://github.com/scientific-python/cookie + rev: "2023.10.27" + hooks: + - id: sp-repo-review + additional_dependencies: ["repo-review[cli]"] diff --git a/README.md b/README.md index 0ecf6df..f345f60 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,6 @@ import comm class MyCustomComm(comm.base_comm.BaseComm): - def publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys): # TODO implement the logic for sending comm messages through the iopub channel pass diff --git a/comm/__init__.py b/comm/__init__.py index 341a42e..4c83fec 100644 --- a/comm/__init__.py +++ b/comm/__init__.py @@ -7,8 +7,9 @@ the Jupyter kernel protocol. It also provides a base Comm implementation and a default CommManager for the IPython case. """ - -from .base_comm import BaseComm, CommManager +from __future__ import annotations +from typing import Any +from .base_comm import BaseComm, CommManager, MaybeDict, BuffersType __version__ = "0.1.4" __all__ = [ @@ -21,11 +22,11 @@ class DummyComm(BaseComm): - def publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys): + def publish_msg(self, msg_type: str, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType=None, **keys: Any) ->None: pass -def _create_comm(*args, **kwargs): +def _create_comm(*args: Any, **kwargs: Any) -> BaseComm: """Create a Comm. This method is intended to be replaced, so that it returns your Comm instance. @@ -33,7 +34,7 @@ def _create_comm(*args, **kwargs): return DummyComm(*args, **kwargs) -def _get_comm_manager(): +def _get_comm_manager() -> CommManager: """Get the current Comm manager, creates one if there is none. This method is intended to be replaced if needed (if you want to manage multiple CommManagers). diff --git a/comm/base_comm.py b/comm/base_comm.py index eeddfdc..e405b37 100644 --- a/comm/base_comm.py +++ b/comm/base_comm.py @@ -3,16 +3,25 @@ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. - +from __future__ import annotations import logging import uuid +import typing as t from traitlets.utils.importstring import import_item import comm +if t.TYPE_CHECKING: + from zmq.eventloop.zmqstream import ZMQStream + logger = logging.getLogger("Comm") +MessageType = t.Dict[str, t.Any] +MaybeDict = t.Optional[t.Dict[str, t.Any]] +BuffersType = t.Optional[t.List[bytes]] +CommCallback = t.Callable[[MessageType], None] +CommTargetCallback = t.Callable[["BaseComm", MessageType], None] class BaseComm: """Class for communicating between a Frontend and a Kernel @@ -23,18 +32,18 @@ class BaseComm: def __init__( self, - target_name="comm", - data=None, - metadata=None, - buffers=None, - comm_id=None, - primary=True, - target_module=None, - topic=None, - _open_data=None, - _close_data=None, - **kwargs, - ): + target_name:str="comm", + data: MaybeDict=None, + metadata: MaybeDict=None, + buffers: BuffersType=None, + comm_id: str | None=None, + primary:bool=True, + target_module: str|None=None, + topic:bytes|None=None, + _open_data: MaybeDict=None, + _close_data: MaybeDict=None, + **kwargs: t.Any, + ) -> None: super().__init__(**kwargs) self.comm_id = comm_id if comm_id else uuid.uuid4().hex @@ -46,8 +55,8 @@ def __init__( self._open_data = _open_data if _open_data else {} self._close_data = _close_data if _close_data else {} - self._msg_callback = None - self._close_callback = None + self._msg_callback: CommCallback|None = None + self._close_callback: CommCallback|None = None self._closed = True @@ -57,16 +66,16 @@ def __init__( else: self._closed = False - def publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys): + def publish_msg(self, msg_type:str, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType=None, **keys:t.Any) -> None: raise NotImplementedError("publish_msg Comm method is not implemented") - def __del__(self): + def __del__(self) -> None: """trigger close on gc""" self.close(deleting=True) # publishing messages - def open(self, data=None, metadata=None, buffers=None): # noqa + def open(self, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType=None)->None: """Open the frontend-side version of this comm""" if data is None: @@ -90,7 +99,7 @@ def open(self, data=None, metadata=None, buffers=None): # noqa comm_manager.unregister_comm(self) raise - def close(self, data=None, metadata=None, buffers=None, deleting=False): + def close(self, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType=None, deleting:bool=False)->None: """Close the frontend-side version of this comm""" if self._closed: # only close once @@ -108,7 +117,7 @@ def close(self, data=None, metadata=None, buffers=None, deleting=False): # If deleting, the comm can't be registered comm.get_comm_manager().unregister_comm(self) - def send(self, data=None, metadata=None, buffers=None): + def send(self, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType=None)->None: """Send a message to the frontend-side version of this comm""" self.publish_msg( "comm_msg", @@ -119,7 +128,7 @@ def send(self, data=None, metadata=None, buffers=None): # registering callbacks - def on_close(self, callback): + def on_close(self, callback:CommCallback|None) -> None: """Register a callback for comm_close Will be called with the `data` of the close message. @@ -128,7 +137,7 @@ def on_close(self, callback): """ self._close_callback = callback - def on_msg(self, callback): + def on_msg(self, callback: CommCallback|None) ->None: """Register a callback for comm_msg Will be called with the `data` of any comm_msg messages. @@ -139,17 +148,17 @@ def on_msg(self, callback): # handling of incoming messages - def handle_close(self, msg): + def handle_close(self, msg:MessageType)->None: """Handle a comm_close message""" logger.debug("handle_close[%s](%s)", self.comm_id, msg) if self._close_callback: self._close_callback(msg) - def handle_msg(self, msg): + def handle_msg(self, msg:MessageType)->None: """Handle a comm_msg message""" logger.debug("handle_msg[%s](%s)", self.comm_id, msg) if self._msg_callback: - from IPython import get_ipython # type:ignore + from IPython import get_ipython shell = get_ipython() if shell: @@ -164,11 +173,11 @@ class CommManager: # Public APIs - def __init__(self): - self.comms = {} - self.targets = {} + def __init__(self) -> None: + self.comms: dict[str, BaseComm] = {} + self.targets: dict[str, CommTargetCallback] = {} - def register_target(self, target_name, f): + def register_target(self, target_name:str, f:CommTargetCallback | str)->None: """Register a callable f for a given target name f will be called with two arguments when a comm_open message is received with `target`: @@ -181,24 +190,24 @@ def register_target(self, target_name, f): if isinstance(f, str): f = import_item(f) - self.targets[target_name] = f + self.targets[target_name] = t.cast(CommTargetCallback, f) - def unregister_target(self, target_name, f): + def unregister_target(self, target_name:str, f:CommTargetCallback) ->CommTargetCallback: """Unregister a callable registered with register_target""" return self.targets.pop(target_name) - def register_comm(self, comm): + def register_comm(self, comm: BaseComm) -> str: """Register a new comm""" comm_id = comm.comm_id self.comms[comm_id] = comm return comm_id - def unregister_comm(self, comm): + def unregister_comm(self, comm:BaseComm) -> None: """Unregister a comm, and close its counterpart""" # unlike get_comm, this should raise a KeyError comm = self.comms.pop(comm.comm_id) - def get_comm(self, comm_id): + def get_comm(self, comm_id:str) -> BaseComm | None: """Get a comm with a particular id Returns the comm if found, otherwise None. @@ -213,10 +222,11 @@ def get_comm(self, comm_id): if logger.isEnabledFor(logging.DEBUG): # don't create the list of keys if debug messages aren't enabled logger.debug("Current comms: %s", list(self.comms.keys())) + return None # Message handlers - def comm_open(self, stream, ident, msg): + def comm_open(self, stream:ZMQStream, ident: str, msg:MessageType)->None: """Handler for comm_open messages""" from comm import create_comm @@ -249,7 +259,7 @@ def comm_open(self, stream, ident, msg): exc_info=True, ) - def comm_msg(self, stream, ident, msg): + def comm_msg(self, stream:ZMQStream, ident:str, msg:MessageType)->None: """Handler for comm_msg messages""" content = msg["content"] comm_id = content["comm_id"] @@ -262,7 +272,7 @@ def comm_msg(self, stream, ident, msg): except Exception: logger.error("Exception in comm_msg for %s", comm_id, exc_info=True) - def comm_close(self, stream, ident, msg): + def comm_close(self, stream:ZMQStream, ident:str, msg:MessageType)->None: """Handler for comm_close messages""" content = msg["content"] comm_id = content["comm_id"] diff --git a/comm/py.typed b/comm/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/pyproject.toml b/pyproject.toml index 5dc076e..631f6d0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ dynamic = ["version"] description = "Jupyter Python Comm implementation, for usage in ipykernel, xeus-python etc." readme = "README.md" license = { file="LICENSE" } -requires-python = ">=3.6" +requires-python = ">=3.8" authors = [ { name = "Jupyter contributors" }, ] @@ -22,11 +22,6 @@ classifiers = [ "License :: OSI Approved :: BSD License", "Programming Language :: Python", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", ] dependencies = [ "traitlets>=4", @@ -36,13 +31,6 @@ dependencies = [ test = [ "pytest", ] -lint = [ - "black>=22.6.0", - "mdformat>0.7", - "mdformat-gfm>=0.3.5", - "ruff>=0.0.156" -] -typing = ["mypy>=0.990"] [project.urls] Homepage = "https://github.com/ipython/comm" @@ -62,52 +50,41 @@ test = "python -m pytest -vv {args}" nowarn = "test -W default {args}" [tool.hatch.envs.typing] -features = ["typing", "test"] +dependencies = ["pre-commit"] +detached = true [tool.hatch.envs.typing.scripts] -test = "mypy --install-types --non-interactive {args:comm tests}" +test = "pre-commit run --all-files --hook-stage manual mypy" [tool.hatch.envs.lint] -features = ["lint"] +dependencies = ["pre-commit"] +detached = true [tool.hatch.envs.lint.scripts] -style = [ - "ruff {args:.}", - "black --check --diff {args:.}", - "mdformat --check {args:*.md}" -] -fmt = [ - "black {args:.}", - "ruff --fix {args:.}", - "mdformat {args:*.md}" -] +build = ["pre-commit run --all-files ruff"] [tool.pytest.ini_options] -addopts = "-raXs --durations 10 --color=yes" +minversion = "6.0" +testpaths = ["tests"] +xfail_strict = true +log_cli_level = "info" +addopts = [ + "-raXs", "--durations=10", "--color=yes", "--doctest-modules", + "--showlocals", "--strict-markers", "--strict-config" +] filterwarnings = ["error"] [tool.mypy] -check_untyped_defs = true -disallow_incomplete_defs = true -disallow_untyped_decorators = true -no_implicit_optional = true -no_implicit_reexport = true -pretty = true -show_error_context = true +files = "comm" +python_version = "3.8" +strict = true show_error_codes = true -strict_equality = true -strict_optional = true -warn_unused_configs = true -warn_redundant_casts = true -warn_return_any = true -warn_unused_ignores = true - -[tool.black] -line-length = 100 -skip-string-normalization = true -target-version = ["py37"] +enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"] +warn_unreachable = true [tool.ruff] -target-version = "py37" +target-version = "py38" line-length = 100 + +[tool.ruff.lint] select = [ "A", "B", "C", "E", "F", "FBT", "I", "N", "Q", "RUF", "S", "T", "UP", "W", "YTT", @@ -123,6 +100,9 @@ unfixable = [ "RUF100", ] -[tool.ruff.per-file-ignores] +[tool.ruff.lint.per-file-ignores] # S101 Use of `assert` detected "tests/*" = ["S101"] + +[tool.repo-review] +ignore = ["PY007", "PP308", "PY004", "GH102", "MY101", "RTD100"] diff --git a/tests/test_comm.py b/tests/test_comm.py index 15bdb99..5318ab9 100644 --- a/tests/test_comm.py +++ b/tests/test_comm.py @@ -1,16 +1,17 @@ +from typing import Any from comm.base_comm import BaseComm, CommManager class MyComm(BaseComm): - def publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys): + def publish_msg(self, msg_type:str, data:Any=None, metadata:Any=None, buffers:Any=None, **keys:Any) ->None: pass -def test_comm_manager(): +def test_comm_manager() -> None: test = CommManager() assert test.targets == {} -def test_base_comm(): +def test_base_comm() -> None: test = MyComm() assert test.target_name == "comm" From aec489ac6a96261edd7bbe121c72ae6436c08a1e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 4 Nov 2023 16:23:23 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .pre-commit-config.yaml | 3 +- comm/__init__.py | 13 +++++-- comm/base_comm.py | 75 ++++++++++++++++++++++++++--------------- tests/test_comm.py | 10 +++++- 4 files changed, 68 insertions(+), 33 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cb318d9..04aea43 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -63,8 +63,7 @@ repos: - id: mypy files: comm stages: [manual] - additional_dependencies: - ["traitlets>=5.13"] + additional_dependencies: ["traitlets>=5.13"] - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.1.3 diff --git a/comm/__init__.py b/comm/__init__.py index 4c83fec..d85a8c8 100644 --- a/comm/__init__.py +++ b/comm/__init__.py @@ -8,8 +8,10 @@ It also provides a base Comm implementation and a default CommManager for the IPython case. """ from __future__ import annotations + from typing import Any -from .base_comm import BaseComm, CommManager, MaybeDict, BuffersType + +from .base_comm import BaseComm, BuffersType, CommManager, MaybeDict __version__ = "0.1.4" __all__ = [ @@ -22,7 +24,14 @@ class DummyComm(BaseComm): - def publish_msg(self, msg_type: str, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType=None, **keys: Any) ->None: + def publish_msg( + self, + msg_type: str, + data: MaybeDict = None, + metadata: MaybeDict = None, + buffers: BuffersType = None, + **keys: Any, + ) -> None: pass diff --git a/comm/base_comm.py b/comm/base_comm.py index e405b37..a092b1f 100644 --- a/comm/base_comm.py +++ b/comm/base_comm.py @@ -4,9 +4,10 @@ # Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. from __future__ import annotations + import logging -import uuid import typing as t +import uuid from traitlets.utils.importstring import import_item @@ -23,6 +24,7 @@ CommCallback = t.Callable[[MessageType], None] CommTargetCallback = t.Callable[["BaseComm", MessageType], None] + class BaseComm: """Class for communicating between a Frontend and a Kernel @@ -32,16 +34,16 @@ class BaseComm: def __init__( self, - target_name:str="comm", - data: MaybeDict=None, - metadata: MaybeDict=None, - buffers: BuffersType=None, - comm_id: str | None=None, - primary:bool=True, - target_module: str|None=None, - topic:bytes|None=None, - _open_data: MaybeDict=None, - _close_data: MaybeDict=None, + target_name: str = "comm", + data: MaybeDict = None, + metadata: MaybeDict = None, + buffers: BuffersType = None, + comm_id: str | None = None, + primary: bool = True, + target_module: str | None = None, + topic: bytes | None = None, + _open_data: MaybeDict = None, + _close_data: MaybeDict = None, **kwargs: t.Any, ) -> None: super().__init__(**kwargs) @@ -55,8 +57,8 @@ def __init__( self._open_data = _open_data if _open_data else {} self._close_data = _close_data if _close_data else {} - self._msg_callback: CommCallback|None = None - self._close_callback: CommCallback|None = None + self._msg_callback: CommCallback | None = None + self._close_callback: CommCallback | None = None self._closed = True @@ -66,7 +68,14 @@ def __init__( else: self._closed = False - def publish_msg(self, msg_type:str, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType=None, **keys:t.Any) -> None: + def publish_msg( + self, + msg_type: str, + data: MaybeDict = None, + metadata: MaybeDict = None, + buffers: BuffersType = None, + **keys: t.Any, + ) -> None: raise NotImplementedError("publish_msg Comm method is not implemented") def __del__(self) -> None: @@ -75,7 +84,9 @@ def __del__(self) -> None: # publishing messages - def open(self, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType=None)->None: + def open( + self, data: MaybeDict = None, metadata: MaybeDict = None, buffers: BuffersType = None + ) -> None: """Open the frontend-side version of this comm""" if data is None: @@ -99,7 +110,13 @@ def open(self, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType comm_manager.unregister_comm(self) raise - def close(self, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType=None, deleting:bool=False)->None: + def close( + self, + data: MaybeDict = None, + metadata: MaybeDict = None, + buffers: BuffersType = None, + deleting: bool = False, + ) -> None: """Close the frontend-side version of this comm""" if self._closed: # only close once @@ -117,7 +134,9 @@ def close(self, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersTyp # If deleting, the comm can't be registered comm.get_comm_manager().unregister_comm(self) - def send(self, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType=None)->None: + def send( + self, data: MaybeDict = None, metadata: MaybeDict = None, buffers: BuffersType = None + ) -> None: """Send a message to the frontend-side version of this comm""" self.publish_msg( "comm_msg", @@ -128,7 +147,7 @@ def send(self, data:MaybeDict=None, metadata:MaybeDict=None, buffers:BuffersType # registering callbacks - def on_close(self, callback:CommCallback|None) -> None: + def on_close(self, callback: CommCallback | None) -> None: """Register a callback for comm_close Will be called with the `data` of the close message. @@ -137,7 +156,7 @@ def on_close(self, callback:CommCallback|None) -> None: """ self._close_callback = callback - def on_msg(self, callback: CommCallback|None) ->None: + def on_msg(self, callback: CommCallback | None) -> None: """Register a callback for comm_msg Will be called with the `data` of any comm_msg messages. @@ -148,13 +167,13 @@ def on_msg(self, callback: CommCallback|None) ->None: # handling of incoming messages - def handle_close(self, msg:MessageType)->None: + def handle_close(self, msg: MessageType) -> None: """Handle a comm_close message""" logger.debug("handle_close[%s](%s)", self.comm_id, msg) if self._close_callback: self._close_callback(msg) - def handle_msg(self, msg:MessageType)->None: + def handle_msg(self, msg: MessageType) -> None: """Handle a comm_msg message""" logger.debug("handle_msg[%s](%s)", self.comm_id, msg) if self._msg_callback: @@ -177,7 +196,7 @@ def __init__(self) -> None: self.comms: dict[str, BaseComm] = {} self.targets: dict[str, CommTargetCallback] = {} - def register_target(self, target_name:str, f:CommTargetCallback | str)->None: + def register_target(self, target_name: str, f: CommTargetCallback | str) -> None: """Register a callable f for a given target name f will be called with two arguments when a comm_open message is received with `target`: @@ -192,7 +211,7 @@ def register_target(self, target_name:str, f:CommTargetCallback | str)->None: self.targets[target_name] = t.cast(CommTargetCallback, f) - def unregister_target(self, target_name:str, f:CommTargetCallback) ->CommTargetCallback: + def unregister_target(self, target_name: str, f: CommTargetCallback) -> CommTargetCallback: """Unregister a callable registered with register_target""" return self.targets.pop(target_name) @@ -202,12 +221,12 @@ def register_comm(self, comm: BaseComm) -> str: self.comms[comm_id] = comm return comm_id - def unregister_comm(self, comm:BaseComm) -> None: + def unregister_comm(self, comm: BaseComm) -> None: """Unregister a comm, and close its counterpart""" # unlike get_comm, this should raise a KeyError comm = self.comms.pop(comm.comm_id) - def get_comm(self, comm_id:str) -> BaseComm | None: + def get_comm(self, comm_id: str) -> BaseComm | None: """Get a comm with a particular id Returns the comm if found, otherwise None. @@ -226,7 +245,7 @@ def get_comm(self, comm_id:str) -> BaseComm | None: # Message handlers - def comm_open(self, stream:ZMQStream, ident: str, msg:MessageType)->None: + def comm_open(self, stream: ZMQStream, ident: str, msg: MessageType) -> None: """Handler for comm_open messages""" from comm import create_comm @@ -259,7 +278,7 @@ def comm_open(self, stream:ZMQStream, ident: str, msg:MessageType)->None: exc_info=True, ) - def comm_msg(self, stream:ZMQStream, ident:str, msg:MessageType)->None: + def comm_msg(self, stream: ZMQStream, ident: str, msg: MessageType) -> None: """Handler for comm_msg messages""" content = msg["content"] comm_id = content["comm_id"] @@ -272,7 +291,7 @@ def comm_msg(self, stream:ZMQStream, ident:str, msg:MessageType)->None: except Exception: logger.error("Exception in comm_msg for %s", comm_id, exc_info=True) - def comm_close(self, stream:ZMQStream, ident:str, msg:MessageType)->None: + def comm_close(self, stream: ZMQStream, ident: str, msg: MessageType) -> None: """Handler for comm_close messages""" content = msg["content"] comm_id = content["comm_id"] diff --git a/tests/test_comm.py b/tests/test_comm.py index 5318ab9..51ba9ee 100644 --- a/tests/test_comm.py +++ b/tests/test_comm.py @@ -1,9 +1,17 @@ from typing import Any + from comm.base_comm import BaseComm, CommManager class MyComm(BaseComm): - def publish_msg(self, msg_type:str, data:Any=None, metadata:Any=None, buffers:Any=None, **keys:Any) ->None: + def publish_msg( + self, + msg_type: str, + data: Any = None, + metadata: Any = None, + buffers: Any = None, + **keys: Any, + ) -> None: pass From ecf712c88a498f65d312d3355be0e5103bea96e9 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 4 Nov 2023 11:26:15 -0500 Subject: [PATCH 3/6] lint --- comm/base_comm.py | 2 +- pyproject.toml | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/comm/base_comm.py b/comm/base_comm.py index a092b1f..058e557 100644 --- a/comm/base_comm.py +++ b/comm/base_comm.py @@ -84,7 +84,7 @@ def __del__(self) -> None: # publishing messages - def open( + def open( # noqa: A003 self, data: MaybeDict = None, metadata: MaybeDict = None, buffers: BuffersType = None ) -> None: """Open the frontend-side version of this comm""" diff --git a/pyproject.toml b/pyproject.toml index 631f6d0..616870f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -86,12 +86,10 @@ line-length = 100 [tool.ruff.lint] select = [ - "A", "B", "C", "E", "F", "FBT", "I", "N", "Q", "RUF", "S", "T", + "A", "B", "C", "E", "F", "I", "N", "Q", "RUF", "S", "T", "UP", "W", "YTT", ] ignore = [ - # FBT002 Boolean default value in function definition - "FBT002", "FBT003", ] unfixable = [ # Don't touch print statements From 454000dcb75d902ad05a23de8efbd42ac5077a19 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 4 Nov 2023 11:31:07 -0500 Subject: [PATCH 4/6] cleanup matrix --- .github/workflows/python-tests.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index a409de1..5c102ab 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -3,6 +3,7 @@ on: push: branches: ["main"] pull_request: + workflow_dispatch: schedule: - cron: "0 8 * * *" @@ -14,16 +15,16 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, windows-latest, macos-latest] - python-version: ["3.7", "3.11"] + python-version: ["3.8", "3.12"] include: - os: windows-latest python-version: "3.9" - os: ubuntu-latest - python-version: "pypy-3.8" + python-version: "pypy-3.9" - os: ubuntu-latest python-version: "3.10" - os: macos-latest - python-version: "3.8" + python-version: "3.11" steps: - name: Checkout uses: actions/checkout@v3 From 62bb187535baffc08674334e393685c0ecf0d6ce Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 4 Nov 2023 11:33:33 -0500 Subject: [PATCH 5/6] update lint job --- .github/workflows/python-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 5c102ab..5798e75 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -42,7 +42,7 @@ jobs: - name: Run Linters run: | hatch run typing:test - hatch run lint:style + hatch run lint:build pipx run 'validate-pyproject[all]' pyproject.toml check_release: From 66eaa116427c8bddb641ecac1a0fa015740acaa3 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 4 Nov 2023 11:35:07 -0500 Subject: [PATCH 6/6] add check job --- .github/workflows/python-tests.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/python-tests.yml b/.github/workflows/python-tests.yml index 5798e75..3f4dcf8 100644 --- a/.github/workflows/python-tests.yml +++ b/.github/workflows/python-tests.yml @@ -67,3 +67,17 @@ jobs: - uses: actions/checkout@v3 - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1 - uses: jupyterlab/maintainer-tools/.github/actions/check-links@v1 + + check: # This job does nothing and is only used for the branch protection + if: always() + needs: + - check_links + - check_release + - test_lint + - build + runs-on: ubuntu-latest + steps: + - name: Decide whether the needed jobs succeeded or failed + uses: re-actors/alls-green@release/v1 + with: + jobs: ${{ toJSON(needs) }}