-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Improving dict autocomplete #13735
Comments
This is similar to a previous request to only show magics after I was recently thinking about adding more capabilities to matchers in backward compatible way (#12820). On high level, the solution can be implemented by (expanding upon ideas proposed above):
|
On implementation level, approach (A): (1) Suppressing can be performed as a matcher-level function. Matcher would start to resemble a proper class with methods (not just a function). We could introduce this while maintaining backward-compatibility by adding an optional def matcher(*, should_suppress_others: Callable):
def wrapper(func):
func.should_suppress_others = should_suppress_others
return func
return wrapper
class IPCompleter:
# ...
@matcher(should_suppress_others=lambda text: does_it_look_like_I_am_in_a_dict(text))
def dict_key_matches(self): The new class LegacyMatcher(Protocol):
__call__: Callable[[str], list[str]]
class MatcherSupressionProtocol(Protocol):
should_suppress_others: Callable[[str], bool]
Matcher = Union[LegacyMatcher, MatcherSupressionProtocol] (2) Currently sorting is governed by hard-coded Matcher-level priority/rank could be a solution if it took the request text in and return a number depending on situation (here answering "how likely it seems that we are in a dict"): class MatcherPrioritySystemDynamic(Protocol):
priority: Callable[[str], float]
Matcher = Union[LegacyMatcher, MatcherSupressionProtocol, MatcherPrioritySystemDynamic] |
Completion-level priority/rank would enable more granular control for matcher developers and could expand the API proposed in my earlier comment: class SimpleCompletion(NamedTuple): # or TypedDict with NotRequired
__slots__ = ('text', 'origin', 'type', 'priority')
text: str
origin: Optional[str]
type: Optional[str]
priority: Optional[float]
class LegacyMatcher(Protocol):
__call__: Callable[[str], Union[list[str], list[SimpleCompletion]]] However, what follows from my comment above is an observation that the information about priority and suppression is context-specific (both Approach (B): matchers are just simple functions, but (optionally) return an extensible structure with metadata. In this scenario matchers would comply with the following typing: class MatchingResult(TypedDict):
matches: list[SimpleCompletion]
suppress_others: NotRequired[bool]
priority: NotRequired[float]
LegacyMatcher = Callable[[str], list[str]]
NewMatcher = Callable[[str], MatchingResult] # TODO: find better code name
Matcher = Union[LegacyMatcher, NewMatcher]
|
@krassowski, thanks for the work here. A few pieces of feedback: |
We have found the dict autocomplete to show too many option at times. As an example, given this:
The following is appears:
Here you can see a number of things are suggested that are not helpful including:
This is made much worse by something like pyflyby where all possible imports that start with
a
are also listed.Ideas:
The text was updated successfully, but these errors were encountered: