Skip to content

refactor: add type hints to untyped functions in api/libs/helper.py - #46709

Closed
Cmz9111 wants to merge 1 commit into
langgenius:mainfrom
Cmz9111:fix/helper-type-hints
Closed

Cmz9111 wants to merge 1 commit into
langgenius:mainfrom
Cmz9111:fix/helper-type-hints

Conversation

@Cmz9111

@Cmz9111 Cmz9111 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Problem

Several utility and validation functions in api/libs/helper.py were missing type hints for their parameters and return types. This reduces code readability and prevents static type checkers from catching type-related bugs.

The following functions/methods lacked type hints:

  • email(email)
  • run(script)
  • timestamp_value(timestamp)
  • timezone(timezone_string)
  • generate_string(n)
  • StrLen.__init__ and StrLen.__call__
  • DatetimeString.__init__ and DatetimeString.__call__

Solution

Added appropriate type hints to all listed functions and methods:

# Before
def email(email):
def run(script):
def timestamp_value(timestamp):
def timezone(timezone_string):
def generate_string(n):

# After
def email(email: str) -> str:
def run(script: str) -> tuple[int, str]:
def timestamp_value(timestamp: Any) -> int:
def timezone(timezone_string: str) -> str:
def generate_string(n: int) -> str:

Also added type hints to StrLen and DatetimeString class constructors and __call__ methods.

Testing

  • Verified all changes are purely type annotation additions with no behavioral changes
  • Confirmed Any is already imported in the file (used by other functions)
  • Ensured type hints are consistent with existing typed functions in the same file

@github-actions github-actions Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Nov 18, 2026
@asukaminato0721 asukaminato0721 self-assigned this Nov 19, 2026

@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.

1 inline suggestion at e50af88776, from a rule learned from earlier reviews in this repository. Reply on the comment if it is wrong.

dify-codereview

Comment thread api/libs/helper.py


def timestamp_value(timestamp):
def timestamp_value(timestamp: Any) -> int:

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.

Please use the precise type here, not object / Any: e.g. -> str, -> Session, Callable[...]; for a parametrized test, the union of the case types (or a type alias for it); for __exit__, exc_type: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None.

dify-codereview · precise-types (concrete-type) Learned from an earlier review. Reply here if this is wrong.

@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 19, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-19 06:01:25.474684438 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-19 06:01:08.014449341 +0000
@@ -3766,6 +3766,52 @@
    --> tests/unit_tests/core/mcp/server/test_streamable_http.py:390:16
 ERROR Argument `Literal['invalid_mode']` is not assignable to parameter `mode` with type `AppMode | SQLCoreOperations[AppMode]` in function `models.model.App.__init__` [bad-argument-type]
    --> tests/unit_tests/core/mcp/server/test_streamable_http.py:609:18
+ERROR `MockNotificationParams` is not assignable to upper bound `NotificationParams | dict[str, Any] | None` of type variable `NotificationParamsT` [bad-specialization]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:51:24
+ERROR `MockRequest` is not assignable to upper bound `ClientRequest | ServerRequest` of type variable `SendRequestT` [bad-specialization]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:64:19
+ERROR `MockNotification` is not assignable to upper bound `ClientNotification | ServerNotification` of type variable `SendNotificationT` [bad-specialization]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:64:19
+ERROR `MockResult` is not assignable to upper bound `ClientResult | ServerResult` of type variable `SendResultT` [bad-specialization]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:64:19
+ERROR `ReceiveRequest` is not assignable to upper bound `ClientRequest | ServerRequest` of type variable `ReceiveRequestT` [bad-specialization]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:64:19
+ERROR `ReceiveNotification` is not assignable to upper bound `ClientNotification | ServerNotification` of type variable `ReceiveNotificationT` [bad-specialization]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:64:19
+ERROR Argument `object` is not assignable to parameter `read_stream` with type `Queue[Unknown]` in function `core.mcp.session.base_session.BaseSession.__init__` [bad-argument-type]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:66:26
+ERROR Argument `object` is not assignable to parameter `write_stream` with type `Queue[Unknown]` in function `core.mcp.session.base_session.BaseSession.__init__` [bad-argument-type]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:66:26
+ERROR Argument `object` is not assignable to parameter `receive_request_type` with type `type[ReceiveRequest]` in function `core.mcp.session.base_session.BaseSession.__init__` [bad-argument-type]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:66:26
+ERROR Argument `object` is not assignable to parameter `receive_notification_type` with type `type[ReceiveNotification]` in function `core.mcp.session.base_session.BaseSession.__init__` [bad-argument-type]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:66:26
+ERROR Argument `object` is not assignable to parameter `read_timeout_seconds` with type `timedelta | None` in function `core.mcp.session.base_session.BaseSession.__init__` [bad-argument-type]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:66:26
+ERROR Class member `MockSession._received_request` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:71:9
+ERROR Class member `MockSession._received_notification` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:74:9
+ERROR Class member `MockSession._handle_incoming` overrides parent class `BaseSession` in an inconsistent manner [bad-override-param-name]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:77:9
+ERROR Class member `MockSession._handle_incoming` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:77:9
+ERROR `ReceiveRequest` is not assignable to upper bound `ClientRequest | ServerRequest` of type variable `ReceiveRequestT` [bad-specialization]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:93:33
+ERROR `MockResult` is not assignable to upper bound `ClientResult | ServerResult` of type variable `SendResultT` [bad-specialization]
+  --> tests/unit_tests/core/mcp/session/test_base_session.py:93:33
+ERROR `ReceiveRequest` is not assignable to upper bound `ClientRequest | ServerRequest` of type variable `ReceiveRequestT` [bad-specialization]
+   --> tests/unit_tests/core/mcp/session/test_base_session.py:120:33
+ERROR `MockResult` is not assignable to upper bound `ClientResult | ServerResult` of type variable `SendResultT` [bad-specialization]
+   --> tests/unit_tests/core/mcp/session/test_base_session.py:120:33
+ERROR Object of class `NoneType` has no attribute `result` [missing-attribute]
+   --> tests/unit_tests/core/mcp/session/test_base_session.py:148:5
+ERROR Object of class `NoneType` has no attribute `done` [missing-attribute]
+   --> tests/unit_tests/core/mcp/session/test_base_session.py:149:12
+ERROR `ReceiveRequest` is not assignable to upper bound `ClientRequest | ServerRequest` of type variable `ReceiveRequestT` [bad-specialization]
+   --> tests/unit_tests/core/mcp/session/test_base_session.py:594:26
+ERROR `ReceiveNotification` is not assignable to upper bound `ClientNotification | ServerNotification` of type variable `ReceiveNotificationT` [bad-specialization]
+   --> tests/unit_tests/core/mcp/session/test_base_session.py:594:26
 ERROR Argument `(c: Unknown, p: Unknown) -> None` is not assignable to parameter `sampling_callback` with type `SamplingFnT | None` in function `core.mcp.session.client_session.ClientSession.__init__` [bad-argument-type]
    --> tests/unit_tests/core/mcp/session/test_client_session.py:570:58
 ERROR Argument `(c: Unknown) -> None` is not assignable to parameter `list_roots_callback` with type `ListRootsFnT | None` in function `core.mcp.session.client_session.ClientSession.__init__` [bad-argument-type]
@@ -5181,6 +5227,18 @@
     --> tests/unit_tests/services/test_agent_config_service.py:1180:12
 ERROR Type `object` is not iterable [not-iterable]
     --> tests/unit_tests/services/test_agent_config_service.py:1216:38
+ERROR Argument `AppMode | str` is not assignable to parameter `mode` with type `AppMode | SQLCoreOperations[AppMode]` in function `models.model.App.__init__` [bad-argument-type]
+  --> tests/unit_tests/services/test_app_generate_service.py:84:14
+ERROR Class member `_TrackingRateLimit.exit` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
+   --> tests/unit_tests/services/test_app_generate_service.py:869:21
+ERROR Class member `_TrackingRateLimit.exit` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
+   --> tests/unit_tests/services/test_app_generate_service.py:905:21
+ERROR Class member `_TrackingRateLimit.exit` overrides a member in a parent class but is missing an `@override` decorator [missing-override-decorator]
+   --> tests/unit_tests/services/test_app_generate_service.py:939:21
+ERROR Argument `_FakeStreams` is not assignable to parameter `redis_client` with type `Redis[Unknown] | RedisCluster[Unknown]` in function `libs.broadcast_channel.redis.streams_channel.StreamsBroadcastChannel.__init__` [bad-argument-type]
+    --> tests/unit_tests/services/test_app_generate_service.py:1275:40
+ERROR Argument `_FakeRedisClient` is not assignable to parameter `redis_client` with type `Redis[Unknown] | RedisCluster[Unknown]` in function `libs.broadcast_channel.redis.pubsub_channel.BroadcastChannel.__init__` [bad-argument-type]
+    --> tests/unit_tests/services/test_app_generate_service.py:1293:38
 ERROR Argument `str` is not assignable to parameter `freeze_type` with type `Literal['', 'email_domain_suspended', 'freeze']` in function `services.billing_client.saas.CheckAccountInFreezeReply.model_construct` [bad-argument-type]
    --> tests/unit_tests/services/test_billing_service.py:373:87
 ERROR Argument `Literal['normal']` is not assignable to parameter `status` with type `ConversationStatus | SQLCoreOperations[ConversationStatus]` in function `models.model.Conversation.__init__` [bad-argument-type]

@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 86.95% 86.88% -0.07%
Strict coverage 86.61% 86.54% -0.07%
Typed symbols 95,519 95,436 -83
Untyped symbols 14,391 14,470 +79
Modules 4631 4631 0

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.12%. Comparing base (dd990c3) to head (e50af88).
⚠️ Report is 502 commits behind head on main.

Files with missing lines Patch % Lines
api/libs/helper.py 50.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #46709      +/-   ##
==========================================
+ Coverage   89.88%   90.12%   +0.24%     
==========================================
  Files        6247     6365     +118     
  Lines      391170   403226   +12056     
  Branches    78443    79957    +1514     
==========================================
+ Hits       351619   363426   +11807     
+ Misses      33446    33398      -48     
- Partials     6105     6402     +297     
Flag Coverage Δ
api 90.08% <50.00%> (+0.40%) ⬆️

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.

@41tair

41tair commented Nov 19, 2026

Copy link
Copy Markdown
Member

I will close this PR because it can be easily merged into #46711.

@41tair 41tair closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files. waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants