Skip to content

[2/5] Type-check assertions #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 14, 2025
Merged

[2/5] Type-check assertions #14

merged 5 commits into from
Jul 14, 2025

Conversation

dandavison
Copy link
Contributor

@dandavison dandavison commented Jul 12, 2025

This is PR 2/5 in a stack.

Update the library for making type-check failure assertions so that it supports making assertions for multiple type checkers.

The main point of this PR is the changes to the library for type-check assertions test_type_errors.py

@dandavison dandavison force-pushed the typing-cleanup branch 2 times, most recently from 31c31f3 to 58d2a77 Compare July 12, 2025 20:38
@dandavison dandavison changed the title Type assertions [2/4] Type-check assertions Jul 12, 2025
@dandavison dandavison force-pushed the type-assertions branch 5 times, most recently from 912ea11 to 97376d2 Compare July 12, 2025 22:08
@dandavison dandavison force-pushed the type-assertions branch 3 times, most recently from 25aebe9 to 12ceb41 Compare July 13, 2025 18:50
@@ -39,8 +39,8 @@ packages = ["src/nexusrpc"]

[tool.poe.tasks]
lint = [
{cmd = "uv run basedpyright src"},
{cmd = "uv run pyright src"},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test suite passes type-checking after this PR.

@dandavison dandavison changed the title [2/4] Type-check assertions [2/5] Type-check assertions Jul 14, 2025
Copy link

@cretz cretz left a comment

Choose a reason for hiding this comment

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

Not applying much scrutiny to tests here, but did mention in a previous PR that reportDeprecated is noisy on our PRs here

@dandavison
Copy link
Contributor Author

dandavison commented Jul 14, 2025

Thanks!

Not applying much scrutiny to tests here,

You might want to look at test_type_errors.py -- it's not a test, but rather a framework for making assertions about type checker error diagnostics.

Base automatically changed from typing-cleanup to main July 14, 2025 15:54
@cretz
Copy link

cretz commented Jul 14, 2025

You might want to look at test_type_errors.py -- it's not a test, but rather a framework for making assertions about type checker error diagnostics.

Will look...

Comment on lines 61 to 62
# This test is disabled since we currently have no way to be able to
# assert-type-error-mypy on a line with a `type: ignore`.
Copy link

Choose a reason for hiding this comment

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

Can't say I understand this, but if this is dead code, can we remove it or open an issue to follow up on it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Deleted. The issue is that:

  • We would like to be able to make an assertion that a line produces a certain type error
  • We would also like the test code to pass type checking
  • For pyright, the current solution is to place the type: ignore on the line in question but instruct pyright to ignore it when performing the type error assertion check.
  • For mypy we have no such solution

There are probably other solutions, e.g. entirely excluding such files from our type checking in CI. I'll look into that: I think that may work because the type assertion checker does check that no there are no type errors other than the expected ones. But let's merge this as-is if that's OK, i.e. with just pyright support.

metafunc.parametrize("test_file", files_with_assertions, ids=lambda f: f.name)


@pytest.mark.skipif(platform.system() == "Windows", reason="TODO: broken on Windows")
Copy link

Choose a reason for hiding this comment

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

Can we open an issue to address this TODO so we don't lose track?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes good call, done #19. There should be no technical obstacle there; I just failed to solve it within the time I had allotted to the task.

import pytest


def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
Copy link

Choose a reason for hiding this comment

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

I bit confused here. How is this used? It seems to look for certain function names but ignores them if they are in this file but I can find them nowhere else? Is this code not leveraged in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a pytest hook https://docs.pytest.org/en/stable/how-to/parametrize.html#pytest-generate-tests. Here we're using it to dynamically generate a test for every test file that contains type error assertion comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It generates a test like this (we only have those comments in one file at the moment):

tests/test_type_errors.py::test_type_errors_pyright[test_type_checking_sync_operation.py] PASSED 

AI: This commit should have tag name type-assertions and should be
force-pushed to a branch of that name as necessary.
@dandavison dandavison requested a review from cretz July 14, 2025 17:49
@dandavison dandavison merged commit c8d54f8 into main Jul 14, 2025
10 checks passed
@dandavison dandavison deleted the type-assertions branch July 14, 2025 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants