Skip to content

test(api): bring the completion app generator tests under strict pyrefly - #46401

Open
MindOfqc wants to merge 5 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-completion-app-generator
Open

MindOfqc wants to merge 5 commits into
langgenius:mainfrom
MindOfqc:test/pyrefly-completion-app-generator

Conversation

@MindOfqc

Copy link
Copy Markdown
Contributor

Why

core/app/apps/completion/test_app_generator.py is listed under "Existing strict-mode debt" in api/tests/unit_tests/pyrefly.toml. This PR pays that off: the file leaves project-excludes and is checked by make type-check-core from now on.

Before: 18 errors + 3 warnings. After: 0.

project-excludes goes from 199 to 198 entries.

How

  • Conversation.invoke_from and Message.invoke_from are typed with models.enums.InvokeFrom, so the fixtures now construct those rows with that enum while the generator call keeps the core enum it declares. Both are StrEnum with identical values.
  • The fixture no longer assigns generate_entity onto the generator instance. Nothing on CompletionAppGenerator or its bases reads that attribute — generate() calls the module-level entity directly — so the assignment was dead. The assertions now read the patched module-level factory, the same object the dead attribute held.
  • Two mocker.patch.object(...) results are bound and narrowed with assert isinstance(x, MagicMock): pytest-mock returns MockType, which does not declare assert_*.
  • The remaining diagnostics were missing -> None and missing fixture annotations.

One typed-dict key was dropped: pydantic_core.InitErrorDetails has no msg field, and the generated .errors() output is identical before and after.

How to Test

cd api
uv run --project . pyrefly check --config tests/unit_tests/pyrefly.toml
uv run --project . pytest tests/unit_tests/core/app/apps/completion/test_app_generator.py

Local verification (whole tests/unit_tests project, with this entry removed from the config):

  • ERROR=29, all missing-import for optional dependencies absent from this machine; WARN=0; zero diagnostics left in the target file.
  • pytest: 10 passed both before and after the change.
  • ruff check / ruff format --check: clean.
  • No # pyrefly: ignore, no # type: ignore, no typing.cast, no typing.Any.

Annotate the generator fixture and its tests so
`tests/unit_tests/pyrefly.toml` no longer excludes
`core/app/apps/completion/test_app_generator.py`.

`Conversation.invoke_from` and `Message.invoke_from` are typed with the model
enum, so the fixtures now pass `models.enums.InvokeFrom`; the generator call
keeps the core enum it declares.

The fixture no longer assigns `generate_entity` onto the generator: nothing on
`CompletionAppGenerator` or its bases reads that attribute, `generate()` calls
the module-level entity directly. The assertions now read the patched
module-level factory, which is the same object the dead attribute held.
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-26 06:07:04.246305486 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-26 06:06:53.906201857 +0000
@@ -3100,18 +3100,6 @@
     --> tests/unit_tests/core/app/apps/common/test_workflow_response_converter.py:1477:9
 ERROR Object of type `FileInputConfig | FileListInputConfig | ParagraphInputConfig | SelectInputConfig` has no attribute `option_source` [missing-attribute]
     --> tests/unit_tests/core/app/apps/common/test_workflow_response_converter.py:1877:16
-ERROR Object of class `CompletionAppGenerator` has no attribute `generate_entity` [missing-attribute]
-  --> tests/unit_tests/core/app/apps/completion/test_app_generator.py:62:5
-ERROR Argument `Literal[core.app.entities.app_invoke_entities.InvokeFrom.WEB_APP]` is not assignable to parameter `invoke_from` with type `models.enums.InvokeFrom | SQLCoreOperations[models.enums.InvokeFrom | None] | None` in function `models.model.Conversation.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/app/apps/completion/test_app_generator.py:114:21
-ERROR Argument `Literal[core.app.entities.app_invoke_entities.InvokeFrom.WEB_APP]` is not assignable to parameter `invoke_from` with type `models.enums.InvokeFrom | SQLCoreOperations[models.enums.InvokeFrom | None] | None` in function `models.model.Message.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/app/apps/completion/test_app_generator.py:142:21
-ERROR Object of class `FunctionType` has no attribute `assert_not_called` [missing-attribute]
-   --> tests/unit_tests/core/app/apps/completion/test_app_generator.py:253:9
-ERROR Object of class `FunctionType` has no attribute `assert_called_once` [missing-attribute]
-   --> tests/unit_tests/core/app/apps/completion/test_app_generator.py:309:9
-ERROR Key `msg` is not defined in TypedDict `InitErrorDetails` [bad-typed-dict-key]
-   --> tests/unit_tests/core/app/apps/completion/test_app_generator.py:376:57
 ERROR Argument `Literal['bad']` is not assignable to parameter `metadata` with type `Mapping[str, object]` in function `core.app.entities.task_entities.Data.model_construct` [bad-argument-type]
    --> tests/unit_tests/core/app/apps/completion/test_generate_response_converter.py:100:22
 ERROR Cannot index into `str` [bad-index]
@@ -3182,6 +3170,8 @@
    --> tests/unit_tests/core/app/apps/test_base_app_runner.py:477:72
 ERROR Missing argument `task_id` in function `core.app.entities.app_invoke_entities.EasyUIBasedAppGenerateEntity.model_construct` [missing-argument]
    --> tests/unit_tests/core/app/apps/test_base_app_runner.py:478:75
+ERROR Missing argument `app_config` in function `core.app.entities.app_invoke_entities.EasyUIBasedAppGenerateEntity.model_construct` [missing-argument]
+   --> tests/unit_tests/core/app/apps/test_base_app_runner.py:478:75
 ERROR Missing argument `inputs` in function `core.app.entities.app_invoke_entities.EasyUIBasedAppGenerateEntity.model_construct` [missing-argument]
    --> tests/unit_tests/core/app/apps/test_base_app_runner.py:478:75
 ERROR Missing argument `files` in function `core.app.entities.app_invoke_entities.EasyUIBasedAppGenerateEntity.model_construct` [missing-argument]

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 87.86% 87.88% +0.01%
Strict coverage 87.53% 87.54% +0.01%
Typed symbols 97,148 97,163 +15
Untyped symbols 13,469 13,455 -14
Modules 4669 4669 0

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.46%. Comparing base (e07c1f4) to head (15117d4).
⚠️ Report is 423 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #46401      +/-   ##
==========================================
+ Coverage   90.40%   90.46%   +0.05%     
==========================================
  Files        6334     6400      +66     
  Lines      396668   406962   +10294     
  Branches    79710    81042    +1332     
==========================================
+ Hits       358626   368150    +9524     
- Misses      31934    32427     +493     
- Partials     6108     6385     +277     
Flag Coverage Δ
api 90.20% <ø> (+0.10%) ⬆️

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.

mocker.patch.object(generator, "_get_app_model_config", return_value=app_model_config)
mocker.patch.object(AppModelConfig, "to_dict", return_value={"model": {"provider": "x"}})

file_extra_config = MagicMock()

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.

real class ?

mocker.patch.object(module.FileUploadConfigManager, "convert", return_value=file_extra_config)
mocker.patch.object(module.file_factory, "build_from_mappings", return_value=["file1"])
build_from_mappings = mocker.patch.object(module.file_factory, "build_from_mappings", return_value=["file1"])
assert isinstance(build_from_mappings, MagicMock)

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.

.

error: Exception,
should_publish: bool,
) -> None:
flask_app = MagicMock()

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.

.

module, "CompletionAppGenerateEntity", side_effect=lambda **kwargs: SimpleNamespace(**kwargs)
)
gen.generate_entity = generate_entity
mocker.patch.object(module, "CompletionAppGenerateEntity", side_effect=lambda **kwargs: SimpleNamespace(**kwargs))

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.

.

constructions live on that module attribute rather than on the generator.
"""
entity: object = module.CompletionAppGenerateEntity
assert isinstance(entity, MagicMock)

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.

.

@asukaminato0721 asukaminato0721 self-assigned this Nov 15, 2026
The generate entity declared `app_config: EasyUIBasedAppConfig = None` behind a
`type: ignore`, so every construction of an EasyUI entity read as untyped and the
tests had to substitute a mock factory to observe the construction at all.

Every EasyUI construction site already passes a real `EasyUIBasedAppConfig`
subclass, so the field can be required and correctly typed. The completion app
generator test now builds the real entity and reads it back from the recorded
`_init_generate_records` call instead of patching the entity class.
@github-actions github-actions Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Nov 15, 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.

2 inline suggestions at 8c33bfe442, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 16, 2026
The completion generator tests stubbed out the two pieces of real logic they
were meant to protect: `file_factory.build_from_mappings` and
`ModelConfigConverter.convert`. The assertions therefore described the stubs,
not the behaviour, and every mutation of either survived.

Drop both stubs. The file case now stores a real `UploadFile` row and lets the
factory read it back through the session; the debugger case builds a real
`ProviderManager` with a real predefined-model catalog and only replaces the
outermost daemon HTTP boundary, so credential resolution, schema lookup and
parameter normalisation all run for real.

Postconditions the assertions now protect, each verified by mutating the
production code and confirming the test fails:
  * invalid caller mappings are dropped instead of raising;
  * the built file carries the stored key, source URL and record reference;
  * stop words are lifted out of the completion parameters;
  * the mode comes from the real model schema when the app config omits it;
  * credentials come from the resolved provider bundle.

Checks: 10 passed; six mutations all caught; ruff check/format clean; pyrefly
reports no diagnostic for this file in any of the three configs; all eight
ast-grep guards pass.

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

2 inline suggestions at f0de2ba69c, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong. 5 review threads are still unresolved.

dify-codereview

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 17, 2026
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 26, 2026
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. 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.

2 participants