Skip to content

refactor(test): replace SimpleNamespace with real model instances in test_snippet.py#37679

Open
lavkeshdwivedi wants to merge 3 commits into
langgenius:mainfrom
lavkeshdwivedi:refactor/test-snippet-real-model-instances
Open

refactor(test): replace SimpleNamespace with real model instances in test_snippet.py#37679
lavkeshdwivedi wants to merge 3 commits into
langgenius:mainfrom
lavkeshdwivedi:refactor/test-snippet-real-model-instances

Conversation

@lavkeshdwivedi

Copy link
Copy Markdown

Summary

Addresses #37604.

test_snippet.py used SimpleNamespace to stand in for Workflow, Account, and Tag model instances. pyrefly flags these as type errors because SimpleNamespace does not satisfy the model's type annotations.

Changes

  • Workflow(graph=...) replaces SimpleNamespace(graph=...)
  • Account() with .id / .name set replaces SimpleNamespace(id=..., name=...)
  • Tag() with .id set replaces SimpleNamespace(id=...)
  • Mock(...) replaces SimpleNamespace(...) for session stubs (consistent with the rest of the test suite)

Why real instances, not Mock(spec=...)

Per maintainer guidance in #37604: real class instances, not mocks. Real instances let pyrefly verify that the attributes being accessed (.graph, .name, .id) are actual declared fields on the model.

Test plan

  • uv run pytest api/tests/unit_tests/models/test_snippet.py passes
  • uv run pyrefly check api/tests/unit_tests/models/test_snippet.py reports no SimpleNamespace-related errors

SQLAlchemy's CursorResult.rowcount can return None for some DBAPI
drivers/operations. Five bulk-delete methods used result.rowcount
directly in += or as a return value, causing:

  TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'

Apply the `result.rowcount or 0` pattern already used in sibling
methods (delete_by_runs, count_by_runs) across all five affected sites:

- sqlalchemy_api_workflow_node_execution_repository.py
  - delete_expired_executions
  - delete_executions_by_app
  - delete_executions_by_ids
- sqlalchemy_api_workflow_run_repository.py
  - delete_runs_by_ids
  - delete_runs_by_app

Fixes langgenius#37643
Covers all five affected methods to guard against regression:
- delete_expired_executions, delete_executions_by_app,
  delete_executions_by_ids (node execution repository)
- delete_runs_by_ids, delete_runs_by_app (run repository)

Each test mocks the DBAPI session to return rowcount=None and asserts
the method returns 0 instead of raising TypeError.
…test_snippet.py

Fixes the pyrefly type errors reported in langgenius#37604.

SimpleNamespace objects passed where Workflow, Account, and Tag are
expected do not satisfy the type checker. Replace with real ORM
instances so pyrefly can verify the fields being accessed actually
exist on the model.

Also replace SimpleNamespace session stubs with unittest.mock.Mock,
which is the standard pattern in the rest of the test suite.

Refs langgenius#37604
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. refactor labels Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant