1111import traceback
1212import warnings
1313from io import TextIOWrapper
14+ from typing import Iterable , Iterator , List , Optional , Sequence , Union
1415
1516import astroid
1617from astroid import AstroidError , nodes
3132)
3233from pylint .message import MessageDefinitionStore , MessagesHandlerMixIn
3334from pylint .reporters .ureports import nodes as report_nodes
34- from pylint .typing import CheckerStats
35+ from pylint .typing import CheckerStats , FileItem , ModuleDescriptionDict
3536from pylint .utils import ASTWalker , FileState , utils
3637from pylint .utils .pragma_parser import (
3738 OPTION_PO ,
@@ -938,16 +939,20 @@ def initialize(self):
938939 if not msg .may_be_emitted ():
939940 self ._msgs_state [msg .msgid ] = False
940941
941- def check (self , files_or_modules ) :
942+ def check (self , files_or_modules : Union [ Sequence [ str ], str ]) -> None :
942943 """main checking entry: check a list of files or modules from their name.
943944
944945 files_or_modules is either a string or list of strings presenting modules to check.
945946 """
946947 self .initialize ()
947-
948948 if not isinstance (files_or_modules , (list , tuple )):
949- files_or_modules = (files_or_modules ,)
950-
949+ # pylint: disable-next=fixme
950+ # TODO: Update typing and docstring for 'files_or_modules' when removing the deprecation
951+ warnings .warn (
952+ "In pylint 3.0, the checkers check function will only accept sequence of string" ,
953+ DeprecationWarning ,
954+ )
955+ files_or_modules = (files_or_modules ,) # type: ignore
951956 if self .config .from_stdin :
952957 if len (files_or_modules ) != 1 :
953958 raise exceptions .InvalidArgsError (
@@ -973,66 +978,65 @@ def check(self, files_or_modules):
973978 files_or_modules ,
974979 )
975980
976- def check_single_file (self , name , filepath , modname ):
977- """Check single file
981+ def check_single_file (self , name : str , filepath : str , modname : str ) -> None :
982+ warnings .warn (
983+ "In pylint 3.0, the checkers check_single_file function will be removed. "
984+ "Use check_single_file_item instead." ,
985+ DeprecationWarning ,
986+ )
987+ self .check_single_file_item (FileItem (name , filepath , modname ))
988+
989+ def check_single_file_item (self , file : FileItem ) -> None :
990+ """Check single file item
978991
979992 The arguments are the same that are documented in _check_files
980993
981994 The initialize() method should be called before calling this method
982995 """
983996 with self ._astroid_module_checker () as check_astroid_module :
984- self ._check_file (
985- self .get_ast , check_astroid_module , name , filepath , modname
986- )
987-
988- def _check_files (self , get_ast , file_descrs ):
989- """Check all files from file_descrs
990-
991- The file_descrs should be iterable of tuple (name, filepath, modname)
992- where
993- - name: full name of the module
994- - filepath: path of the file
995- - modname: module name
996- """
997+ self ._check_file (self .get_ast , check_astroid_module , file )
998+
999+ def _check_files (
1000+ self ,
1001+ get_ast ,
1002+ file_descrs : Iterable [FileItem ],
1003+ ) -> None :
1004+ """Check all files from file_descrs"""
9971005 with self ._astroid_module_checker () as check_astroid_module :
998- for name , filepath , modname in file_descrs :
1006+ for file in file_descrs :
9991007 try :
1000- self ._check_file (
1001- get_ast , check_astroid_module , name , filepath , modname
1002- )
1008+ self ._check_file (get_ast , check_astroid_module , file )
10031009 except Exception as ex : # pylint: disable=broad-except
10041010 template_path = prepare_crash_report (
1005- ex , filepath , self .crash_file_path
1011+ ex , file . filepath , self .crash_file_path
10061012 )
1007- msg = get_fatal_error_message (filepath , template_path )
1013+ msg = get_fatal_error_message (file . filepath , template_path )
10081014 if isinstance (ex , AstroidError ):
10091015 symbol = "astroid-error"
1010- msg = ( filepath , msg )
1016+ self . add_message ( symbol , args = ( file . filepath , msg ) )
10111017 else :
10121018 symbol = "fatal"
1013- self .add_message (symbol , args = msg )
1019+ self .add_message (symbol , args = msg )
10141020
1015- def _check_file (self , get_ast , check_astroid_module , name , filepath , modname ):
1021+ def _check_file (self , get_ast , check_astroid_module , file : FileItem ):
10161022 """Check a file using the passed utility functions (get_ast and check_astroid_module)
10171023
10181024 :param callable get_ast: callable returning AST from defined file taking the following arguments
10191025 - filepath: path to the file to check
10201026 - name: Python module name
10211027 :param callable check_astroid_module: callable checking an AST taking the following arguments
10221028 - ast: AST of the module
1023- :param str name: full name of the module
1024- :param str filepath: path to checked file
1025- :param str modname: name of the checked Python module
1029+ :param FileItem file: data about the file
10261030 """
1027- self .set_current_module (name , filepath )
1031+ self .set_current_module (file . name , file . filepath )
10281032 # get the module representation
1029- ast_node = get_ast (filepath , name )
1033+ ast_node = get_ast (file . filepath , file . name )
10301034 if ast_node is None :
10311035 return
10321036
10331037 self ._ignore_file = False
10341038
1035- self .file_state = FileState (modname )
1039+ self .file_state = FileState (file . modpath )
10361040 # fix the current file (if the source file was not available or
10371041 # if it's actually a c extension)
10381042 self .current_file = ast_node .file # pylint: disable=maybe-no-member
@@ -1045,7 +1049,7 @@ def _check_file(self, get_ast, check_astroid_module, name, filepath, modname):
10451049 self .add_message (msgid , line , None , args )
10461050
10471051 @staticmethod
1048- def _get_file_descr_from_stdin (filepath ) :
1052+ def _get_file_descr_from_stdin (filepath : str ) -> FileItem :
10491053 """Return file description (tuple of module name, file path, base name) from given file path
10501054
10511055 This method is used for creating suitable file description for _check_files when the
@@ -1059,19 +1063,19 @@ def _get_file_descr_from_stdin(filepath):
10591063 except ImportError :
10601064 modname = os .path .splitext (os .path .basename (filepath ))[0 ]
10611065
1062- return (modname , filepath , filepath )
1066+ return FileItem (modname , filepath , filepath )
10631067
1064- def _iterate_file_descrs (self , files_or_modules ):
1068+ def _iterate_file_descrs (self , files_or_modules ) -> Iterator [ FileItem ] :
10651069 """Return generator yielding file descriptions (tuples of module name, file path, base name)
10661070
10671071 The returned generator yield one item for each Python module that should be linted.
10681072 """
10691073 for descr in self ._expand_files (files_or_modules ):
10701074 name , filepath , is_arg = descr ["name" ], descr ["path" ], descr ["isarg" ]
10711075 if self .should_analyze_file (name , filepath , is_argument = is_arg ):
1072- yield (name , filepath , descr ["basename" ])
1076+ yield FileItem (name , filepath , descr ["basename" ])
10731077
1074- def _expand_files (self , modules ):
1078+ def _expand_files (self , modules ) -> List [ ModuleDescriptionDict ] :
10751079 """get modules and errors from a list of modules and handle errors"""
10761080 result , errors = expand_modules (
10771081 modules ,
@@ -1088,7 +1092,7 @@ def _expand_files(self, modules):
10881092 self .add_message (key , args = message )
10891093 return result
10901094
1091- def set_current_module (self , modname , filepath = None ):
1095+ def set_current_module (self , modname , filepath : Optional [ str ] = None ):
10921096 """set the name of the currently analyzed module and
10931097 init statistics for it
10941098 """
@@ -1097,10 +1101,10 @@ def set_current_module(self, modname, filepath=None):
10971101 self .reporter .on_set_current_module (modname , filepath )
10981102 self .current_name = modname
10991103 self .current_file = filepath or modname
1100- self .stats ["by_module" ][modname ] = {}
1101- self .stats ["by_module" ][modname ]["statement" ] = 0
1104+ self .stats ["by_module" ][modname ] = {} # type: ignore # Refactor of PyLinter.stats necessary
1105+ self .stats ["by_module" ][modname ]["statement" ] = 0 # type: ignore
11021106 for msg_cat in MSG_TYPES .values ():
1103- self .stats ["by_module" ][modname ][msg_cat ] = 0
1107+ self .stats ["by_module" ][modname ][msg_cat ] = 0 # type: ignore
11041108
11051109 @contextlib .contextmanager
11061110 def _astroid_module_checker (self ):
0 commit comments