11
11
import traceback
12
12
import warnings
13
13
from io import TextIOWrapper
14
+ from typing import Iterable , Iterator , List , Optional , Sequence , Union
14
15
15
16
import astroid
16
17
from astroid import AstroidError , nodes
31
32
)
32
33
from pylint .message import MessageDefinitionStore , MessagesHandlerMixIn
33
34
from pylint .reporters .ureports import nodes as report_nodes
34
- from pylint .typing import CheckerStats
35
+ from pylint .typing import CheckerStats , FileItem , ModuleDescriptionDict
35
36
from pylint .utils import ASTWalker , FileState , utils
36
37
from pylint .utils .pragma_parser import (
37
38
OPTION_PO ,
@@ -938,16 +939,20 @@ def initialize(self):
938
939
if not msg .may_be_emitted ():
939
940
self ._msgs_state [msg .msgid ] = False
940
941
941
- def check (self , files_or_modules ) :
942
+ def check (self , files_or_modules : Union [ Sequence [ str ], str ]) -> None :
942
943
"""main checking entry: check a list of files or modules from their name.
943
944
944
945
files_or_modules is either a string or list of strings presenting modules to check.
945
946
"""
946
947
self .initialize ()
947
-
948
948
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
951
956
if self .config .from_stdin :
952
957
if len (files_or_modules ) != 1 :
953
958
raise exceptions .InvalidArgsError (
@@ -973,66 +978,65 @@ def check(self, files_or_modules):
973
978
files_or_modules ,
974
979
)
975
980
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
978
991
979
992
The arguments are the same that are documented in _check_files
980
993
981
994
The initialize() method should be called before calling this method
982
995
"""
983
996
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"""
997
1005
with self ._astroid_module_checker () as check_astroid_module :
998
- for name , filepath , modname in file_descrs :
1006
+ for file in file_descrs :
999
1007
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 )
1003
1009
except Exception as ex : # pylint: disable=broad-except
1004
1010
template_path = prepare_crash_report (
1005
- ex , filepath , self .crash_file_path
1011
+ ex , file . filepath , self .crash_file_path
1006
1012
)
1007
- msg = get_fatal_error_message (filepath , template_path )
1013
+ msg = get_fatal_error_message (file . filepath , template_path )
1008
1014
if isinstance (ex , AstroidError ):
1009
1015
symbol = "astroid-error"
1010
- msg = ( filepath , msg )
1016
+ self . add_message ( symbol , args = ( file . filepath , msg ) )
1011
1017
else :
1012
1018
symbol = "fatal"
1013
- self .add_message (symbol , args = msg )
1019
+ self .add_message (symbol , args = msg )
1014
1020
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 ):
1016
1022
"""Check a file using the passed utility functions (get_ast and check_astroid_module)
1017
1023
1018
1024
:param callable get_ast: callable returning AST from defined file taking the following arguments
1019
1025
- filepath: path to the file to check
1020
1026
- name: Python module name
1021
1027
:param callable check_astroid_module: callable checking an AST taking the following arguments
1022
1028
- 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
1026
1030
"""
1027
- self .set_current_module (name , filepath )
1031
+ self .set_current_module (file . name , file . filepath )
1028
1032
# get the module representation
1029
- ast_node = get_ast (filepath , name )
1033
+ ast_node = get_ast (file . filepath , file . name )
1030
1034
if ast_node is None :
1031
1035
return
1032
1036
1033
1037
self ._ignore_file = False
1034
1038
1035
- self .file_state = FileState (modname )
1039
+ self .file_state = FileState (file . modpath )
1036
1040
# fix the current file (if the source file was not available or
1037
1041
# if it's actually a c extension)
1038
1042
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):
1045
1049
self .add_message (msgid , line , None , args )
1046
1050
1047
1051
@staticmethod
1048
- def _get_file_descr_from_stdin (filepath ) :
1052
+ def _get_file_descr_from_stdin (filepath : str ) -> FileItem :
1049
1053
"""Return file description (tuple of module name, file path, base name) from given file path
1050
1054
1051
1055
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):
1059
1063
except ImportError :
1060
1064
modname = os .path .splitext (os .path .basename (filepath ))[0 ]
1061
1065
1062
- return (modname , filepath , filepath )
1066
+ return FileItem (modname , filepath , filepath )
1063
1067
1064
- def _iterate_file_descrs (self , files_or_modules ):
1068
+ def _iterate_file_descrs (self , files_or_modules ) -> Iterator [ FileItem ] :
1065
1069
"""Return generator yielding file descriptions (tuples of module name, file path, base name)
1066
1070
1067
1071
The returned generator yield one item for each Python module that should be linted.
1068
1072
"""
1069
1073
for descr in self ._expand_files (files_or_modules ):
1070
1074
name , filepath , is_arg = descr ["name" ], descr ["path" ], descr ["isarg" ]
1071
1075
if self .should_analyze_file (name , filepath , is_argument = is_arg ):
1072
- yield (name , filepath , descr ["basename" ])
1076
+ yield FileItem (name , filepath , descr ["basename" ])
1073
1077
1074
- def _expand_files (self , modules ):
1078
+ def _expand_files (self , modules ) -> List [ ModuleDescriptionDict ] :
1075
1079
"""get modules and errors from a list of modules and handle errors"""
1076
1080
result , errors = expand_modules (
1077
1081
modules ,
@@ -1088,7 +1092,7 @@ def _expand_files(self, modules):
1088
1092
self .add_message (key , args = message )
1089
1093
return result
1090
1094
1091
- def set_current_module (self , modname , filepath = None ):
1095
+ def set_current_module (self , modname , filepath : Optional [ str ] = None ):
1092
1096
"""set the name of the currently analyzed module and
1093
1097
init statistics for it
1094
1098
"""
@@ -1097,10 +1101,10 @@ def set_current_module(self, modname, filepath=None):
1097
1101
self .reporter .on_set_current_module (modname , filepath )
1098
1102
self .current_name = modname
1099
1103
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
1102
1106
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
1104
1108
1105
1109
@contextlib .contextmanager
1106
1110
def _astroid_module_checker (self ):
0 commit comments