Skip to content

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Aug 21, 2025

Summary

This PR fixes astral-sh/ty#1071

The core issue is that CallableType is a salsa interned but Signature (which CallableType stores) ignores the Definition in its Eq and Hash implementation.

This PR tries to simplest fix by removing the custom Eq and Hash implementation. The main downside of this fix is that it can increase memory usage because CallableTypes that are equal except for their Definition are now interned separately.

The alternative is to remove Definition from CallableType and instead, call bindings directly on the callee (call_expression.func). However, this would require
addressing the TODO

here

// Most class literal constructor calls are handled by `try_call_constructor` and
// not via getting the signature here. This signature can still be used in some
// cases (e.g. evaluating callable subtyping). TODO improve this definition
// (intersection of `__new__` and `__init__` signatures? and respect metaclass
// `__call__`).

This might probably be worth addressing anyway, but is the more involved fix. That's why I opted for removing the custom Eq implementation.

We already "ignore" the definition during normalization, thank's to Alex's work in #19615

Test Plan

Screen.Recording.2025-08-21.at.14.21.06.mov

@MichaReiser MichaReiser added the bug Something isn't working label Aug 21, 2025
@MichaReiser MichaReiser requested a review from carljm as a code owner August 21, 2025 12:20
@MichaReiser MichaReiser added server Related to the LSP server ty Multi-file analysis & type inference labels Aug 21, 2025
@MichaReiser MichaReiser requested review from Gankra and UnboundVariable and removed request for dcreager and sharkdp August 21, 2025 12:21
Copy link
Contributor

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

Copy link
Contributor

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

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.

Makes sense to me!

I agree that we need to do some work cleaning up Type::bindings(), but I think both @sharkdp and I have tried several times at this point and every time we found it surprisingly hard 🙃 so this is definitely the easier fix for now.

@MichaReiser MichaReiser changed the title [ty] Fix wrong docstring in call signature completion [ty] Fix incorrect docstring in call signature completion Aug 21, 2025
Copy link
Contributor

@Gankra Gankra left a comment

Choose a reason for hiding this comment

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

Nice!

@MichaReiser
Copy link
Member Author

I'll give @carljm a chance to chime in, as he reasoned that we should remove Definition from CallableType.

@carljm
Copy link
Contributor

carljm commented Aug 21, 2025

I'll give @carljm a chance to chime in, as he reasoned that we should remove Definition from CallableType.

I do think that's what we should do eventually, but it doesn't have to be done right now -- this fix improves the behavior in the short term and I don't think makes anything more difficult for removing the definition in future, so I'm in favor of it.

(Also it removes code, and all else equal I'm always in favor of that 😆 )

@Gankra
Copy link
Contributor

Gankra commented Aug 21, 2025

I'm gonna land this since it's a important/pervasive bugfix, makes it easier to test/debug other function handling features.

@Gankra Gankra merged commit 365f521 into main Aug 21, 2025
40 checks passed
@Gankra Gankra deleted the micha/fix-wrong-function-in-call-signatures branch August 21, 2025 20:36
second-ed pushed a commit to second-ed/ruff that referenced this pull request Sep 9, 2025
…20021)

## Summary

This PR fixes astral-sh/ty#1071

The core issue is that `CallableType` is a salsa interned but
`Signature` (which `CallableType` stores) ignores the `Definition` in
its `Eq` and `Hash` implementation.

This PR tries to simplest fix by removing the custom `Eq` and `Hash`
implementation. The main downside of this fix is that it can increase
memory usage because `CallableType`s that are equal except for their
`Definition` are now interned separately.

The alternative is to remove `Definition` from `CallableType` and
instead, call `bindings` directly on the callee (call_expression.func).
However, this would require
addressing the TODO 

here
https://github.com/astral-sh/ruff/blob/39ee71c2a57bd6c51482430ba8bfe3728b4ca443/crates/ty_python_semantic/src/types.rs#L4582-L4586

This might probably be worth addressing anyway, but is the more involved
fix. That's why I opted for removing the custom `Eq` implementation.

We already "ignore" the definition during normalization, thank's to
Alex's work in astral-sh#19615

## Test Plan



https://github.com/user-attachments/assets/248d1cb1-12fd-4441-adab-b7e0866d23eb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working server Related to the LSP server ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lsp query caching seems to constantly mixup functions with identical signatures
4 participants