Skip to content

refactor(api): enable strict typing for archive/schedule/external-api test files - #44885

Open
shi-felix wants to merge 2 commits into
langgenius:mainfrom
shi-felix:refactor/pyrefly-strict-test-batch-13
Open

shi-felix wants to merge 2 commits into
langgenius:mainfrom
shi-felix:refactor/pyrefly-strict-test-batch-13

Conversation

@shi-felix

Copy link
Copy Markdown
Contributor

Summary

Enable strict pyrefly checking for 3 test files under tests/unit_tests/libs/ by removing them from the strict-mode exclusion list and fixing the resulting diagnostics:

  • Annotate helpers, closures, and parametrized parameters with their real types (botocore BaseClient/AWSRequest/AWSResponse/HTTPHeaders, TypedDict for visual-config cases, MagicMock, Flask).
  • Add @override to the eight setUp overrides and the UserMixin-style stub members (pattern already used in clients/agent_backend/test_client.py).
  • Replace deep Swagger-JSON index chains with a small _at(container, *keys) walker and explicit casts, so nested assertions stay type-clean without loosening types.
  • Keep suppression comments only where the type system cannot see the runtime truth: deliberately invalid inputs (BadRequest.description = {...} to trigger param-error shaping, a minimal error_body_formatter stand-in used before a flask-restx guard raises), and the loosely typed boto3.client(...) factory.

Verification

  • pyrefly check with the unit-test strict config: 0 diagnostics for all 3 files.
  • ruff format + ruff check: clean.
  • scripts/check_no_new_test_doubles.py --base-rev main: no new test doubles.
  • These libs/ files import tests/unit_tests/controllers/conftest, whose chain cannot be collected in this local Windows sandbox (hangs on import, same as events//tasks/), so runtime coverage relies on CI.

@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Oct 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-03 02:34:09.191807426 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-03 02:33:56.419716710 +0000
@@ -5720,20 +5720,6 @@
    --> tests/unit_tests/libs/broadcast_channel/redis/test_streams_channel.py:562:45
 ERROR Argument `FakeStreamsRedis` is not assignable to parameter `client` with type `Redis[Unknown] | RedisCluster[Unknown]` in function `libs.broadcast_channel.redis.streams_channel._StreamsSubscription.__init__` [bad-argument-type]
    --> tests/unit_tests/libs/broadcast_channel/redis/test_streams_channel.py:580:45
-ERROR Argument `dict[@_, @_]` is not assignable to parameter `headers` with type `HTTPHeaders` in function `botocore.awsrequest.AWSResponse.__init__` [bad-argument-type]
-   --> tests/unit_tests/libs/test_archive_storage.py:160:50
-ERROR `dict[str, str]` is not assignable to attribute `description` with type `str | None` [bad-assignment]
-  --> tests/unit_tests/libs/test_external_api.py:84:29
-ERROR Argument `_PassthroughFormatter` is not assignable to parameter `error_body_formatter` with type `ErrorBodyFormatter | None` in function `libs.external_api.ExternalApi.__init__` [bad-argument-type]
-   --> tests/unit_tests/libs/test_external_api.py:285:72
-ERROR Argument `Unknown | None` is not assignable to parameter `blueprint` with type `Blueprint` in function `flask.sansio.app.App.register_blueprint` [bad-argument-type]
-   --> tests/unit_tests/libs/test_external_api.py:399:28
-ERROR Cannot index into `str` [bad-index]
-   --> tests/unit_tests/libs/test_external_api.py:435:12
-ERROR Cannot index into `bool` [bad-index]
-   --> tests/unit_tests/libs/test_external_api.py:436:12
-ERROR Cannot index into `str` [bad-index]
-   --> tests/unit_tests/libs/test_external_api.py:436:12
 ERROR Class member `User.get_id` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
   --> tests/unit_tests/libs/test_flask_utils.py:18:9
 ERROR `str` is not assignable to dict key `error` with type `None` [bad-assignment]
@@ -5752,26 +5738,6 @@
    --> tests/unit_tests/libs/test_helper.py:181:31
 ERROR Argument `None` is not assignable to parameter `pattern` with type `str` in function `libs.helper.escape_like_pattern` [bad-argument-type]
    --> tests/unit_tests/libs/test_helper.py:228:38
-ERROR Class member `TestCronCompatibility.setUp` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-  --> tests/unit_tests/libs/test_schedule_utils.py:22:9
-ERROR Class member `TestTimezoneCompatibility.setUp` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-   --> tests/unit_tests/libs/test_schedule_utils.py:177:9
-ERROR Class member `TestFrontendBackendIntegration.setUp` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-   --> tests/unit_tests/libs/test_schedule_utils.py:258:9
-ERROR Argument `VisualConfig | str` is not assignable to parameter `frequency` with type `str` in function `services.trigger.schedule_service.ScheduleService.visual_to_cron` [bad-argument-type]
-   --> tests/unit_tests/libs/test_schedule_utils.py:319:60
-ERROR Argument `VisualConfig | str` is not assignable to parameter `visual_config` with type `VisualConfig` in function `services.trigger.schedule_service.ScheduleService.visual_to_cron` [bad-argument-type]
-   --> tests/unit_tests/libs/test_schedule_utils.py:319:84
-ERROR Class member `TestEnhancedCronSyntax.setUp` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-   --> tests/unit_tests/libs/test_schedule_utils.py:389:9
-ERROR Class member `TestTimezoneHandlingEnhanced.setUp` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-   --> tests/unit_tests/libs/test_schedule_utils.py:578:9
-ERROR Class member `TestErrorHandlingEnhanced.setUp` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-   --> tests/unit_tests/libs/test_schedule_utils.py:675:9
-ERROR Class member `TestPerformanceEnhanced.setUp` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-   --> tests/unit_tests/libs/test_schedule_utils.py:719:9
-ERROR Class member `TestRegressionEnhanced.setUp` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
-   --> tests/unit_tests/libs/test_schedule_utils.py:787:9
 ERROR TypedDict `TenantCustomConfigDict` does not have key `feature1` [bad-typed-dict-key]
    --> tests/unit_tests/models/test_account.py:657:23
 ERROR TypedDict `TenantCustomConfigDict` does not have key `feature2` [bad-typed-dict-key]

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 82.88% 82.99% +0.11%
Strict coverage 82.54% 82.64% +0.11%
Typed symbols 85,474 85,583 +109
Untyped symbols 17,725 17,616 -109
Modules 4373 4373 0

@autofix-ci
autofix-ci Bot requested a review from a team October 27, 2026 10:39
@codecov

codecov Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.80%. Comparing base (f9a3770) to head (2f3b6f1).
⚠️ Report is 135 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##             main   #44885     +/-   ##
=========================================
  Coverage   89.79%   89.80%             
=========================================
  Files        6240     6241      +1     
  Lines      396400   391693   -4707     
  Branches    79971    79439    -532     
=========================================
- Hits       355957   351744   -4213     
+ Misses      34142    33850    -292     
+ Partials     6301     6099    -202     
Flag Coverage Δ
api 89.64% <ø> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid cast

@asukaminato0721 asukaminato0721 self-assigned this Oct 29, 2026
…t files

Drop the three entries from the pyrefly exclude list now that the files are
annotated, so strict checking covers them here.
auto-merge was automatically disabled October 30, 2026 03:24

Head branch was pushed to by a user without write access

@shi-felix
shi-felix force-pushed the refactor/pyrefly-strict-test-batch-13 branch from 951fa91 to b217e9c Compare October 30, 2026 03:24
@shi-felix

Copy link
Copy Markdown
Contributor Author

Style Check is green again - test_error_component_names_must_not_collide was missing its -> None. Rebased onto main as well; pyrefly.toml now only drops the three exclude lines for this PR's files.

Reviewer asked to avoid `typing.cast`. Replace all sixteen casts with three small narrowing helpers (`_as_mapping`, `_as_items`, `_as_text`) that validate the JSON node before use, so the tests keep the same assertions without an escape hatch. The `check_no_new_cast` guard rejects net-new casts, aliases included.
@shi-felix

Copy link
Copy Markdown
Contributor Author

All sixteen typing.cast calls in test_external_api.py are gone, replaced by three narrowing helpers:

  • _as_mapping — string-keyed mapping
  • _as_items — list
  • _as_text — string

Each validates with isinstance and raises TypeError otherwise, so the assertions stay exactly as they were but no longer need an escape hatch. check_no_new_cast is green. Pushed as 2f3b6f1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants