Skip to content

Run mypy on the test suite - #6557

Open
SHANNWD wants to merge 5 commits into
zauberzeug:mainfrom
SHANNWD:chore/mypy-test-suite
Open

SHANNWD wants to merge 5 commits into
zauberzeug:mainfrom
SHANNWD:chore/mypy-test-suite

Conversation

@SHANNWD

@SHANNWD SHANNWD commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Motivation

Fixes #6549.

CI previously ran mypy on ./nicegui and ./_nicegui_worker.py only, leaving tests/ unchecked (383 errors across 60 test files on Python 3.11 / Linux). Enabling mypy across tests/ catches typing regressions in tests and test helpers automatically in CI.

Implementation

  • Test suite typing fixes (60 files in tests/):
    • Added assert <var> is not None guards for nonlocal page elements and screen_plugin.DOWNLOAD_DIR.
    • Replaced bare # type: ignore comments with specific mypy error codes (and removed an unused # type: ignore in tests/test_user_simulation.py).
    • Updated WebElement truthiness assertions to is not None, annotated empty containers (Event[Any], list[Exception], list[int], WeakSet[ui.label]), guarded nullable get_attribute(...) returns, replaced void-return tuple lambdas with small helper functions, and separated pandas/polars DataFrame variables in test_table.py and test_aggrid.py.
  • nicegui/testing/user.py: Added the missing User.find(target: str, *, kind: type[T], ...) @overload so calls like user.find('A', kind=ui.radio) type-check cleanly.
  • CI & Documentation: Updated .github/workflows/_check.yml, CONTRIBUTING.md, and AGENTS.md to run mypy ./nicegui ./_nicegui_worker.py tests.

Verification

  • uv run mypy --platform linux ./nicegui ./_nicegui_worker.py tests (Python 3.11): Success: no issues found in 419 source files
  • uv run ruff check tests/ nicegui/testing/user.py: All checks passed!
  • uv run pylint nicegui/testing/user.py: 10.00/10
  • uv run pytest (User-plugin tests including test_radio_click_with_user and test_validate_with_return_result_rejects_async_checks): passed

Progress

  • I chose a meaningful title that completes the sentence: "If applied, this PR will..."
  • The implementation is complete.
  • Verification checks (mypy, ruff, pylint, pytest) pass locally.
  • Documentation (CONTRIBUTING.md, AGENTS.md) and CI workflow (.github/workflows/_check.yml) are updated.
  • Disclosure: Assisted by an AI coding assistant; all changes were verified locally with mypy, ruff, pylint, and pytest.

@falkoschindler falkoschindler added the infrastructure Type/scope: GitHub Actions and IDE label Dec 2, 2026
@falkoschindler falkoschindler added this to the 4.2 milestone Dec 2, 2026
@falkoschindler

Copy link
Copy Markdown
Collaborator

Thanks for picking this up, @SHANNWD! I took an early look at the direction (not a full review yet), and it looks right to me: it follows the plan from #6549, and the changes stay close to what mypy asks for.

Some thoughts before you invest more time:

  • Guards vs. annotations: Most of the new assert x is not None lines follow a x = None / nonlocal x pair. Could you try a plain annotation without a value instead, e.g. label: ui.label before the page function, keeping nonlocal label inside? A small sample with this pattern passes mypy with our configuration, so most guards and the scene: ui.scene = None # type: ignore[assignment] lines might become unnecessary. New tests will copy whatever pattern we pick, so I'd prefer the one that needs a single line. Guards are fine where the value can really be None, like user.client.
  • CI: The mypy job fails on test_raising_the_minimum_keeps_the_value_above_it in tests/test_number.py, which landed on main with Keep a sanitized number inside the limits and on the precision grid #6546 after you branched off. Please merge main and include it. The pre-commit job fails because autopep8 modified files; uv run pre-commit run --all-files should fix that.
  • User.scope: It has the same missing overload (target: str with kind) that you added to User.find. Could you add it there as well?

※

@falkoschindler falkoschindler added the in progress Status: Someone is working on it label Dec 2, 2026
@SHANNWD

SHANNWD commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the early feedback, @falkoschindler! I've updated the PR with all three items:

  • Merged \main\ and updated the new tests in \ ests/test_number.py.
  • Added the matching \User.scope(target: str, *, kind: type[T], ...)\ overload in
    icegui/testing/user.py.
  • Replaced the \x = None\ /
    onlocal x\ + \�ssert x is not None\ and # type: ignore[assignment]\ patterns with plain single-line \x: Type\ annotations across the test suite.
  • Ran \uv run pre-commit run\ (fixing the \�utopep8\ formatting) and verified \mypy --platform linux ./nicegui ./_nicegui_worker.py tests\ passes with 0 errors across all 419 files.

@falkoschindler

Copy link
Copy Markdown
Collaborator

Thanks for the quick update, @SHANNWD! mypy and pre-commit are green now, and the annotations make the tests a lot more compact.

The remaining red check is not a flake: both CI runs fail on test_streamed_media_file_handle_is_released_on_teardown in tests/test_serving_files.py. The new response variable keeps the response alive, and with it the generator in response.body_iterator, so del generator followed by gc.collect() no longer releases the file handle. del generator, response should fix it. I derived this from the code and the CI log and have not run it, so please check it locally.

If you are at it: some assert x is not None guards are left after chained assignments like b = row = None in tests/test_element_delete.py, tests/test_element.py and tests/test_scene_view.py. Separate annotations should work there as well, except where the test really checks for None, like the wait_for(lambda: scene_view is not None) in tests/test_scene_view.py.

※

@SHANNWD

SHANNWD commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@falkoschindler Thanks for the review! I updated \ est_streamed_media_file_handle_is_released_on_teardown\ to also \del response, and split the chained = None\ variables in \ est_element.py, \ est_element_delete.py, and \ est_scene_view.py\ into separate type annotations. Both \pytest\ on the serving-files teardown test and \mypy --platform linux\ +
uff check\ pass with 0 errors.

This branch has not been deployed

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

Labels

in progress Status: Someone is working on it infrastructure Type/scope: GitHub Actions and IDE

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Run mypy on the test suite

2 participants