mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-06 12:54:29 +08:00
Add type annotations to start- and end-pos attributes
These are frequently used within consuming code, so having annotations avoids others needing to work around mypy errors from them.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
from codecs import BOM_UTF8
|
from codecs import BOM_UTF8
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
from parso.python.tokenize import group
|
from parso.python.tokenize import group
|
||||||
|
|
||||||
@@ -13,10 +14,10 @@ class PrefixPart:
|
|||||||
self.type = typ
|
self.type = typ
|
||||||
self.value = value
|
self.value = value
|
||||||
self.spacing = spacing
|
self.spacing = spacing
|
||||||
self.start_pos = start_pos
|
self.start_pos = start_pos # type: Tuple[int, int]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def end_pos(self):
|
def end_pos(self) -> Tuple[int, int]:
|
||||||
if self.value.endswith('\n'):
|
if self.value.endswith('\n'):
|
||||||
return self.start_pos[0] + 1, 0
|
return self.start_pos[0] + 1, 0
|
||||||
if self.value == unicode_bom:
|
if self.value == unicode_bom:
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ try:
|
|||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from collections import Mapping
|
from collections import Mapping
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
from parso.tree import Node, BaseNode, Leaf, ErrorNode, ErrorLeaf, \
|
from parso.tree import Node, BaseNode, Leaf, ErrorNode, ErrorLeaf, \
|
||||||
search_ancestor
|
search_ancestor
|
||||||
@@ -149,7 +150,7 @@ class _LeafWithoutNewlines(PythonLeaf):
|
|||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def end_pos(self):
|
def end_pos(self) -> Tuple[int, int]:
|
||||||
return self.line, self.column + len(self.value)
|
return self.line, self.column + len(self.value)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from abc import abstractmethod, abstractproperty
|
from abc import abstractmethod, abstractproperty
|
||||||
|
from typing import List, Optional, Tuple
|
||||||
|
|
||||||
from parso.utils import split_lines
|
from parso.utils import split_lines
|
||||||
|
|
||||||
@@ -125,7 +126,7 @@ class NodeOrLeaf:
|
|||||||
return node
|
return node
|
||||||
|
|
||||||
@abstractproperty
|
@abstractproperty
|
||||||
def start_pos(self):
|
def start_pos(self) -> Tuple[int, int]:
|
||||||
"""
|
"""
|
||||||
Returns the starting position of the prefix as a tuple, e.g. `(3, 4)`.
|
Returns the starting position of the prefix as a tuple, e.g. `(3, 4)`.
|
||||||
|
|
||||||
@@ -133,7 +134,7 @@ class NodeOrLeaf:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractproperty
|
@abstractproperty
|
||||||
def end_pos(self):
|
def end_pos(self) -> Tuple[int, int]:
|
||||||
"""
|
"""
|
||||||
Returns the end position of the prefix as a tuple, e.g. `(3, 4)`.
|
Returns the end position of the prefix as a tuple, e.g. `(3, 4)`.
|
||||||
|
|
||||||
@@ -180,7 +181,7 @@ class Leaf(NodeOrLeaf):
|
|||||||
'''
|
'''
|
||||||
__slots__ = ('value', 'parent', 'line', 'column', 'prefix')
|
__slots__ = ('value', 'parent', 'line', 'column', 'prefix')
|
||||||
|
|
||||||
def __init__(self, value, start_pos, prefix=''):
|
def __init__(self, value: str, start_pos: Tuple[int, int], prefix: str = '') -> None:
|
||||||
self.value = value
|
self.value = value
|
||||||
'''
|
'''
|
||||||
:py:func:`str` The value of the current token.
|
:py:func:`str` The value of the current token.
|
||||||
@@ -191,17 +192,17 @@ class Leaf(NodeOrLeaf):
|
|||||||
:py:func:`str` Typically a mixture of whitespace and comments. Stuff
|
:py:func:`str` Typically a mixture of whitespace and comments. Stuff
|
||||||
that is syntactically irrelevant for the syntax tree.
|
that is syntactically irrelevant for the syntax tree.
|
||||||
'''
|
'''
|
||||||
self.parent = None
|
self.parent = None # type: Optional[BaseNode]
|
||||||
'''
|
'''
|
||||||
The parent :class:`BaseNode` of this leaf.
|
The parent :class:`BaseNode` of this leaf.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def start_pos(self):
|
def start_pos(self) -> Tuple[int, int]:
|
||||||
return self.line, self.column
|
return self.line, self.column
|
||||||
|
|
||||||
@start_pos.setter
|
@start_pos.setter
|
||||||
def start_pos(self, value):
|
def start_pos(self, value: Tuple[int, int]) -> None:
|
||||||
self.line = value[0]
|
self.line = value[0]
|
||||||
self.column = value[1]
|
self.column = value[1]
|
||||||
|
|
||||||
@@ -226,7 +227,7 @@ class Leaf(NodeOrLeaf):
|
|||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def end_pos(self):
|
def end_pos(self) -> Tuple[int, int]:
|
||||||
lines = split_lines(self.value)
|
lines = split_lines(self.value)
|
||||||
end_pos_line = self.line + len(lines) - 1
|
end_pos_line = self.line + len(lines) - 1
|
||||||
# Check for multiline token
|
# Check for multiline token
|
||||||
@@ -258,26 +259,26 @@ class BaseNode(NodeOrLeaf):
|
|||||||
"""
|
"""
|
||||||
__slots__ = ('children', 'parent')
|
__slots__ = ('children', 'parent')
|
||||||
|
|
||||||
def __init__(self, children):
|
def __init__(self, children: List[NodeOrLeaf]) -> None:
|
||||||
self.children = children
|
self.children = children
|
||||||
"""
|
"""
|
||||||
A list of :class:`NodeOrLeaf` child nodes.
|
A list of :class:`NodeOrLeaf` child nodes.
|
||||||
"""
|
"""
|
||||||
self.parent = None
|
self.parent = None # type: Optional[BaseNode]
|
||||||
'''
|
'''
|
||||||
The parent :class:`BaseNode` of this leaf.
|
The parent :class:`BaseNode` of this leaf.
|
||||||
None if this is the root node.
|
None if this is the root node.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def start_pos(self):
|
def start_pos(self) -> Tuple[int, int]:
|
||||||
return self.children[0].start_pos
|
return self.children[0].start_pos
|
||||||
|
|
||||||
def get_start_pos_of_prefix(self):
|
def get_start_pos_of_prefix(self):
|
||||||
return self.children[0].get_start_pos_of_prefix()
|
return self.children[0].get_start_pos_of_prefix()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def end_pos(self):
|
def end_pos(self) -> Tuple[int, int]:
|
||||||
return self.children[-1].end_pos
|
return self.children[-1].end_pos
|
||||||
|
|
||||||
def _get_code_for_children(self, children, include_prefix):
|
def _get_code_for_children(self, children, include_prefix):
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ tests of pydocstyle.
|
|||||||
import difflib
|
import difflib
|
||||||
import re
|
import re
|
||||||
from functools import total_ordering
|
from functools import total_ordering
|
||||||
|
from typing import Iterator, Tuple
|
||||||
|
|
||||||
import parso
|
import parso
|
||||||
from parso.utils import python_bytes_to_unicode
|
from parso.utils import python_bytes_to_unicode
|
||||||
@@ -13,7 +14,7 @@ from parso.utils import python_bytes_to_unicode
|
|||||||
|
|
||||||
@total_ordering
|
@total_ordering
|
||||||
class WantedIssue:
|
class WantedIssue:
|
||||||
def __init__(self, code, line, column):
|
def __init__(self, code: str, line: int, column: int) -> None:
|
||||||
self.code = code
|
self.code = code
|
||||||
self._line = line
|
self._line = line
|
||||||
self._column = column
|
self._column = column
|
||||||
@@ -21,18 +22,18 @@ class WantedIssue:
|
|||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return self.code == other.code and self.start_pos == other.start_pos
|
return self.code == other.code and self.start_pos == other.start_pos
|
||||||
|
|
||||||
def __lt__(self, other):
|
def __lt__(self, other: 'WantedIssue') -> bool:
|
||||||
return self.start_pos < other.start_pos or self.code < other.code
|
return self.start_pos < other.start_pos or self.code < other.code
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self) -> int:
|
||||||
return hash(str(self.code) + str(self._line) + str(self._column))
|
return hash(str(self.code) + str(self._line) + str(self._column))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def start_pos(self):
|
def start_pos(self) -> Tuple[int, int]:
|
||||||
return self._line, self._column
|
return self._line, self._column
|
||||||
|
|
||||||
|
|
||||||
def collect_errors(code):
|
def collect_errors(code: str) -> Iterator[WantedIssue]:
|
||||||
for line_nr, line in enumerate(code.splitlines(), 1):
|
for line_nr, line in enumerate(code.splitlines(), 1):
|
||||||
match = re.match(r'(\s*)#: (.*)$', line)
|
match = re.match(r'(\s*)#: (.*)$', line)
|
||||||
if match is not None:
|
if match is not None:
|
||||||
|
|||||||
Reference in New Issue
Block a user