Skip to content

Conversation

@intgr
Copy link
Contributor

@intgr intgr commented Oct 21, 2025

I tried to convert existing tests with minimal changes -- such that in many cases "git blame" can still find the commit where the original .yml tests were added.

Tests written in regular .py files using assert_type() have lots of advantages compared to pytest-mypy-plugins .yml-format tests:

  • MUCH faster to run
  • Easier to write/maintain: autoformatting, auto-fixes provided by Ruff. IDE allows auto-adding imports, etc.
  • Supported by multiple typecheckers, not just mypy (though there is no plan to run other typecheckers for now)

However, not all tests can be converted -- pytest-mypy-plugins tests are still useful for more complex cases:

  • Tests using multiple files (e.g. separate models.py)
  • Tests checking that mypy correctly raises certain error messages.
  • Parameterized tests.

@intgr intgr force-pushed the add-assert_type-tests branch from 381da44 to 35ddf78 Compare October 21, 2025 19:13
@intgr intgr closed this Oct 21, 2025
@intgr intgr reopened this Oct 21, 2025
@intgr intgr force-pushed the add-assert_type-tests branch 4 times, most recently from 61baa73 to 5db5e09 Compare October 21, 2025 19:29

[tool.ruff.lint.flake8-tidy-imports.banned-api]
"_typeshed.Self".msg = "Use typing_extensions.Self (PEP 673) instead."
"typing.assert_type".msg = "Use typing_extensions.assert_type instead."
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We still typecheck with Python 3.10, which doesn't have typing.assert_type

@intgr intgr requested a review from Copilot October 21, 2025 20:05
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR converts simpler typecheck tests from pytest-mypy-plugins .yml format to regular Python files using assert_type(). The conversion maintains test coverage while improving test performance, maintainability, and developer experience through IDE support and autoformatting.

Key changes:

  • Migrated 50+ test cases from .yml files to .py files using assert_type()
  • Retained complex tests (multi-file, error message validation, parameterized) in .yml format
  • Added mypy validation of test files to CI workflow

Reviewed Changes

Copilot reviewed 27 out of 28 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/typecheck/test_views.yml Removed 5 simple test cases (e.g., request types, generics)
tests/typecheck/test_test.yml Removed entire file containing client API test cases
tests/typecheck/test_serializers.yml Removed 10 test cases for serializer functionality
tests/typecheck/test_routers.yml Removed entire file containing router URL tests
tests/typecheck/test_request.yml Removed entire file containing request querydict tests
tests/typecheck/test_pagination.yml Removed entire file containing pagination tests
tests/typecheck/test_mixins.yml Removed entire file containing mixin tests
tests/typecheck/test_filters.yml Removed entire file containing filter backend tests
tests/typecheck/test_fields.yml Removed 2 test cases for field functionality
tests/typecheck/test_exceptions.yml Removed 3 test cases for exception declarations
tests/typecheck/test_decorators.yml Removed 4 test cases, kept error validation tests
tests/typecheck/test_compat.yml Removed entire file containing compatibility tests
tests/typecheck/test_api_client.yml Removed entire file containing API client tests
tests/assert_type/views.py Added converted view-related tests
tests/assert_type/test.py Added converted test client tests
tests/assert_type/serializers.py Added converted serializer tests
tests/assert_type/routers.py Added converted router tests
tests/assert_type/request.py Added converted request tests
tests/assert_type/pagination.py Added converted pagination tests
tests/assert_type/mixins.py Added converted mixin tests
tests/assert_type/filters.py Added converted filter tests
tests/assert_type/fields.py Added converted field tests
tests/assert_type/exceptons.py Added converted exception tests (note: filename typo)
tests/assert_type/decorators.py Added converted decorator tests
tests/assert_type/compat.py Added converted compatibility tests
pyproject.toml Added linter exceptions and banned API for assert_type tests
.github/workflows/test.yml Added mypy validation step for test files

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@intgr intgr force-pushed the add-assert_type-tests branch from 5db5e09 to 35b5792 Compare October 21, 2025 20:26
@intgr intgr requested a review from sobolevn October 22, 2025 20:42
Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

I am ok with this, but I didn't review all the test changes, I hope that they are the same :)

@intgr intgr merged commit eee75f4 into typeddjango:master Oct 22, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants