Skip to content

Commit

Permalink
[doctools] More type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy C committed Jan 14, 2025
1 parent 283fb7c commit e8eddb3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions devtools/types.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ check-mycpp() {
}

check-doctools() {
if true; then
if false; then
local -a files=(
$(for x in doctools/*.py; do echo $x; done | grep -v '_test.py' )
lazylex/html.py
)
else
#local -a files=( lazylex/html.py )
#local -a files=( doctools/help_gen.py )
local -a files=( doctools/ul_table.py lazylex/html.py )
local -a files=( doctools/ul_table.py lazylex/html.py doctools/oils_doc.py doctools/help_gen.py )
fi

# 777 errors before pyann
Expand Down
10 changes: 6 additions & 4 deletions doctools/help_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import re
import sys

from typing import AnyStr

from _devbuild.gen.htm8_asdl import h8_id
from doctools import html_lib
from doctools.util import log
Expand Down Expand Up @@ -239,7 +241,7 @@ def log(self, msg, *args):
log(ind + msg, *args)

def handle_starttag(self, tag, attrs):
# type: (str, List[Tuple[str, str]]) -> None
# type: (AnyStr, List[Tuple[AnyStr, AnyStr]]) -> None
if tag in self.heading_tags:
self.in_heading = True
if self.cur_group:
Expand All @@ -251,15 +253,15 @@ def handle_starttag(self, tag, attrs):
self.indent += 1

def handle_endtag(self, tag):
# type: (str) -> None
# type: (AnyStr) -> None
if tag in self.heading_tags:
self.in_heading = False

self.log('[%d] </> %s', self.indent, tag)
self.indent -= 1

def handle_entityref(self, name):
# type: (str) -> None
# type: (AnyStr) -> None
"""
From Python docs:
This method is called to process a named character reference of the form
Expand All @@ -273,7 +275,7 @@ def handle_entityref(self, name):
self.cur_group[3].append(c)

def handle_data(self, data):
# type: (str) -> None
# type: (AnyStr) -> None
self.log('data %r', data)
if self.in_heading:
self.cur_group[2].append(data)
Expand Down
15 changes: 11 additions & 4 deletions doctools/oils_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
from _devbuild.gen.htm8_asdl import h8_id

import cgi
from typing import Iterator
from typing import Any
from typing import List
from typing import Optional
try:
from cStringIO import StringIO
except ImportError:
Expand All @@ -26,6 +22,8 @@
import re
import sys

from typing import Iterator, Any, List, Optional, IO

from doctools.util import log
from lazylex import html

Expand Down Expand Up @@ -180,6 +178,7 @@ def __init__(self, s, start_pos, end_pos):
self.end_pos = end_pos

def PrintHighlighted(self, out):
# type: (html.Output) -> None
raise NotImplementedError()


Expand Down Expand Up @@ -287,6 +286,7 @@ def __init__(self, s, start_pos, end_pos, chapter, linkify_stop_col):
self.linkify_stop_col = linkify_stop_col

def PrintHighlighted(self, out):
# type: (html.Output) -> None
from doctools import help_gen

debug_out = []
Expand Down Expand Up @@ -318,6 +318,8 @@ def __init__(self, s, start_pos, end_pos, lang):
self.lang = lang

def PrintHighlighted(self, out):
# type: (html.Output) -> None

# unescape before passing to pygments, which will escape
code = html.ToText(self.s, self.start_pos, self.end_pos)

Expand All @@ -329,6 +331,7 @@ def PrintHighlighted(self, out):


def SimpleHighlightCode(s):
# type: (str) -> str
"""Simple highlighting for test/shell-vs-shell.sh."""

f = StringIO()
Expand Down Expand Up @@ -540,6 +543,7 @@ def HighlightCode(s, default_highlighter, debug_out=None):


def ExtractCode(s, f):
# type: (str, IO[str]) -> None
"""Print code blocks to a plain text file.
So we can at least validate the syntax.
Expand Down Expand Up @@ -637,6 +641,7 @@ class ShellSession(object):
"""

def __init__(self, shell_exe, cache_dir):
# type: (str, str) -> None
"""
Args:
shell_exe: sh, bash, osh, or oil. Use the one in the $PATH by default.
Expand All @@ -646,6 +651,7 @@ def __init__(self, shell_exe, cache_dir):
self.cache_dir = cache_dir

def PrintHighlighted(self, s, start_pos, end_pos, out):
# type: (str, int, int, html.Output) -> None
"""
Args:
s: an HTML string.
Expand All @@ -654,6 +660,7 @@ def PrintHighlighted(self, s, start_pos, end_pos, out):


def main(argv):
# type: (List[str]) -> None
action = argv[1]

if action == 'highlight':
Expand Down

0 comments on commit e8eddb3

Please sign in to comment.