Skip to content

Commit

Permalink
MNT: Fix mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Mar 11, 2024
1 parent da13281 commit 26c98f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
25 changes: 14 additions & 11 deletions pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def _pep224_docstrings(doc_obj: Union['Module', 'Class'], *,
# Maybe raise exceptions with appropriate message
# before using cleaned doc_obj.source
_ = inspect.findsource(doc_obj.obj)
tree = ast.parse(doc_obj.source) # type: ignore
tree = ast.parse(doc_obj.source)
except (OSError, TypeError, SyntaxError) as exc:
# Don't emit a warning for builtins that don't have source available
is_builtin = getattr(doc_obj.obj, '__module__', None) == 'builtins'
Expand Down Expand Up @@ -345,7 +345,7 @@ def get_name(assign_node):
def get_indent(line):
return len(line) - len(line.lstrip())

source_lines = doc_obj.source.splitlines() # type: ignore
source_lines = doc_obj.source.splitlines()
assign_line = source_lines[assign_node.lineno - 1]
assign_indent = get_indent(assign_line)
comment_lines = []
Expand Down Expand Up @@ -451,7 +451,7 @@ def _toposort(graph: Mapping[T, Set[T]]) -> Generator[T, None, None]:
assert not graph, f"A cyclic dependency exists amongst {graph!r}"


def link_inheritance(context: Context = None):
def link_inheritance(context: Optional[Context] = None):
"""
Link inheritance relationsships between `pdoc.Class` objects
(and between their members) of all `pdoc.Module` objects that
Expand Down Expand Up @@ -491,7 +491,7 @@ class Doc:
"""
__slots__ = ('module', 'name', 'obj', 'docstring', 'inherits')

def __init__(self, name: str, module, obj, docstring: str = None):
def __init__(self, name: str, module, obj, docstring: str = ''):
"""
Initializes a documentation object, where `name` is the public
identifier name, `module` is a `pdoc.Module` object where raw
Expand Down Expand Up @@ -566,7 +566,7 @@ def qualname(self) -> str:
return getattr(self.obj, '__qualname__', self.name)

@lru_cache()
def url(self, relative_to: 'Module' = None, *, link_prefix: str = '',
def url(self, relative_to: Optional['Module'] = None, *, link_prefix: str = '',
top_ancestor: bool = False) -> str:
"""
Canonical relative URL (including page fragment) for this
Expand Down Expand Up @@ -624,8 +624,10 @@ class Module(Doc):
__slots__ = ('supermodule', 'doc', '_context', '_is_inheritance_linked',
'_skipped_submodules')

def __init__(self, module: Union[ModuleType, str], *, docfilter: Callable[[Doc], bool] = None,
supermodule: 'Module' = None, context: Context = None,
def __init__(self, module: Union[ModuleType, str], *,
docfilter: Optional[Callable[[Doc], bool]] = None,
supermodule: Optional['Module'] = None,
context: Optional[Context] = None,
skip_errors: bool = False):
"""
Creates a `Module` documentation object given the actual
Expand Down Expand Up @@ -1010,7 +1012,7 @@ class Class(Doc):
"""
__slots__ = ('doc', '_super_members')

def __init__(self, name: str, module: Module, obj, *, docstring: str = None):
def __init__(self, name: str, module: Module, obj, *, docstring: Optional[str] = None):
assert inspect.isclass(obj)

if docstring is None:
Expand Down Expand Up @@ -1317,7 +1319,7 @@ class Function(Doc):
"""
__slots__ = ('cls',)

def __init__(self, name: str, module: Module, obj, *, cls: Class = None):
def __init__(self, name: str, module: Module, obj, *, cls: Optional[Class] = None):
"""
Same as `pdoc.Doc`, except `obj` must be a
Python function object. The docstring is gathered automatically.
Expand Down Expand Up @@ -1419,7 +1421,8 @@ def return_annotation(self, *, link=None) -> str:
s = re.sub(r'[\w\.]+', partial(_linkify, link=link, module=self.module), s)
return s

def params(self, *, annotate: bool = False, link: Callable[[Doc], str] = None) -> List[str]:
def params(self, *, annotate: bool = False,
link: Optional[Callable[[Doc], str]] = None) -> List[str]:
"""
Returns a list where each element is a nicely formatted
parameter of this function. This includes argument lists,
Expand Down Expand Up @@ -1589,7 +1592,7 @@ class Variable(Doc):
__slots__ = ('cls', 'instance_var')

def __init__(self, name: str, module: Module, docstring, *,
obj=None, cls: Class = None, instance_var: bool = False):
obj=None, cls: Optional[Class] = None, instance_var: bool = False):
"""
Same as `pdoc.Doc`, except `cls` should be provided
as a `pdoc.Class` object when this is a class or instance
Expand Down
12 changes: 7 additions & 5 deletions pdoc/html_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import traceback
from contextlib import contextmanager
from functools import partial, lru_cache
from typing import Callable, Match
from typing import Callable, Match, Optional
from warnings import warn
import xml.etree.ElementTree as etree

Expand Down Expand Up @@ -408,8 +408,9 @@ def handleMatch(self, m, data):


def to_html(text: str, *,
docformat: str = None,
module: pdoc.Module = None, link: Callable[..., str] = None,
docformat: Optional[str] = None,
module: Optional[pdoc.Module] = None,
link: Optional[Callable[..., str]] = None,
latex_math: bool = False):
"""
Returns HTML of `text` interpreted as `docformat`. `__docformat__` is respected
Expand All @@ -433,8 +434,9 @@ def to_html(text: str, *,


def to_markdown(text: str, *,
docformat: str = None,
module: pdoc.Module = None, link: Callable[..., str] = None):
docformat: Optional[str] = None,
module: Optional[pdoc.Module] = None,
link: Optional[Callable[..., str]] = None):
"""
Returns `text`, assumed to be a docstring in `docformat`, converted to markdown.
`__docformat__` is respected
Expand Down
8 changes: 4 additions & 4 deletions pdoc/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def run(*args, **kwargs) -> int:
params = list(filter(None, chain.from_iterable(params))) # type: ignore
_args = cli.parser.parse_args([*params, *args]) # type: ignore
try:
returncode = cli.main(_args)
return returncode or 0
cli.main(_args)
return 0
except SystemExit as e:
return e.code
return bool(e.code)


@contextmanager
Expand Down Expand Up @@ -953,7 +953,7 @@ def test_test_Function_params_python38_specific(self):
self.assertEqual(func.params(), ['a', '/'])

def test_Function_return_annotation(self):
def f() -> typing.List[typing.Union[str, pdoc.Doc]]: pass
def f() -> typing.List[typing.Union[str, pdoc.Doc]]: return []
func = pdoc.Function('f', DUMMY_PDOC_MODULE, f)
self.assertEqual(func.return_annotation(), 'List[Union[str,\N{NBSP}pdoc.Doc]]')

Expand Down

0 comments on commit 26c98f5

Please sign in to comment.