GeezerVison is showing github.com — Ensure signature help label contains parameter substrings per LSP spec by SoulSniper-V2 · Pull Request #728 · python-lsp/python-lsp-server
Conversation
Problem
When signature help is requested for callable objects whose docstring header does not explicitly list the parameter signature (for example, callable class instances or C-extensions/ufuncs), pylsp could return a truncated label like MyCallable() or ufunc() while parameters lists parameter names like ['a', 'b'] or ['args', 'kwargs'].
According to the LSP 3.17 specification:
Note: a label of type string should be a substring of its containing signature label. Its intended use case is to highlight the parameter label part in the SignatureInformation.label.
Because parameter labels were not substrings of the signature label, strict LSP clients (such as YouCompleteMe) report a protocol error / invalid signature message.
Reported in #678.
Root cause
In pylsp/plugins/signature.py:
function_sig was parsed by splitting s.docstring() on \n\n and taking the first line/paragraph. For callable instances or objects whose docstring begins with a class or type header without parameter names (e.g. ufunc()), this produced a header without parameters.
s.params was populated with parameter objects whose names (p.name) were not contained in the truncated label.
Fix
- Use Jedi's
s.to_string() to obtain the canonical signature string (e.g. MyCallable(a, b=1)), falling back to docstring parsing if to_string() is empty or unavailable.
- If
s.params is present and any parameter name is not a substring of function_sig, construct the label as f"{name}({param_list})" so that all parameter names are guaranteed to be substrings of the signature label.
- As an additional safeguard, ensure parameter entries in
sig["parameters"] are only included if their label is a substring of the containing signature label per the LSP specification.
Test
Added unit tests in test/plugins/test_signature.py:
test_callable_instance_signature: Verifies that signature help for a callable class instance includes its parameters in the signature label and that all parameter labels are substrings of the signature label.
test_signature_fallback_when_label_missing_params: Verifies fallback formatting when a signature label would otherwise omit parameter names.
Fixes #678
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
At least 1 approving review is required to merge this pull request.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.