Skip to content

[red-knot] Add support for assert_never #17287

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 1 commit into from
Apr 8, 2025
Merged

[red-knot] Add support for assert_never #17287

merged 1 commit into from
Apr 8, 2025

Conversation

sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Apr 7, 2025

Summary

We already have partial "support" for assert_never, because it is annotated as

def assert_never(arg: Never, /) -> Never: ...

in typeshed. So we already emit a invalid-argument-type diagnostic if the argument type to assert_never is not assignable to Never.

That is not enough, however. Gradual types like Any, Unknown, @Todo(…) or Any & int can be assignable to Never. Which means that we didn't issue any diagnostic in those cases.

Also, it seems like assert_never deserves a dedicated diagnostic message, not just a generic "invalid argument type" error.

Test Plan

New Markdown tests.

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Apr 7, 2025
@@ -682,7 +682,7 @@ declare_lint! {

declare_lint! {
/// ## What it does
/// Checks for `assert_type()` calls where the actual type
/// Checks for `assert_type()` and `assert_never()` calls where the actual type
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did consider making more changes in the lint description here. I also considered a dedicated new diagnostic. But eventually thought that this might be enough? Let me know if you have a different opinion.

Copy link
Contributor

github-actions bot commented Apr 7, 2025

mypy_primer results

No ecosystem changes detected ✅

@sharkdp sharkdp force-pushed the david/assert-never branch 2 times, most recently from 433494f to 9ce9a1d Compare April 7, 2025 21:00
@sharkdp sharkdp force-pushed the david/assert-never branch from 9ce9a1d to 4dc402e Compare April 7, 2025 21:03
@sharkdp sharkdp merged commit b662c3f into main Apr 8, 2025
23 checks passed
@sharkdp sharkdp deleted the david/assert-never branch April 8, 2025 07:31
dcreager added a commit that referenced this pull request Apr 9, 2025
* origin/main:
  [red-knot] Default `python-platform` to current platform (#17183)
  [red-knot] Add new 'unreachable code' test case (#17306)
  [red-knot] mypy_primer: Run on `async-utils` (#17303)
  [red-knot] Add custom `__setattr__` support (#16748)
  [red-knot] Add `__init__` arguments check when doing `try_call` on a class literal (#16512)
  [`flake8-pie`] Avoid false positive for multiple assignment with `auto()` (`PIE796`) (#17274)
  [syntax-errors] Async comprehension in sync comprehension (#17177)
  [`airflow`] Expand module path check to individual symbols (`AIR302`) (#17278)
  [syntax-errors] Check annotations in annotated assignments (#17283)
  [syntax-errors] Extend annotation checks to `await` (#17282)
  [red-knot] Add support for `assert_never` (#17287)
  [`flake8-pytest-style`] Avoid false positive for legacy form of `pytest.raises` (`PT011`) (#17231)
  [red-knot] Do not show types for literal expressions on hover (#17290)
  [red-knot] Fix dead-code clippy warning (#17291)
  [red-knot] Reachability analysis (#17199)
  [red-knot] Don't use latency-sensitive for handlers (#17227)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants