1
1
"""Plugin for supporting the attrs library (http://www.attrs.org)"""
2
2
3
+ from mypy import message_registry
3
4
from mypy .backports import OrderedDict
4
5
5
6
from typing import Optional , Dict , List , cast , Tuple , Iterable
@@ -130,7 +131,7 @@ def argument(self, ctx: 'mypy.plugin.ClassDefContext') -> Argument:
130
131
init_type = UnionType .make_union ([init_type , NoneType ()])
131
132
132
133
if not init_type :
133
- ctx .api .fail ("Cannot determine __init__ type from converter" , self .context )
134
+ ctx .api .fail (message_registry . CANNOT_DETERMINE_INIT_TYPE , self .context )
134
135
init_type = AnyType (TypeOfAny .from_error )
135
136
elif self .converter .name == '' :
136
137
# This means we had a converter but it's not of a type we can infer.
@@ -210,7 +211,7 @@ def _determine_eq_order(ctx: 'mypy.plugin.ClassDefContext') -> bool:
210
211
order = _get_decorator_optional_bool_argument (ctx , 'order' )
211
212
212
213
if cmp is not None and any ((eq is not None , order is not None )):
213
- ctx .api .fail ('Don \' t mix "cmp" with "eq" and "order"' , ctx .reason )
214
+ ctx .api .fail (message_registry . CMP_WITH_EQ_AND_ORDER , ctx .reason )
214
215
215
216
# cmp takes precedence due to bw-compatibility.
216
217
if cmp is not None :
@@ -224,7 +225,7 @@ def _determine_eq_order(ctx: 'mypy.plugin.ClassDefContext') -> bool:
224
225
order = eq
225
226
226
227
if eq is False and order is True :
227
- ctx .api .fail ('eq must be True if order is True' , ctx .reason )
228
+ ctx .api .fail (message_registry . EQ_TRUE_IF_ORDER_TRUE , ctx .reason )
228
229
229
230
return order
230
231
@@ -248,7 +249,7 @@ def _get_decorator_optional_bool_argument(
248
249
return False
249
250
if attr_value .fullname == 'builtins.None' :
250
251
return None
251
- ctx .api .fail ('"{}" argument must be True or False.' .format (name ), ctx .reason )
252
+ ctx .api .fail (message_registry . ARG_MUST_BE_TRUE_OR_FALSE .format (name ), ctx .reason )
252
253
return default
253
254
return default
254
255
else :
@@ -281,14 +282,14 @@ def attr_class_maker_callback(ctx: 'mypy.plugin.ClassDefContext',
281
282
282
283
if ctx .api .options .python_version [0 ] < 3 :
283
284
if auto_attribs :
284
- ctx .api .fail ("auto_attribs is not supported in Python 2" , ctx .reason )
285
+ ctx .api .fail (message_registry . AUTO_ATTRIBS_UNSUPPORTED_PY2 , ctx .reason )
285
286
return
286
287
if not info .defn .base_type_exprs :
287
288
# Note: This will not catch subclassing old-style classes.
288
- ctx .api .fail ("attrs only works with new-style classes" , info .defn )
289
+ ctx .api .fail (message_registry . ATTRS_NEWSTYLE_CLASS_ONLY , info .defn )
289
290
return
290
291
if kw_only :
291
- ctx .api .fail (KW_ONLY_PYTHON_2_UNSUPPORTED , ctx .reason )
292
+ ctx .api .fail (message_registry . KW_ONLY_UNSUPPORTED_PY2 , ctx .reason )
292
293
return
293
294
294
295
attributes = _analyze_class (ctx , auto_attribs , kw_only )
@@ -407,9 +408,7 @@ def _analyze_class(ctx: 'mypy.plugin.ClassDefContext',
407
408
context = attribute .context if i >= len (super_attrs ) else ctx .cls
408
409
409
410
if not attribute .has_default and last_default :
410
- ctx .api .fail (
411
- "Non-default attributes not allowed after default attributes." ,
412
- context )
411
+ ctx .api .fail (message_registry .NON_DEFAULT_ATTRS_AFTER_DEFAULT , context )
413
412
last_default |= attribute .has_default
414
413
415
414
return attributes
@@ -532,7 +531,7 @@ def _attribute_from_attrib_maker(ctx: 'mypy.plugin.ClassDefContext',
532
531
return None
533
532
534
533
if len (stmt .lvalues ) > 1 :
535
- ctx .api .fail ("Too many names for one attribute" , stmt )
534
+ ctx .api .fail (message_registry . ATTR_TOO_MANY_NAMES , stmt )
536
535
return None
537
536
538
537
# This is the type that belongs in the __init__ method for this attrib.
@@ -544,15 +543,15 @@ def _attribute_from_attrib_maker(ctx: 'mypy.plugin.ClassDefContext',
544
543
# See https://github.com/python-attrs/attrs/issues/481 for explanation.
545
544
kw_only |= _get_bool_argument (ctx , rvalue , 'kw_only' , False )
546
545
if kw_only and ctx .api .options .python_version [0 ] < 3 :
547
- ctx .api .fail (KW_ONLY_PYTHON_2_UNSUPPORTED , stmt )
546
+ ctx .api .fail (message_registry . KW_ONLY_UNSUPPORTED_PY2 , stmt )
548
547
return None
549
548
550
549
# TODO: Check for attr.NOTHING
551
550
attr_has_default = bool (_get_argument (rvalue , 'default' ))
552
551
attr_has_factory = bool (_get_argument (rvalue , 'factory' ))
553
552
554
553
if attr_has_default and attr_has_factory :
555
- ctx .api .fail ('Can \' t pass both "default" and "factory".' , rvalue )
554
+ ctx .api .fail (message_registry . DEFAULT_WITH_FACTORY , rvalue )
556
555
elif attr_has_factory :
557
556
attr_has_default = True
558
557
@@ -562,7 +561,7 @@ def _attribute_from_attrib_maker(ctx: 'mypy.plugin.ClassDefContext',
562
561
try :
563
562
un_type = expr_to_unanalyzed_type (type_arg , ctx .api .options , ctx .api .is_stub_file )
564
563
except TypeTranslationError :
565
- ctx .api .fail ('Invalid argument to type' , type_arg )
564
+ ctx .api .fail (message_registry . TYPE_INVALID_ARG , type_arg )
566
565
else :
567
566
init_type = ctx .api .anal_type (un_type )
568
567
if init_type and isinstance (lhs .node , Var ) and not lhs .node .type :
@@ -574,9 +573,9 @@ def _attribute_from_attrib_maker(ctx: 'mypy.plugin.ClassDefContext',
574
573
converter = _get_argument (rvalue , 'converter' )
575
574
convert = _get_argument (rvalue , 'convert' )
576
575
if convert and converter :
577
- ctx .api .fail ('Can \' t pass both "convert" and "converter".' , rvalue )
576
+ ctx .api .fail (message_registry . CONVERT_WITH_CONVERTER , rvalue )
578
577
elif convert :
579
- ctx .api .fail ("convert is deprecated, use converter" , rvalue )
578
+ ctx .api .fail (message_registry . CONVERT_DEPRECATED , rvalue )
580
579
converter = convert
581
580
converter_info = _parse_converter (ctx , converter )
582
581
@@ -613,10 +612,7 @@ def _parse_converter(ctx: 'mypy.plugin.ClassDefContext',
613
612
return argument
614
613
615
614
# Signal that we have an unsupported converter.
616
- ctx .api .fail (
617
- "Unsupported converter, only named functions and types are currently supported" ,
618
- converter
619
- )
615
+ ctx .api .fail (message_registry .UNSUPPORTED_CONVERTER , converter )
620
616
return Converter ('' )
621
617
return Converter (None )
622
618
0 commit comments