From e0719babdc8744144449c81fac14ba7bbf21446f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 27 Feb 2023 11:40:00 +0100 Subject: Merging upstream version 3.0.23. Signed-off-by: Daniel Baumann --- ptpython/completer.py | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) (limited to 'ptpython/completer.py') diff --git a/ptpython/completer.py b/ptpython/completer.py index 9252106..f28d2b1 100644 --- a/ptpython/completer.py +++ b/ptpython/completer.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import ast import collections.abc as collections_abc import inspect @@ -44,8 +46,8 @@ class PythonCompleter(Completer): def __init__( self, - get_globals: Callable[[], Dict[str, Any]], - get_locals: Callable[[], Dict[str, Any]], + get_globals: Callable[[], dict[str, Any]], + get_locals: Callable[[], dict[str, Any]], enable_dictionary_completion: Callable[[], bool], ) -> None: super().__init__() @@ -58,8 +60,8 @@ class PythonCompleter(Completer): self._jedi_completer = JediCompleter(get_globals, get_locals) self._dictionary_completer = DictionaryCompleter(get_globals, get_locals) - self._path_completer_cache: Optional[GrammarCompleter] = None - self._path_completer_grammar_cache: Optional["_CompiledGrammar"] = None + self._path_completer_cache: GrammarCompleter | None = None + self._path_completer_grammar_cache: _CompiledGrammar | None = None @property def _path_completer(self) -> GrammarCompleter: @@ -74,7 +76,7 @@ class PythonCompleter(Completer): return self._path_completer_cache @property - def _path_completer_grammar(self) -> "_CompiledGrammar": + def _path_completer_grammar(self) -> _CompiledGrammar: """ Return the grammar for matching paths inside strings inside Python code. @@ -85,7 +87,7 @@ class PythonCompleter(Completer): self._path_completer_grammar_cache = self._create_path_completer_grammar() return self._path_completer_grammar_cache - def _create_path_completer_grammar(self) -> "_CompiledGrammar": + def _create_path_completer_grammar(self) -> _CompiledGrammar: def unwrapper(text: str) -> str: return re.sub(r"\\(.)", r"\1", text) @@ -189,7 +191,6 @@ class PythonCompleter(Completer): ): # If we are inside a string, Don't do Jedi completion. if not self._path_completer_grammar.match(document.text_before_cursor): - # Do Jedi Python completions. yield from self._jedi_completer.get_completions( document, complete_event @@ -203,8 +204,8 @@ class JediCompleter(Completer): def __init__( self, - get_globals: Callable[[], Dict[str, Any]], - get_locals: Callable[[], Dict[str, Any]], + get_globals: Callable[[], dict[str, Any]], + get_locals: Callable[[], dict[str, Any]], ) -> None: super().__init__() @@ -242,7 +243,7 @@ class JediCompleter(Completer): # Jedi issue: "KeyError: u'a_lambda'." # https://github.com/jonathanslenders/ptpython/issues/89 pass - except IOError: + except OSError: # Jedi issue: "IOError: No such file or directory." # https://github.com/jonathanslenders/ptpython/issues/71 pass @@ -303,8 +304,8 @@ class DictionaryCompleter(Completer): def __init__( self, - get_globals: Callable[[], Dict[str, Any]], - get_locals: Callable[[], Dict[str, Any]], + get_globals: Callable[[], dict[str, Any]], + get_locals: Callable[[], dict[str, Any]], ) -> None: super().__init__() @@ -386,7 +387,7 @@ class DictionaryCompleter(Completer): re.VERBOSE, ) - def _lookup(self, expression: str, temp_locals: Dict[str, Any]) -> object: + def _lookup(self, expression: str, temp_locals: dict[str, Any]) -> object: """ Do lookup of `object_var` in the context. `temp_locals` is a dictionary, used for the locals. @@ -399,7 +400,6 @@ class DictionaryCompleter(Completer): def get_completions( self, document: Document, complete_event: CompleteEvent ) -> Iterable[Completion]: - # First, find all for-loops, and assign the first item of the # collections they're iterating to the iterator variable, so that we # can provide code completion on the iterators. @@ -431,7 +431,7 @@ class DictionaryCompleter(Completer): except BaseException: raise ReprFailedError - def eval_expression(self, document: Document, locals: Dict[str, Any]) -> object: + def eval_expression(self, document: Document, locals: dict[str, Any]) -> object: """ Evaluate """ @@ -446,7 +446,7 @@ class DictionaryCompleter(Completer): self, document: Document, complete_event: CompleteEvent, - temp_locals: Dict[str, Any], + temp_locals: dict[str, Any], ) -> Iterable[Completion]: """ Complete the [ or . operator after an object. @@ -454,7 +454,6 @@ class DictionaryCompleter(Completer): result = self.eval_expression(document, temp_locals) if result is not None: - if isinstance( result, (list, tuple, dict, collections_abc.Mapping, collections_abc.Sequence), @@ -470,7 +469,7 @@ class DictionaryCompleter(Completer): self, document: Document, complete_event: CompleteEvent, - temp_locals: Dict[str, Any], + temp_locals: dict[str, Any], ) -> Iterable[Completion]: """ Complete dictionary keys. @@ -478,6 +477,7 @@ class DictionaryCompleter(Completer): def meta_repr(value: object) -> Callable[[], str]: "Abbreviate meta text, make sure it fits on one line." + # We return a function, so that it gets computed when it's needed. # When there are many completions, that improves the performance # quite a bit (for the multi-column completion menu, we only need @@ -549,7 +549,7 @@ class DictionaryCompleter(Completer): self, document: Document, complete_event: CompleteEvent, - temp_locals: Dict[str, Any], + temp_locals: dict[str, Any], ) -> Iterable[Completion]: """ Complete attribute names. @@ -568,9 +568,9 @@ class DictionaryCompleter(Completer): obj = getattr(result, name, None) if inspect.isfunction(obj) or inspect.ismethod(obj): return "()" - if isinstance(obj, dict): + if isinstance(obj, collections_abc.Mapping): return "{}" - if isinstance(obj, (list, tuple)): + if isinstance(obj, collections_abc.Sequence): return "[]" except: pass @@ -581,13 +581,13 @@ class DictionaryCompleter(Completer): suffix = get_suffix(name) yield Completion(name, -len(attr_name), display=name + suffix) - def _sort_attribute_names(self, names: List[str]) -> List[str]: + def _sort_attribute_names(self, names: list[str]) -> list[str]: """ Sort attribute names alphabetically, but move the double underscore and underscore names to the end. """ - def sort_key(name: str) -> Tuple[int, str]: + def sort_key(name: str) -> tuple[int, str]: if name.startswith("__"): return (2, name) # Double underscore comes latest. if name.startswith("_"): @@ -599,7 +599,7 @@ class DictionaryCompleter(Completer): class HidePrivateCompleter(Completer): """ - Wrapper around completer that hides private fields, deponding on whether or + Wrapper around completer that hides private fields, depending on whether or not public fields are shown. (The reason this is implemented as a `Completer` wrapper is because this @@ -617,7 +617,6 @@ class HidePrivateCompleter(Completer): def get_completions( self, document: Document, complete_event: CompleteEvent ) -> Iterable[Completion]: - completions = list(self.completer.get_completions(document, complete_event)) complete_private_attributes = self.complete_private_attributes() hide_private = False @@ -653,7 +652,7 @@ except ImportError: # Python 2. def _get_style_for_jedi_completion( - jedi_completion: "jedi.api.classes.Completion", + jedi_completion: jedi.api.classes.Completion, ) -> str: """ Return completion style to use for this name. -- cgit v1.2.3