Skip to content

Commit

Permalink
Type fixes according to mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Aug 21, 2022
1 parent a4aa8c5 commit 090a490
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
10 changes: 6 additions & 4 deletions mkdocs_section_index/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from typing import Sequence
from typing import List, Union

from mkdocs.config import Config
from mkdocs.structure.files import File
from mkdocs.structure.nav import Section
from mkdocs.structure.nav import Link, Section
from mkdocs.structure.pages import Page

__all__ = ["SectionPage"]


class SectionPage(Section, Page):
def __init__(self, title: str, file: File, config: Config, children: Sequence):
class SectionPage(Section, Page): # type: ignore[misc]
def __init__(
self, title: str, file: File, config: Config, children: List[Union[Page, Section, Link]]
):
Page.__init__(self, title=title, file=file, config=config)
Section.__init__(self, title=title, children=children)
self.is_section = self.is_page = True
Expand Down
2 changes: 2 additions & 0 deletions mkdocs_section_index/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def on_nav(self, nav: Navigation, config, files) -> Navigation:
if not page.title and page.url:
# The page becomes a section-page.
page.__class__ = SectionPage
assert isinstance(page, SectionPage)
page.is_section = page.is_page = True
page.title = section.title
# The page leaves the section but takes over children that used to be its peers.
Expand All @@ -44,6 +45,7 @@ def on_nav(self, nav: Navigation, config, files) -> Navigation:
return nav

def on_env(self, env: Environment, config, files) -> Environment:
assert env.loader is not None
env.loader = self._loader = rewrites.TemplateRewritingLoader(env.loader)
return env

Expand Down
9 changes: 7 additions & 2 deletions mkdocs_section_index/rewrites.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import pathlib
import textwrap
from typing import Optional, Tuple
from typing import Callable, Optional, Tuple

import mkdocs.utils
from jinja2 import BaseLoader, Environment
Expand All @@ -18,9 +18,13 @@ def __init__(self, loader: BaseLoader):
self.loader = loader
self.found_supported_theme = False

def get_source(self, environment: Environment, template: str) -> Tuple[str, str, bool]:
def get_source(
self, environment: Environment, template: str
) -> Tuple[str, str, Optional[Callable[[], bool]]]:
src: Optional[str]
src, filename, uptodate = self.loader.get_source(environment, template)
old_src = src
assert filename is not None
path = pathlib.Path(filename).as_posix()

if path.endswith("/mkdocs/templates/sitemap.xml"):
Expand Down Expand Up @@ -51,6 +55,7 @@ def _transform_mkdocs_sitemap_template(src: str) -> Optional[str]:
"{%- else %}",
"{%- endif %}{% if item.url %}",
)
return None


def _transform_material_nav_item_template(src: str) -> str:
Expand Down

0 comments on commit 090a490

Please sign in to comment.