9
9
10
10
from mypy import defaults , errorcodes as codes , message_registry
11
11
from mypy .errors import Errors
12
+ from mypy .message_registry import ErrorMessage
12
13
from mypy .nodes import (
13
14
ARG_NAMED ,
14
15
ARG_NAMED_OPT ,
@@ -242,10 +243,6 @@ def ast3_parse(
242
243
MISSING_FALLBACK : Final = FakeInfo ("fallback can't be filled out until semanal" )
243
244
_dummy_fallback : Final = Instance (MISSING_FALLBACK , [], - 1 )
244
245
245
- TYPE_COMMENT_SYNTAX_ERROR : Final = "syntax error in type comment"
246
-
247
- INVALID_TYPE_IGNORE : Final = 'Invalid "type: ignore" comment'
248
-
249
246
TYPE_IGNORE_PATTERN : Final = re .compile (r"[^#]*#\s*type:\s*ignore\s*(.*)" )
250
247
251
248
@@ -354,8 +351,8 @@ def parse_type_comment(
354
351
except SyntaxError :
355
352
if errors is not None :
356
353
stripped_type = type_comment .split ("#" , 2 )[0 ].strip ()
357
- err_msg = f' { TYPE_COMMENT_SYNTAX_ERROR } " { stripped_type } "'
358
- errors .report (line , column , err_msg , blocker = True , code = codes . SYNTAX )
354
+ err_msg = message_registry . TYPE_COMMENT_SYNTAX_ERROR_VALUE . format ( stripped_type )
355
+ errors .report (line , column , err_msg . value , blocker = True , code = err_msg . code )
359
356
return None , None
360
357
else :
361
358
raise
@@ -366,7 +363,9 @@ def parse_type_comment(
366
363
ignored : list [str ] | None = parse_type_ignore_tag (tag )
367
364
if ignored is None :
368
365
if errors is not None :
369
- errors .report (line , column , INVALID_TYPE_IGNORE , code = codes .SYNTAX )
366
+ errors .report (
367
+ line , column , message_registry .INVALID_TYPE_IGNORE .value , code = codes .SYNTAX
368
+ )
370
369
else :
371
370
raise SyntaxError
372
371
else :
@@ -439,24 +438,16 @@ def __init__(
439
438
def note (self , msg : str , line : int , column : int ) -> None :
440
439
self .errors .report (line , column , msg , severity = "note" , code = codes .SYNTAX )
441
440
442
- def fail (
443
- self ,
444
- msg : str ,
445
- line : int ,
446
- column : int ,
447
- blocker : bool = True ,
448
- code : codes .ErrorCode = codes .SYNTAX ,
449
- ) -> None :
441
+ def fail (self , msg : ErrorMessage , line : int , column : int , blocker : bool = True ) -> None :
450
442
if blocker or not self .options .ignore_errors :
451
- self .errors .report (line , column , msg , blocker = blocker , code = code )
443
+ self .errors .report (line , column , msg . value , blocker = blocker , code = msg . code )
452
444
453
445
def fail_merge_overload (self , node : IfStmt ) -> None :
454
446
self .fail (
455
- "Condition can't be inferred, unable to merge overloads" ,
447
+ message_registry . FAILED_TO_MERGE_OVERLOADS ,
456
448
line = node .line ,
457
449
column = node .column ,
458
450
blocker = False ,
459
- code = codes .MISC ,
460
451
)
461
452
462
453
def visit (self , node : AST | None ) -> Any :
@@ -516,10 +507,7 @@ def translate_stmt_list(
516
507
if ignores :
517
508
joined_ignores = ", " .join (ignores )
518
509
self .fail (
519
- (
520
- "type ignore with error code is not supported for modules; "
521
- f'use `# mypy: disable-error-code="{ joined_ignores } "`'
522
- ),
510
+ message_registry .TYPE_IGNORE_WITH_ERRCODE_ON_MODULE .format (joined_ignores ),
523
511
line = min (self .type_ignores ),
524
512
column = 0 ,
525
513
blocker = False ,
@@ -912,7 +900,7 @@ def visit_Module(self, mod: ast3.Module) -> MypyFile:
912
900
if parsed is not None :
913
901
self .type_ignores [ti .lineno ] = parsed
914
902
else :
915
- self .fail (INVALID_TYPE_IGNORE , ti .lineno , - 1 , blocker = False )
903
+ self .fail (message_registry . INVALID_TYPE_IGNORE , ti .lineno , - 1 , blocker = False )
916
904
body = self .fix_function_overloads (self .translate_stmt_list (mod .body , ismodule = True ))
917
905
return MypyFile (body , self .imports , False , self .type_ignores )
918
906
@@ -985,7 +973,7 @@ def do_func_def(
985
973
arg_types .insert (0 , AnyType (TypeOfAny .special_form ))
986
974
except SyntaxError :
987
975
stripped_type = n .type_comment .split ("#" , 2 )[0 ].strip ()
988
- err_msg = f' { TYPE_COMMENT_SYNTAX_ERROR } " { stripped_type } "'
976
+ err_msg = message_registry . TYPE_COMMENT_SYNTAX_ERROR_VALUE . format ( stripped_type )
989
977
self .fail (err_msg , lineno , n .col_offset )
990
978
if n .type_comment and n .type_comment [0 ] not in ["(" , "#" ]:
991
979
self .note (
@@ -1005,18 +993,20 @@ def do_func_def(
1005
993
func_type = None
1006
994
if any (arg_types ) or return_type :
1007
995
if len (arg_types ) != 1 and any (isinstance (t , EllipsisType ) for t in arg_types ):
996
+ self .fail (message_registry .ELLIPSIS_WITH_OTHER_TYPEARGS , lineno , n .col_offset )
997
+ elif len (arg_types ) > len (arg_kinds ):
1008
998
self .fail (
1009
- "Ellipses cannot accompany other argument types in function type signature" ,
999
+ message_registry . TYPE_SIGNATURE_TOO_MANY_ARGS ,
1010
1000
lineno ,
1011
1001
n .col_offset ,
1012
- )
1013
- elif len (arg_types ) > len (arg_kinds ):
1014
- self .fail (
1015
- "Type signature has too many arguments" , lineno , n .col_offset , blocker = False
1002
+ blocker = False ,
1016
1003
)
1017
1004
elif len (arg_types ) < len (arg_kinds ):
1018
1005
self .fail (
1019
- "Type signature has too few arguments" , lineno , n .col_offset , blocker = False
1006
+ message_registry .TYPE_SIGNATURE_TOO_FEW_ARGS ,
1007
+ lineno ,
1008
+ n .col_offset ,
1009
+ blocker = False ,
1020
1010
)
1021
1011
else :
1022
1012
func_type = CallableType (
@@ -1162,7 +1152,7 @@ def make_argument(
1162
1152
return argument
1163
1153
1164
1154
def fail_arg (self , msg : str , arg : ast3 .arg ) -> None :
1165
- self .fail (msg , arg .lineno , arg .col_offset )
1155
+ self .fail (ErrorMessage ( msg ) , arg .lineno , arg .col_offset )
1166
1156
1167
1157
# ClassDef(identifier name,
1168
1158
# expr* bases,
@@ -1889,9 +1879,9 @@ def parent(self) -> AST | None:
1889
1879
return None
1890
1880
return self .node_stack [- 2 ]
1891
1881
1892
- def fail (self , msg : str , line : int , column : int ) -> None :
1882
+ def fail (self , msg : ErrorMessage , line : int , column : int ) -> None :
1893
1883
if self .errors :
1894
- self .errors .report (line , column , msg , blocker = True , code = codes . SYNTAX )
1884
+ self .errors .report (line , column , msg . value , blocker = True , code = msg . code )
1895
1885
1896
1886
def note (self , msg : str , line : int , column : int ) -> None :
1897
1887
if self .errors :
@@ -1911,7 +1901,7 @@ def visit_Call(self, e: Call) -> Type:
1911
1901
note = "Suggestion: use {0}[...] instead of {0}(...)" .format (constructor )
1912
1902
return self .invalid_type (e , note = note )
1913
1903
if not constructor :
1914
- self .fail ("Expected arg constructor name" , e .lineno , e .col_offset )
1904
+ self .fail (message_registry . ARG_CONSTRUCTOR_NAME_EXPECTED , e .lineno , e .col_offset )
1915
1905
1916
1906
name : str | None = None
1917
1907
default_type = AnyType (TypeOfAny .special_form )
@@ -1924,25 +1914,21 @@ def visit_Call(self, e: Call) -> Type:
1924
1914
elif i == 1 :
1925
1915
name = self ._extract_argument_name (arg )
1926
1916
else :
1927
- self .fail ("Too many arguments for argument constructor" , f .lineno , f .col_offset )
1917
+ self .fail (message_registry . ARG_CONSTRUCTOR_TOO_MANY_ARGS , f .lineno , f .col_offset )
1928
1918
for k in e .keywords :
1929
1919
value = k .value
1930
1920
if k .arg == "name" :
1931
1921
if name is not None :
1932
1922
self .fail (
1933
- '"{}" gets multiple values for keyword argument "name"' .format (
1934
- constructor
1935
- ),
1923
+ message_registry .MULTIPLE_VALUES_FOR_NAME_KWARG .format (constructor ),
1936
1924
f .lineno ,
1937
1925
f .col_offset ,
1938
1926
)
1939
1927
name = self ._extract_argument_name (value )
1940
1928
elif k .arg == "type" :
1941
1929
if typ is not default_type :
1942
1930
self .fail (
1943
- '"{}" gets multiple values for keyword argument "type"' .format (
1944
- constructor
1945
- ),
1931
+ message_registry .MULTIPLE_VALUES_FOR_TYPE_KWARG .format (constructor ),
1946
1932
f .lineno ,
1947
1933
f .col_offset ,
1948
1934
)
@@ -1951,7 +1937,7 @@ def visit_Call(self, e: Call) -> Type:
1951
1937
typ = converted
1952
1938
else :
1953
1939
self .fail (
1954
- f'Unexpected argument " { k .arg } " for argument constructor' ,
1940
+ message_registry . ARG_CONSTRUCTOR_UNEXPECTED_ARG . format ( k .arg ) ,
1955
1941
value .lineno ,
1956
1942
value .col_offset ,
1957
1943
)
@@ -1966,7 +1952,9 @@ def _extract_argument_name(self, n: ast3.expr) -> str | None:
1966
1952
elif isinstance (n , NameConstant ) and str (n .value ) == "None" :
1967
1953
return None
1968
1954
self .fail (
1969
- f"Expected string literal for argument name, got { type (n ).__name__ } " , self .line , 0
1955
+ message_registry .ARG_NAME_EXPECTED_STRING_LITERAL .format (type (n ).__name__ ),
1956
+ self .line ,
1957
+ 0 ,
1970
1958
)
1971
1959
return None
1972
1960
0 commit comments