diff options
author | danieleades <33452915+danieleades@users.noreply.github.com> | 2022-07-18 22:08:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-18 22:08:16 +0100 |
commit | a504ac6100a577cbda1bedf80d69636603ee287c (patch) | |
tree | 0aa79585440486bdb109f7238f8f5c992dad0d99 /sphinx/directives/code.py | |
parent | a340427ba4d208193d539cb7e8401be23d75547e (diff) | |
download | sphinx-git-a504ac6100a577cbda1bedf80d69636603ee287c.tar.gz |
Improve static typing strictness (#10569)
Diffstat (limited to 'sphinx/directives/code.py')
-rw-r--r-- | sphinx/directives/code.py | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/sphinx/directives/code.py b/sphinx/directives/code.py index 9437fe9a5..0ea492e12 100644 --- a/sphinx/directives/code.py +++ b/sphinx/directives/code.py @@ -1,7 +1,7 @@ import sys import textwrap from difflib import unified_diff -from typing import TYPE_CHECKING, Any, Dict, List, Tuple +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple from docutils import nodes from docutils.nodes import Element, Node @@ -48,7 +48,9 @@ class Highlight(SphinxDirective): linenothreshold=linenothreshold)] -def dedent_lines(lines: List[str], dedent: int, location: Tuple[str, int] = None) -> List[str]: +def dedent_lines( + lines: List[str], dedent: Optional[int], location: Optional[Tuple[str, int]] = None +) -> List[str]: if dedent is None: return textwrap.dedent(''.join(lines)).splitlines(True) @@ -186,7 +188,7 @@ class LiteralIncludeReader: ('diff', 'end-at'), ] - def __init__(self, filename: str, options: Dict, config: Config) -> None: + def __init__(self, filename: str, options: Dict[str, Any], config: Config) -> None: self.filename = filename self.options = options self.encoding = options.get('encoding', config.source_encoding) @@ -200,7 +202,9 @@ class LiteralIncludeReader: raise ValueError(__('Cannot use both "%s" and "%s" options') % (option1, option2)) - def read_file(self, filename: str, location: Tuple[str, int] = None) -> List[str]: + def read_file( + self, filename: str, location: Optional[Tuple[str, int]] = None + ) -> List[str]: try: with open(filename, encoding=self.encoding, errors='strict') as f: text = f.read() @@ -216,7 +220,7 @@ class LiteralIncludeReader: 'be wrong, try giving an :encoding: option') % (self.encoding, filename)) from exc - def read(self, location: Tuple[str, int] = None) -> Tuple[str, int]: + def read(self, location: Optional[Tuple[str, int]] = None) -> Tuple[str, int]: if 'diff' in self.options: lines = self.show_diff() else: @@ -233,14 +237,16 @@ class LiteralIncludeReader: return ''.join(lines), len(lines) - def show_diff(self, location: Tuple[str, int] = None) -> List[str]: + def show_diff(self, location: Optional[Tuple[str, int]] = None) -> List[str]: new_lines = self.read_file(self.filename) old_filename = self.options.get('diff') old_lines = self.read_file(old_filename) diff = unified_diff(old_lines, new_lines, old_filename, self.filename) return list(diff) - def pyobject_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]: + def pyobject_filter( + self, lines: List[str], location: Optional[Tuple[str, int]] = None + ) -> List[str]: pyobject = self.options.get('pyobject') if pyobject: from sphinx.pycode import ModuleAnalyzer @@ -258,7 +264,9 @@ class LiteralIncludeReader: return lines - def lines_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]: + def lines_filter( + self, lines: List[str], location: Optional[Tuple[str, int]] = None + ) -> List[str]: linespec = self.options.get('lines') if linespec: linelist = parselinenos(linespec, len(lines)) @@ -282,7 +290,9 @@ class LiteralIncludeReader: return lines - def start_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]: + def start_filter( + self, lines: List[str], location: Optional[Tuple[str, int]] = None + ) -> List[str]: if 'start-at' in self.options: start = self.options.get('start-at') inclusive = False @@ -313,7 +323,9 @@ class LiteralIncludeReader: return lines - def end_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]: + def end_filter( + self, lines: List[str], location: Optional[Tuple[str, int]] = None + ) -> List[str]: if 'end-at' in self.options: end = self.options.get('end-at') inclusive = True @@ -340,21 +352,27 @@ class LiteralIncludeReader: return lines - def prepend_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]: + def prepend_filter( + self, lines: List[str], location: Optional[Tuple[str, int]] = None + ) -> List[str]: prepend = self.options.get('prepend') if prepend: lines.insert(0, prepend + '\n') return lines - def append_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]: + def append_filter( + self, lines: List[str], location: Optional[Tuple[str, int]] = None + ) -> List[str]: append = self.options.get('append') if append: lines.append(append + '\n') return lines - def dedent_filter(self, lines: List[str], location: Tuple[str, int] = None) -> List[str]: + def dedent_filter( + self, lines: List[str], location: Optional[Tuple[str, int]] = None + ) -> List[str]: if 'dedent' in self.options: return dedent_lines(lines, self.options.get('dedent'), location=location) else: |