Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e75f9d

Browse files
authored
Add error code arguments to plugin API (#7323)
Update some `fail()` methods to accept an error code argument.
1 parent c610a31 commit 6e75f9d

4 files changed

Lines changed: 12 additions & 8 deletions

File tree

mypy/checker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
from mypy.typeops import tuple_fallback
7373
from mypy import state
7474
from mypy.traverser import has_return_statement
75+
from mypy.errorcodes import ErrorCode
7576

7677
T = TypeVar('T')
7778

@@ -3948,9 +3949,9 @@ def temp_node(self, t: Type, context: Optional[Context] = None) -> TempNode:
39483949
temp.set_line(context.get_line())
39493950
return temp
39503951

3951-
def fail(self, msg: str, context: Context) -> None:
3952+
def fail(self, msg: str, context: Context, *, code: Optional[ErrorCode] = None) -> None:
39523953
"""Produce an error message."""
3953-
self.msg.fail(msg, context)
3954+
self.msg.fail(msg, context, code=code)
39543955

39553956
def note(self, msg: str, context: Context, offset: int = 0) -> None:
39563957
"""Produce a note."""

mypy/plugin.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class C: pass
134134
from mypy.messages import MessageBuilder
135135
from mypy.options import Options
136136
from mypy.lookup import lookup_fully_qualified
137+
from mypy.errorcodes import ErrorCode
137138
import mypy.interpreted_plugin
138139

139140

@@ -152,7 +153,7 @@ class TypeAnalyzerPluginInterface:
152153
options = None # type: Options
153154

154155
@abstractmethod
155-
def fail(self, msg: str, ctx: Context) -> None:
156+
def fail(self, msg: str, ctx: Context, *, code: Optional[ErrorCode] = None) -> None:
156157
"""Emmit an error message at given location."""
157158
raise NotImplementedError
158159

@@ -217,7 +218,7 @@ class CheckerPluginInterface:
217218
path = None # type: str
218219

219220
@abstractmethod
220-
def fail(self, msg: str, ctx: Context) -> None:
221+
def fail(self, msg: str, ctx: Context, *, code: Optional[ErrorCode] = None) -> None:
221222
"""Emit an error message at given location."""
222223
raise NotImplementedError
223224

@@ -255,7 +256,7 @@ def parse_bool(self, expr: Expression) -> Optional[bool]:
255256

256257
@abstractmethod
257258
def fail(self, msg: str, ctx: Context, serious: bool = False, *,
258-
blocker: bool = False) -> None:
259+
blocker: bool = False, code: Optional[ErrorCode] = None) -> None:
259260
"""Emmit an error message at given location."""
260261
raise NotImplementedError
261262

mypy/semanal_shared.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from mypy.util import correct_relative_import
1414
from mypy.types import Type, FunctionLike, Instance, TupleType, TPDICT_FB_NAMES
1515
from mypy.tvar_scope import TypeVarScope
16+
from mypy.errorcodes import ErrorCode
1617
from mypy import join
1718

1819
# Priorities for ordering of patches within the "patch" phase of semantic analysis
@@ -44,7 +45,7 @@ def lookup_fully_qualified_or_none(self, name: str) -> Optional[SymbolTableNode]
4445

4546
@abstractmethod
4647
def fail(self, msg: str, ctx: Context, serious: bool = False, *,
47-
blocker: bool = False) -> None:
48+
blocker: bool = False, code: Optional[ErrorCode] = None) -> None:
4849
raise NotImplementedError
4950

5051
@abstractmethod

mypy/typeanal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from mypy.exprtotype import expr_to_unanalyzed_type, TypeTranslationError
3030
from mypy.plugin import Plugin, TypeAnalyzerPluginInterface, AnalyzeTypeContext
3131
from mypy.semanal_shared import SemanticAnalyzerCoreInterface
32+
from mypy.errorcodes import ErrorCode
3233
from mypy import nodes, message_registry
3334

3435
T = TypeVar('T')
@@ -746,8 +747,8 @@ def analyze_literal_param(self, idx: int, arg: Type, ctx: Context) -> Optional[L
746747
def analyze_type(self, t: Type) -> Type:
747748
return t.accept(self)
748749

749-
def fail(self, msg: str, ctx: Context) -> None:
750-
self.fail_func(msg, ctx)
750+
def fail(self, msg: str, ctx: Context, *, code: Optional[ErrorCode] = None) -> None:
751+
self.fail_func(msg, ctx, code=code)
751752

752753
def note(self, msg: str, ctx: Context) -> None:
753754
self.note_func(msg, ctx)

0 commit comments

Comments
 (0)