Skip to content

Conversation

carljm
Copy link
Contributor

@carljm carljm commented Jul 24, 2025

Summary

See discussion at https://github.com/astral-sh/ruff/pull/19478/files#r2223870292

Fixes astral-sh/ty#865

Test Plan

Added one mdtest for invalid Callable annotation; removed pull-types: skip from that test file.

Co-authored-by: lipefree [email protected]

@carljm carljm added the ty Multi-file analysis & type inference label Jul 24, 2025
Copy link
Contributor

github-actions bot commented Jul 24, 2025

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

@carljm
Copy link
Contributor Author

carljm commented Jul 24, 2025

I don't know what's up with "CodSpeed Macro Runners" but I don't think it's related to this PR.

Copy link
Member

@dcreager dcreager left a comment

Choose a reason for hiding this comment

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

👍 I like this. There are definitely places where we're currently jumping through extra hoops to infer a type for every expression, but we can simplify those as needed moving forward.

This also opens the door for storing separate value form and type form types for each expression, I think? That would in turn let us simplify parts of the call binding machinery.

Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

This also resolves astral-sh/ty#865 (I tested this PR locally on the examples mentioned in that issue), it might be useful to add a couple of test cases from that issue.

@MichaReiser
Copy link
Member

I think it's worth a try and we can always come back if it makes tracking down bugs harder

What I would find useful is to add least log a tracing debug message if we take the fallback branch

@sharkdp
Copy link
Contributor

sharkdp commented Jul 24, 2025

What I would find useful is to add least log a tracing debug message if we take the fallback branch

I thought about that as well. In https://github.com/astral-sh/ruff/pull/19478/files#r2223870292, I suggested that we might only want to do that in valid code (and in this case, it could even be an error?). Because otherwise, if the Unknown fallback is now our preferred way of dealing with AST nodes inside invalid code, we would see a lot of these tracing messages (in the LSP), e.g. when the user is currently typing a annotation, but it's not yet valid?

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.

I agree that some logging for the fallback case might be good, but also that it might be hard to stop that from being very noisy in an IDE context. Even if you only emit the logs when there's valid syntax, there are lots of situations where you might have valid syntax but an invalid type expression when you're halfway through typing an annotation

@MichaReiser
Copy link
Member

MichaReiser commented Jul 24, 2025

If the Unknown fallback is now our preferred way of dealing with AST nodes inside invalid code, we would see a lot of these tracing messages (in the LSP), e.g. when the user is currently typing a annotation, but it's not yet valid?

That's a fair concern. I don't think it should impact most users because the LSP only shows info level logs by default.

@carljm carljm force-pushed the cjm/unknown-fallback branch from 4fa7e0e to 53103b3 Compare July 25, 2025 02:02
@carljm
Copy link
Contributor Author

carljm commented Jul 25, 2025

The tracing feels likely to be quite noisy to me, I would rather try without it -- if anyone finds themselves debugging a case like this and the tracing would have helped, we can add it.

@carljm carljm enabled auto-merge (squash) July 25, 2025 02:04
@carljm carljm merged commit ae9d450 into main Jul 25, 2025
36 checks passed
@carljm carljm deleted the cjm/unknown-fallback branch July 25, 2025 02:05
sharkdp added a commit to astral-sh/ty that referenced this pull request Jul 25, 2025
Changes which I chose not to include; let me know if one of these should
be added:

```
- Add warning for unknown `TY_MEMORY_REPORT` value ([#19465](astral-sh/ruff#19465))
- Add goto definition to playground ([#19425](astral-sh/ruff#19425))
- Added support for "go to references" in ty playground. ([#19516](astral-sh/ruff#19516))
- Fall back to `Unknown` if no type is stored for an expression ([#19517](astral-sh/ruff#19517))
- Make `Module` a Salsa ingredient ([#19495](astral-sh/ruff#19495))
- Return a tuple spec from the iterator protocol ([#19496](astral-sh/ruff#19496))
```

---------

Co-authored-by: Alex Waygood <[email protected]>
AlexWaygood pushed a commit that referenced this pull request Jul 25, 2025
## Summary

See discussion at
https://github.com/astral-sh/ruff/pull/19478/files#r2223870292

Fixes astral-sh/ty#865

## Test Plan

Added one mdtest for invalid Callable annotation; removed `pull-types:
skip` from that test file.

Co-authored-by: lipefree <[email protected]>
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.

Server panics when encountering invalid syntax related to Callable annotations
6 participants