6464TYPE_CHECKING = False
6565
6666if TYPE_CHECKING :
67- from typing import Any , Iterator , Mapping
67+ from typing import Any , Iterable , Iterator , Mapping
6868
6969
7070MoreLinesCallable = Callable [[str ], bool ]
@@ -629,7 +629,7 @@ class ModuleCompleter:
629629
630630 def __init__ (self , namespace : Mapping [str , Any ] | None = None ) -> None :
631631 self .namespace = namespace or {}
632- self ._global_cache : list [str ] = []
632+ self ._global_cache : list [pkgutil . ModuleInfo ] = []
633633 self ._curr_sys_path : list [str ] = sys .path [:]
634634
635635 def get_completions (self , line : str ) -> list [str ]:
@@ -642,6 +642,7 @@ def get_completions(self, line: str) -> list[str]:
642642 def complete (self , from_name : str | None , name : str | None ) -> list [str ]:
643643 if from_name is None :
644644 # import x.y.z<tab>
645+ assert name is not None
645646 path , prefix = self .get_path_and_prefix (name )
646647 modules = self .find_modules (path , prefix )
647648 return [self .format_completion (path , module ) for module in modules ]
@@ -664,20 +665,20 @@ def find_modules(self, path: str, prefix: str) -> list[str]:
664665
665666 if path .startswith ('.' ):
666667 # Convert relative path to absolute path
667- package = self .namespace .get ('__package__' )
668- path = self .resolve_relative_name (path , package )
668+ package = self .namespace .get ('__package__' , '' )
669+ path = self .resolve_relative_name (path , package ) # type: ignore[assignment]
669670 if path is None :
670671 return []
671672
672- modules = self .global_cache
673+ modules : Iterable [ pkgutil . ModuleInfo ] = self .global_cache
673674 for segment in path .split ('.' ):
674675 modules = [mod_info for mod_info in modules
675676 if mod_info .ispkg and mod_info .name == segment ]
676677 modules = self .iter_submodules (modules )
677678 return [module .name for module in modules
678679 if module .name .startswith (prefix )]
679680
680- def iter_submodules (self , parent_modules ) -> Iterator [pkgutil .ModuleInfo ]:
681+ def iter_submodules (self , parent_modules : list [ pkgutil . ModuleInfo ] ) -> Iterator [pkgutil .ModuleInfo ]:
681682 """Iterate over all submodules of the given parent modules."""
682683 specs = [info .module_finder .find_spec (info .name )
683684 for info in parent_modules if info .ispkg ]
@@ -714,7 +715,7 @@ def format_completion(self, path: str, module: str) -> str:
714715 return f'{ path } { module } '
715716 return f'{ path } .{ module } '
716717
717- def resolve_relative_name (self , name , package ) -> str | None :
718+ def resolve_relative_name (self , name : str , package : str ) -> str | None :
718719 """Resolve a relative module name to an absolute name.
719720
720721 Example: resolve_relative_name('.foo', 'bar') -> 'bar.foo'
@@ -733,7 +734,7 @@ def resolve_relative_name(self, name, package) -> str | None:
733734 return f'{ base } .{ name } ' if name else base
734735
735736 @property
736- def global_cache (self ) -> list [str ]:
737+ def global_cache (self ) -> list [pkgutil . ModuleInfo ]:
737738 """Global module cache"""
738739 if not self ._global_cache or self ._curr_sys_path != sys .path :
739740 self ._curr_sys_path = sys .path [:]
@@ -854,14 +855,18 @@ def parse_dotted_name(self) -> str:
854855 if self .tokens .peek_string ('.' ):
855856 name .append ('.' )
856857 self .tokens .pop ()
857- if self .tokens .peek_name () and self .tokens .peek ().string not in self ._keywords :
858+ if (self .tokens .peek_name ()
859+ and (tok := self .tokens .peek ())
860+ and tok .string not in self ._keywords ):
858861 name .append (self .tokens .pop_name ())
859862 if not name :
860863 raise ParseError ('parse_dotted_name' )
861864 while self .tokens .peek_string ('.' ):
862865 name .append ('.' )
863866 self .tokens .pop ()
864- if self .tokens .peek_name () and self .tokens .peek ().string not in self ._keywords :
867+ if (self .tokens .peek_name ()
868+ and (tok := self .tokens .peek ())
869+ and tok .string not in self ._keywords ):
865870 name .append (self .tokens .pop_name ())
866871 else :
867872 break
0 commit comments