Skip to content

Exposes end_col_offset attr from python AST #12972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ def set_line(self, node: N, n: AstNode) -> N:
node.line = n.lineno
node.column = n.col_offset
node.end_line = getattr(n, "end_lineno", None) if isinstance(n, ast3.expr) else None
node.end_column = getattr(n, "end_col_offset", None) if isinstance(n, ast3.expr) else None

return node

def translate_opt_expr_list(self, l: Sequence[Optional[AST]]) -> List[Optional[Expression]]:
Expand Down
1 change: 1 addition & 0 deletions mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ def visit_ClassDef(self, n: ast27.ClassDef) -> ClassDef:
cdef.line = n.lineno + len(n.decorator_list)
cdef.column = n.col_offset
cdef.end_line = n.lineno
cdef.end_column = None
self.class_and_function_stack.pop()
return cdef

Expand Down
28 changes: 19 additions & 9 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@

class Context:
"""Base type for objects that are valid as error message locations."""
__slots__ = ('line', 'column', 'end_line')
__slots__ = ('line', 'column', 'end_line', 'end_column')

def __init__(self, line: int = -1, column: int = -1) -> None:
self.line = line
self.column = column
self.end_line: Optional[int] = None
self.end_column: Optional[int] = None

def set_line(self,
target: Union['Context', int],
column: Optional[int] = None,
end_line: Optional[int] = None) -> None:
end_line: Optional[int] = None,
end_column: Optional[int] = None) -> None:
"""If target is a node, pull line (and column) information
into this node. If column is specified, this will override any column
information coming from a node.
Expand All @@ -44,13 +46,17 @@ def set_line(self,
self.line = target.line
self.column = target.column
self.end_line = target.end_line
self.end_column = target.end_column

if column is not None:
self.column = column

if end_line is not None:
self.end_line = end_line

if end_column is not None:
self.end_column = end_column

def get_line(self) -> int:
"""Don't use. Use x.line."""
return self.line
Expand Down Expand Up @@ -631,13 +637,16 @@ def __init__(self,
def set_line(self,
target: Union[Context, int],
column: Optional[int] = None,
end_line: Optional[int] = None) -> None:
super().set_line(target, column, end_line)
end_line: Optional[int] = None,
end_column: Optional[int] = None) -> None:
super().set_line(target, column, end_line, end_column)

if self.initializer and self.initializer.line < 0:
self.initializer.set_line(self.line, self.column, self.end_line)
self.initializer.set_line(
self.line, self.column, self.end_line, self.end_column)

self.variable.set_line(self.line, self.column, self.end_line)
self.variable.set_line(
self.line, self.column, self.end_line, self.end_column)


FUNCITEM_FLAGS: Final = FUNCBASE_FLAGS + [
Expand Down Expand Up @@ -698,10 +707,11 @@ def max_fixed_argc(self) -> int:
def set_line(self,
target: Union[Context, int],
column: Optional[int] = None,
end_line: Optional[int] = None) -> None:
super().set_line(target, column, end_line)
end_line: Optional[int] = None,
end_column: Optional[int] = None) -> None:
super().set_line(target, column, end_line, end_column)
for arg in self.arguments:
arg.set_line(self.line, self.column, self.end_line)
arg.set_line(self.line, self.column, self.end_line, end_column)

def is_dynamic(self) -> bool:
return self.type is None
Expand Down