Skip to content

fix LSP - typing.NamedTuple tooltip is not informative #2844#2846

Open
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:2844
Open

fix LSP - typing.NamedTuple tooltip is not informative #2844#2846
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:2844

Conversation

@asukaminato0721
Copy link
Contributor

Summary

Fixes #2844

The checker was still analyzing both __new__ and __init__, but the LSP trace for a constructor call was getting overwritten by init, which is why NamedTuple showed the useless *Unknown, **Unknown tooltip.

I changed the trace priority so IDE features keep metaclass __call__ first, otherwise overridden __new__, otherwise __init__.

Test Plan

add test

@meta-cla meta-cla bot added the cla signed label Mar 21, 2026
@asukaminato0721 asukaminato0721 marked this pull request as ready for review March 21, 2026 23:38
Copilot AI review requested due to automatic review settings March 21, 2026 23:38
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes LSP hover/signature help for typing.NamedTuple constructor calls by preventing the constructor trace from being overwritten by __init__, so IDE tooltips show the meaningful field-based signature.

Changes:

  • Adjust constructor trace recording priority to prefer metaclass __call__, otherwise overridden __new__, otherwise __init__.
  • Add LSP signature help regression test for NamedTuple constructor showing named fields and return type.
  • Add LSP hover regression test for NamedTuple constructor showing named fields and return type.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pyrefly/lib/alt/call.rs Changes trace-recording order/conditions for constructor calls to avoid __init__ overwriting more-informative call traces.
pyrefly/lib/test/lsp/signature_help.rs Adds signature help regression test asserting NamedTuple constructor shows field params (not *Unknown/**Unknown).
pyrefly/lib/test/lsp/hover.rs Adds hover regression test asserting NamedTuple constructor shows field params (not *Unknown/**Unknown).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 699 to 703
let hint = None; // discard hint
let class_metadata = self.get_metadata_for_class(cls.class_object());
let metaclass_dunder_call = self.get_metaclass_dunder_call(&cls);
if let Some(ret) =
self.call_metaclass(&cls, arguments_range, args, keywords, errors, context, hint)
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

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

construct_class now calls get_metaclass_dunder_call unconditionally (and that internally calls get_metadata_for_class), even when the class has no custom metaclass __call__. Since constructor analysis is on a hot path, this adds an extra metadata/member lookup for every class construction. Consider deriving a has_metaclass_call bool from call_metaclass(...) (or returning the resolved __call__ type from it) so you only perform the metaclass lookup when it actually exists, or refactor get_metaclass_dunder_call to reuse the already-computed class_metadata.

Copilot uses AI. Check for mistakes.
Comment on lines 704 to 706
{
if let Some(metaclass_dunder_call) = self.get_metaclass_dunder_call(&cls) {
if let Some(metaclass_dunder_call) = metaclass_dunder_call.clone() {
if let Some(callee_range) = callee_range
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

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

The outer metaclass_dunder_call is an Option<...> but the inner if let Some(metaclass_dunder_call) = metaclass_dunder_call.clone() reuses the same identifier for the unwrapped value, which is easy to misread (especially since the outer value is still used later for gating trace recording). Renaming the inner binding (e.g. metaclass_dunder_call_ty) would make the trace-priority logic clearer.

Copilot uses AI. Check for mistakes.
@github-actions
Copy link

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LSP - typing.NamedTuple tooltip is not informative

2 participants