Skip to content

[ty] Tell the user why we inferred a certain Python version when reporting version-specific syntax errors #18295

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 4 commits into from
May 26, 2025

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented May 25, 2025

Summary

This PR improves our reporting of version-specific syntax errors in ty so that we inform the user why we inferred a certain Python version when parsing Python syntax in their project.

Screenshot demo

image

## Implementation

I had to add a couple of AsMut implementations so that the function adding the subdiagnostic would be able to accept both a mut LintDiagnosticGuard (if called from within the ty_python_semantic crate) or a mut Diagnostic (if called from within the ty crate). The existing DerefMut<Target=Diagnostic> implementation that LintDiagnosticGuard has is insufficient, because Diagnostic does not (and cannot) implement DerefMut<Target=Diagnostic>. It felt odd adding AsMut implementations without also adding AsRef implementations, so I also added these for consistency, although they are not strictly needed.

Test Plan

I added a snapshot test to the ty crate

@AlexWaygood AlexWaygood requested review from BurntSushi and ntBre May 25, 2025 10:44
@AlexWaygood AlexWaygood added ty Multi-file analysis & type inference diagnostics Related to reporting of diagnostics. labels May 25, 2025
Copy link
Contributor

github-actions bot commented May 25, 2025

mypy_primer results

No ecosystem changes detected ✅

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Nice, this makes sense to me!

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Nice

@AlexWaygood AlexWaygood enabled auto-merge (squash) May 26, 2025 20:38
@AlexWaygood AlexWaygood force-pushed the alex/syntax-error-hint branch from 0f8f882 to 08db324 Compare May 26, 2025 20:41
@AlexWaygood AlexWaygood merged commit 6453ac9 into main May 26, 2025
34 checks passed
@AlexWaygood AlexWaygood deleted the alex/syntax-error-hint branch May 26, 2025 20:44
carljm added a commit to MatthewMckee4/ruff that referenced this pull request May 28, 2025
* main: (246 commits)
  [ty] Simplify signature types, use them in `CallableType` (astral-sh#18344)
  [ty] Support ephemeral uv virtual environments (astral-sh#18335)
  Add a `ViolationMetadata::rule` method (astral-sh#18234)
  Return `DiagnosticGuard` from `Checker::report_diagnostic` (astral-sh#18232)
  [flake8_use_pathlib]: Replace os.symlink with Path.symlink_to (PTH211) (astral-sh#18337)
  [ty] Support cancellation and retry in the server (astral-sh#18273)
  [ty] Synthetic function-like callables (astral-sh#18242)
  [ty] Support publishing diagnostics in the server (astral-sh#18309)
  Add Autofix for ISC003 (astral-sh#18256)
  [`pyupgrade`]: new rule UP050 (`useless-class-metaclass-type`) (astral-sh#18334)
  [pycodestyle] Make `E712` suggestion not assume a context (astral-sh#18328)
  put similar dunder-call tests next to each other (astral-sh#18343)
  [ty] Derive `PartialOrd, Ord` for `KnownInstanceType` (astral-sh#18340)
  [ty] Simplify `Type::try_bool()` (astral-sh#18342)
  [ty] Simplify `Type::normalized` slightly (astral-sh#18339)
  [ty] Move arviz off the list of selected primer projects (astral-sh#18336)
  [ty] Add --config-file CLI arg (astral-sh#18083)
  [ty] Tell the user why we inferred a certain Python version when reporting version-specific syntax errors (astral-sh#18295)
  [ty] Implement implicit inheritance from `Generic[]` for PEP-695 generic classes (astral-sh#18283)
  [ty] Add hint if async context manager is used in non-async with statement (astral-sh#18299)
  ...
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