Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented May 14, 2025

Summary

Add a link to this page when emitting invalid-type-expression diagnostics:

image

@sharkdp sharkdp added ty Multi-file analysis & type inference diagnostics Related to reporting of diagnostics. labels May 14, 2025
@AlexWaygood
Copy link
Member

Could you also add the link to this one:

KnownInstanceType::ClassVar | KnownInstanceType::Final => {
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
builder.into_diagnostic(format_args!(
"Type qualifier `{}` is not allowed in type expressions \
(only in annotation expressions)",
known_instance.repr(self.db())
));
}
self.infer_type_expression(arguments_slice)

@github-actions
Copy link
Contributor

github-actions bot commented May 14, 2025

mypy_primer results

No ecosystem changes detected ✅

@AlexWaygood
Copy link
Member

And maybe this one?

if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, parameters) {
builder.into_diagnostic(format_args!(
"The first argument to `Callable` \
must be either a list of types, \
ParamSpec, Concatenate, or `...`",
));
}

@AlexWaygood
Copy link
Member

And a link in the rule's docs themselves might also be great:

declare_lint! {
/// ## What it does
/// Checks for expressions that are used as type expressions
/// but cannot validly be interpreted as such.
///
/// ## Why is this bad?
/// Such expressions cannot be understood by ty.
/// In some cases, they might raise errors at runtime.
///
/// ## Examples
/// ```python
/// from typing import Annotated
///
/// a: type[1] # `1` is not a type
/// b: Annotated[int] # `Annotated` expects at least two arguments
/// ```
pub(crate) static INVALID_TYPE_FORM = {
summary: "detects invalid type forms",
status: LintStatus::preview("1.0.0"),
default_level: Level::Error,
}
}

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Thank you!!

@sharkdp sharkdp force-pushed the david/invalid-type-expression-diag-info branch from 4b003d9 to d2ecdf2 Compare May 14, 2025 18:51
@sharkdp sharkdp requested a review from MichaReiser as a code owner May 14, 2025 18:51
@sharkdp sharkdp force-pushed the david/invalid-type-expression-diag-info branch from d2ecdf2 to 7f95fe8 Compare May 14, 2025 18:53
@sharkdp sharkdp enabled auto-merge (squash) May 14, 2025 18:54
Comment on lines +12 to 13
use crate::types::LintDiagnosticGuard;
use crate::types::{protocol_class::ProtocolClassLiteral, KnownFunction, KnownInstanceType, Type};
Copy link
Member

@AlexWaygood AlexWaygood May 14, 2025

Choose a reason for hiding this comment

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

if you do this then maybe you don't get any line numbers changing in the generated docs :P

Suggested change
use crate::types::LintDiagnosticGuard;
use crate::types::{protocol_class::ProtocolClassLiteral, KnownFunction, KnownInstanceType, Type};
use crate::types::{protocol_class::ProtocolClassLiteral, KnownFunction, KnownInstanceType, LintDiagnosticGuard, Type};

@sharkdp sharkdp merged commit 6800a9f into main May 14, 2025
31 checks passed
@sharkdp sharkdp deleted the david/invalid-type-expression-diag-info branch May 14, 2025 18:56
dcreager added a commit that referenced this pull request May 14, 2025
…rals

* origin/main:
  [ty] Add type-expression syntax link to invalid-type-expression (#18104)
  [`flake8-simplify`] add fix safety section (`SIM103`) (#18086)
  [ty] mypy_primer: fix static-frame setup (#18103)
  [`flake8-simplify`] Correct behavior for `str.split`/`rsplit` with `maxsplit=0` (`SIM905`) (#18075)
  [ty] Fix more generics-related TODOs (#18062)
Glyphack pushed a commit to Glyphack/ruff that referenced this pull request May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics Related to reporting of diagnostics. ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants