Complete typing with mypy --check-untyped-defs.

This commit is contained in:
Sergio Martínez Portela 2023-10-16 00:21:30 +02:00
parent 9fb4bce5ef
commit 1d0b4cce14
3 changed files with 28 additions and 12 deletions

View File

@ -21,7 +21,7 @@ jobs:
- run: apt-get update && apt-get install -y python3-pip - run: apt-get update && apt-get install -y python3-pip
- run: pip install -e . - run: pip install -e .
- run: pip install mypy - run: pip install mypy
- run: mypy org_rw - run: mypy org_rw --check-untyped-defs
stability-extra-test: stability-extra-test:
runs-on: ubuntu-latest runs-on: ubuntu-latest

View File

@ -1,4 +1,4 @@
from typing import Union from typing import List, Optional, Union
class DrawerNode: class DrawerNode:
@ -95,7 +95,7 @@ class CodeBlock(BlockNode):
def __init__(self, header, subtype, arguments): def __init__(self, header, subtype, arguments):
super().__init__() super().__init__()
self.header = header self.header = header
self.lines = None self.lines: Optional[List] = None
self.subtype = subtype self.subtype = subtype
self.arguments = arguments self.arguments = arguments
@ -103,7 +103,7 @@ class CodeBlock(BlockNode):
self.lines = lines self.lines = lines
def __repr__(self): def __repr__(self):
return "<Code: {}>".format(len(self.lines)) return "<Code: {}>".format(len(self.lines or []))
DomNode = Union[DrawerNode, DomNode = Union[DrawerNode,
PropertyNode, PropertyNode,
@ -116,4 +116,10 @@ DomNode = Union[DrawerNode,
BlockNode, BlockNode,
] ]
ContainerDomNode = Union[DrawerNode,
ListGroupNode,
TableNode,
BlockNode,
]
from .utils import get_raw_contents from .utils import get_raw_contents

View File

@ -9,7 +9,7 @@ import re
import sys import sys
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
from enum import Enum from enum import Enum
from typing import cast, Iterator, List, Optional, Tuple, Union from typing import cast, Iterator, List, Literal, Optional, Tuple, Union
from .types import HeadlineDict from .types import HeadlineDict
@ -366,7 +366,7 @@ class Headline:
tree: List[dom.DomNode] = [] tree: List[dom.DomNode] = []
current_node: Optional[dom.DomNode] = None current_node: Optional[dom.DomNode] = None
indentation_tree: List[dom.DomNode] = [] indentation_tree: List[dom.ContainerDomNode] = []
contents: Optional[str] = None contents: Optional[str] = None
for line in sorted(everything, key=get_line): for line in sorted(everything, key=get_line):
@ -402,7 +402,7 @@ class Headline:
elif isinstance(line, Text): elif isinstance(line, Text):
tree_up = list(indentation_tree) tree_up = list(indentation_tree)
while len(tree_up) > 0: while len(tree_up) > 0:
node = tree_up[-1] node: dom.DomNode = tree_up[-1]
if (isinstance(node, dom.BlockNode) if (isinstance(node, dom.BlockNode)
or isinstance(node, dom.DrawerNode) or isinstance(node, dom.DrawerNode)
): ):
@ -508,6 +508,7 @@ class Headline:
node = dom.TableSeparatorRow(orig=line) node = dom.TableSeparatorRow(orig=line)
else: else:
node = dom.TableRow(line.cells, orig=line) node = dom.TableRow(line.cells, orig=line)
current_node = cast(dom.ContainerDomNode, current_node)
current_node.append(node) current_node.append(node)
elif ( elif (
@ -607,7 +608,7 @@ class Headline:
return self.get_lists() return self.get_lists()
def get_tables(self): def get_tables(self):
tables = [] tables: List[List] = [] # TableRow[][]
last_line = None last_line = None
for row in self.table_rows: for row in self.table_rows:
@ -666,6 +667,7 @@ class Headline:
time_seg = content[len("CLOCK:") :].strip() time_seg = content[len("CLOCK:") :].strip()
parsed: Union[None, OrgTime, TimeRange] = None
if "--" in time_seg: if "--" in time_seg:
# TODO: Consider duration # TODO: Consider duration
start, end = time_seg.split("=")[0].split("--") start, end = time_seg.split("=")[0].split("--")
@ -1307,7 +1309,7 @@ class Link:
return "[[{}]]".format(self.value) return "[[{}]]".format(self.value)
def _update_content(self): def _update_content(self):
new_contents = [] new_contents: List[Union[str, LinkToken]] = []
new_contents.append(self._value) new_contents.append(self._value)
if self._description: if self._description:
new_contents.append(LinkToken(LinkTokenType.OPEN_DESCRIPTION)) new_contents.append(LinkToken(LinkTokenType.OPEN_DESCRIPTION))
@ -1509,9 +1511,13 @@ TOKEN_TYPE_OPEN_LINK = 3
TOKEN_TYPE_CLOSE_LINK = 4 TOKEN_TYPE_CLOSE_LINK = 4
TOKEN_TYPE_OPEN_DESCRIPTION = 5 TOKEN_TYPE_OPEN_DESCRIPTION = 5
TokenItems = Union[
Tuple[int, Union[None, str, MarkerToken]],
]
def tokenize_contents(contents: str):
tokens = [] def tokenize_contents(contents: str) -> List[TokenItems]:
tokens: List[TokenItems] = []
last_char = None last_char = None
text: List[str] = [] text: List[str] = []
@ -1675,14 +1681,17 @@ def parse_content_block(raw_contents: Union[List[RawLine],str]):
else: else:
current_line = raw_contents[0].linenum current_line = raw_contents[0].linenum
contents = [] contents: List[Union[str, MarkerToken, LinkToken]] = []
# Use tokens to tag chunks of text with it's container type # Use tokens to tag chunks of text with it's container type
for (tok_type, tok_val) in tokens: for (tok_type, tok_val) in tokens:
if tok_type == TOKEN_TYPE_TEXT: if tok_type == TOKEN_TYPE_TEXT:
assert isinstance(tok_val, str)
contents.append(tok_val) contents.append(tok_val)
elif tok_type == TOKEN_TYPE_OPEN_MARKER: elif tok_type == TOKEN_TYPE_OPEN_MARKER:
assert isinstance(tok_val, str)
contents.append(MarkerToken(False, MARKERS[tok_val])) contents.append(MarkerToken(False, MARKERS[tok_val]))
elif tok_type == TOKEN_TYPE_CLOSE_MARKER: elif tok_type == TOKEN_TYPE_CLOSE_MARKER:
assert isinstance(tok_val, str)
contents.append(MarkerToken(True, MARKERS[tok_val])) contents.append(MarkerToken(True, MARKERS[tok_val]))
elif tok_type == TOKEN_TYPE_OPEN_LINK: elif tok_type == TOKEN_TYPE_OPEN_LINK:
contents.append(LinkToken(LinkTokenType.OPEN_LINK)) contents.append(LinkToken(LinkTokenType.OPEN_LINK))
@ -2338,6 +2347,7 @@ def loads(s, environment=BASE_ENVIRONMENT, extra_cautious=True):
context_start = i context_start = i
context_last_line = i context_last_line = i
elif context_start: elif context_start:
assert context_last_line is not None
if i > (context_last_line + DEBUG_DIFF_CONTEXT): if i > (context_last_line + DEBUG_DIFF_CONTEXT):
start = max(0, context_start - DEBUG_DIFF_CONTEXT) start = max(0, context_start - DEBUG_DIFF_CONTEXT)
end = min(len(diff), context_last_line + DEBUG_DIFF_CONTEXT) end = min(len(diff), context_last_line + DEBUG_DIFF_CONTEXT)