diff --git a/docs/changelog.rst b/docs/changelog.rst index 8cf036e..5f541ae 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,10 @@ Changelog `CalVer, YY.month.patch `_ +26.11.1 +====== +- Rename ASYNC126 exceptiongroup-subclass-missing-derive to :ref:`ASYNC402 `, since it is about ``ExceptionGroup`` handling rather than async code. Update any ``noqa: ASYNC126`` comments and ``--enable``/``--disable`` settings. `(issue #457) `_ + 26.8.1 ====== - Add :ref:`ASYNC128 ` task-status-never-started, warning about startable functions (i.e. with a ``task_status`` parameter) that never call ``task_status.started()``. `(issue #471) `_ @@ -28,7 +32,7 @@ Changelog - Autofix for :ref:`ASYNC910 ` / :ref:`ASYNC911 ` no longer inserts checkpoints inside ``except`` clauses (which would trigger :ref:`ASYNC120 `); instead the checkpoint is added at the top of the function or of the enclosing loop. `(issue #403) `_ - :ref:`ASYNC910 ` and :ref:`ASYNC911 ` now accept ``__aenter__`` / ``__aexit__`` methods when the partner method provides the checkpoint, or when only one of the two is defined on a class that inherits from another class (charitably assuming the partner is inherited and contains a checkpoint). `(issue #441) `_ - :ref:`ASYNC300 ` no longer triggers when the result of ``asyncio.create_task()`` is returned from a function. `(issue #398) `_ -- Add :ref:`ASYNC126 ` exceptiongroup-subclass-missing-derive. `(issue #334) `_ +- Add ASYNC126 exceptiongroup-subclass-missing-derive. `(issue #334) `_ - :ref:`ASYNC102 ` no longer warns on ``await trio.aclose_forcefully(...)`` / ``await anyio.aclose_forcefully(...)``, which are designed for cleanup and cancel immediately by design. `(issue #446) `_ - :ref:`ASYNC101 ` now also triggers for common third-party context managers that open internal cancel scopes, nurseries, or task groups: ``trio_websocket.{open_websocket, open_websocket_url, serve_websocket}``, ``trio_asyncio.open_loop``, ``trio_parallel.open_worker_context``, ``trio_util.{move_on_when, run_and_cancelling}``, ``qtrio.{open_emissions_nursery, enter_emissions_channel}``, ``anyio.from_thread.{BlockingPortal, start_blocking_portal}``, ``asgi_lifespan.LifespanManager``, ``apscheduler.AsyncScheduler``, ``mcp.client.streamable_http.streamablehttp_client``, and ``mcp.client.sse.sse_client``. `(issue #350) `_ - :ref:`ASYNC102 ` and :ref:`ASYNC120 ` no longer trigger on ``await trio.lowlevel.cancel_shielded_checkpoint()`` (or the ``anyio.lowlevel`` equivalent), which is explicitly a schedule-but-not-cancel point and therefore safe inside ``finally`` / cancelled ``except`` / ``__aexit__``. diff --git a/docs/rules.rst b/docs/rules.rst index 2082bcc..1462696 100644 --- a/docs/rules.rst +++ b/docs/rules.rst @@ -126,14 +126,6 @@ _`ASYNC125`: constant-absolute-deadline :func:`anyio.move_on_after`, or the ``relative_deadline`` parameter to :class:`trio.CancelScope`. -_`ASYNC126`: exceptiongroup-subclass-missing-derive - A subclass of :class:`ExceptionGroup` or :class:`BaseExceptionGroup` must override - :meth:`~BaseExceptionGroup.derive` to return an instance of itself, otherwise - :meth:`~BaseExceptionGroup.split` and :meth:`~BaseExceptionGroup.subgroup` - which - are used by e.g. :ref:`taskgroup_nursery` implementations - will silently produce - plain ``ExceptionGroup`` instances and lose the custom subclass. - See `trio#3175 `_ for motivation. - _`ASYNC127`: unmaintained-httpx The original ``httpx`` package is no longer maintained, which is a problem for a networking library. Use `httpx2`_ - the continuation of the project, maintained @@ -235,6 +227,14 @@ _`ASYNC400` : except-star-invalid-attribute _`ASYNC401` : pytest-raises-exception-group ``pytest.raises(ExceptionGroup)`` and ``pytest.raises(BaseExceptionGroup)`` usually hide the structure of exception groups. Prefer ``pytest.RaisesGroup``. +_`ASYNC402` : exceptiongroup-subclass-missing-derive + A subclass of :class:`ExceptionGroup` or :class:`BaseExceptionGroup` must override + :meth:`~BaseExceptionGroup.derive` to return an instance of itself, otherwise + :meth:`~BaseExceptionGroup.split` and :meth:`~BaseExceptionGroup.subgroup` - which + are used by e.g. :ref:`taskgroup_nursery` implementations - will silently produce + plain ``ExceptionGroup`` instances and lose the custom subclass. + See `trio#3175 `_ for motivation. + Optional rules disabled by default ================================== diff --git a/docs/usage.rst b/docs/usage.rst index 00e092f..50a4cff 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -33,7 +33,7 @@ adding the following to your ``.pre-commit-config.yaml``: minimum_pre_commit_version: '2.9.0' repos: - repo: https://github.com/python-trio/flake8-async - rev: 26.8.1 + rev: 26.11.1 hooks: - id: flake8-async # args: ["--enable=ASYNC100,ASYNC112", "--disable=", "--autofix=ASYNC"] diff --git a/flake8_async/__init__.py b/flake8_async/__init__.py index ab66ef4..739e8ee 100644 --- a/flake8_async/__init__.py +++ b/flake8_async/__init__.py @@ -38,7 +38,7 @@ # CalVer: YY.month.patch, e.g. first release of July 2022 == "22.7.1" -__version__ = "26.8.1" +__version__ = "26.11.1" # taken from https://github.com/Zac-HD/shed diff --git a/flake8_async/visitors/visitor4xx.py b/flake8_async/visitors/visitor4xx.py index 90cdf56..9b84c62 100644 --- a/flake8_async/visitors/visitor4xx.py +++ b/flake8_async/visitors/visitor4xx.py @@ -2,6 +2,8 @@ ASYNC400 except-star-invalid-attribute checks for invalid attribute access on except* ASYNC401 pytest-raises-exception-group checks for pytest.raises(ExceptionGroup) +ASYNC402 exceptiongroup-subclass-missing-derive checks that ExceptionGroup subclasses + override derive """ from __future__ import annotations @@ -145,3 +147,38 @@ def visit_Call(self, node: ast.Call): and (exception_group := self._exception_group_name(expected_exception)) ): self.error(node, exception_group) + + +@error_class +class Visitor402(Flake8AsyncVisitor): + error_codes: Mapping[str, str] = { + "ASYNC402": ( + "ExceptionGroup subclass {} should override `derive`, otherwise" + " `split`/`subgroup` (used by e.g. nursery/TaskGroup" + " implementations) will silently produce plain `ExceptionGroup`" + " instances instead of `{}`." + ) + } + + def visit_ClassDef(self, node: ast.ClassDef): + def base_name(base: ast.expr) -> str: + # strip generic subscripts like `ExceptionGroup[Foo]` + if isinstance(base, ast.Subscript): + base = base.value + canonical = self.canonical_name(base) + unparsed = canonical if canonical is not None else ast.unparse(base) + return unparsed.rsplit(".", 1)[-1] + + if not any( + base_name(b) in ("ExceptionGroup", "BaseExceptionGroup") for b in node.bases + ): + return + + for item in node.body: + if ( + isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)) + and item.name == "derive" + ): + return + + self.error(node, node.name, node.name) diff --git a/flake8_async/visitors/visitors.py b/flake8_async/visitors/visitors.py index 281d613..a9527a7 100644 --- a/flake8_async/visitors/visitors.py +++ b/flake8_async/visitors/visitors.py @@ -558,41 +558,6 @@ def is_constant(value: ast.expr) -> bool: ) -@error_class -class Visitor126(Flake8AsyncVisitor): - error_codes: Mapping[str, str] = { - "ASYNC126": ( - "ExceptionGroup subclass {} should override `derive`, otherwise" - " `split`/`subgroup` (used by e.g. nursery/TaskGroup" - " implementations) will silently produce plain `ExceptionGroup`" - " instances instead of `{}`." - ) - } - - def visit_ClassDef(self, node: ast.ClassDef): - def base_name(base: ast.expr) -> str: - # strip generic subscripts like `ExceptionGroup[Foo]` - if isinstance(base, ast.Subscript): - base = base.value - canonical = self.canonical_name(base) - unparsed = canonical if canonical is not None else ast.unparse(base) - return unparsed.rsplit(".", 1)[-1] - - if not any( - base_name(b) in ("ExceptionGroup", "BaseExceptionGroup") for b in node.bases - ): - return - - for item in node.body: - if ( - isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)) - and item.name == "derive" - ): - return - - self.error(node, node.name, node.name) - - @error_class class Visitor127(Flake8AsyncVisitor): error_codes: Mapping[str, str] = { diff --git a/tests/eval_files/async126.py b/tests/eval_files/async402.py similarity index 100% rename from tests/eval_files/async126.py rename to tests/eval_files/async402.py diff --git a/tests/test_flake8_async.py b/tests/test_flake8_async.py index f0dabd5..f3f7f18 100644 --- a/tests/test_flake8_async.py +++ b/tests/test_flake8_async.py @@ -539,11 +539,11 @@ def _parse_eval_file( "ASYNC122", "ASYNC123", "ASYNC125", - "ASYNC126", "ASYNC127", "ASYNC300", "ASYNC400", "ASYNC401", + "ASYNC402", "ASYNC912", } diff --git a/tests/test_messages_documented.py b/tests/test_messages_documented.py index dae79a1..aad9dbe 100755 --- a/tests/test_messages_documented.py +++ b/tests/test_messages_documented.py @@ -13,9 +13,10 @@ CHANGELOG = ROOT_PATH / "docs" / "changelog.rst" RULES_DOC = ROOT_PATH / "docs" / "rules.rst" -# 107, 108 & 117 are removed (but still mentioned in changelog & readme) +# 107, 108 & 117 are removed, and 126 was renamed to 402 +# (but still mentioned in changelog & readme) # ASYNCxxx_* are fake codes to get different error messages for the same code -IGNORED_CODES_REGEX = r"(TRIO|ASYNC)(107|108|117)|ASYNC\d\d\d_.*" +IGNORED_CODES_REGEX = r"(TRIO|ASYNC)(107|108|117|126)|ASYNC\d\d\d_.*" # temp function for eval files