Skip to content

Conversation

MichaReiser
Copy link
Member

Summary

This PR adds support for file-level type: ignore comments

A # type: ignore comment on a line by itself at the top of a file, before any docstrings, imports, or other executable code, silences all errors in the file. Blank lines and other comments, such as shebang lines and coding cookies, may precede the # type: ignore comment. source

I intentionally didn't enable this behavior for knot: ignore comments because we may want to introduce a distinct syntax that not only allows suppressing errors but also enabling rules or changing the rule's severity: knot: possibly-undefined-variable="warn"

Test Plan

Added mtests

@MichaReiser MichaReiser added the ty Multi-file analysis & type inference label Dec 20, 2024
Comment on lines +181 to +198
let (suppressions, suppressed_range) =
// `type: ignore` comments at the start of the file apply to the entire range.
// > A # type: ignore comment on a line by itself at the top of a file, before any docstrings,
// > imports, or other executable code, silences all errors in the file.
// > Blank lines and other comments, such as shebang lines and coding cookies,
// > may precede the # type: ignore comment.
// > https://typing.readthedocs.io/en/latest/spec/directives.html#type-ignore-comments
if comment.kind.is_type_ignore() && !self.seen_non_trivia_token {
(
&mut self.file,
TextRange::new(0.into(), self.source.text_len()),
)
} else {
(
&mut self.line,
TextRange::new(line_start, comment.range.end()),
)
};
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the only new code in this block. Everything else was extracted from the suppressions functions

@MichaReiser MichaReiser force-pushed the micha/file-level-type-ignore branch from 7af6641 to 357825e Compare December 20, 2024 13:00
@MichaReiser MichaReiser marked this pull request as ready for review December 20, 2024 13:01
Copy link
Contributor

github-actions bot commented Dec 20, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser force-pushed the micha/file-level-type-ignore branch from 357825e to ec700b8 Compare December 21, 2024 16:41
including module docstrings.
"""

# type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this emit a diagnostic? Or not necessary, because it just won't work and that will be obvious from other diagnostics in the file?

Copy link
Member Author

Choose a reason for hiding this comment

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

Base automatically changed from micha/ignore-codes to main December 23, 2024 09:52
@MichaReiser MichaReiser force-pushed the micha/file-level-type-ignore branch from ec700b8 to 788bfd2 Compare December 23, 2024 09:53
@MichaReiser MichaReiser enabled auto-merge (squash) December 23, 2024 09:56
@MichaReiser MichaReiser merged commit 1c3d11e into main Dec 23, 2024
20 checks passed
@MichaReiser MichaReiser deleted the micha/file-level-type-ignore branch December 23, 2024 09:59
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.

2 participants