Skip to content

Commit

Permalink
Upgrade type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Oct 9, 2022
1 parent 657f88b commit 9abf1d7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions mkdocs_gen_files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Then `mkdocs_gen_files.open()` is the same as `mkdocs_gen_files.editor.FilesEditor.current.open()`.
"""
from __future__ import annotations

import logging

Expand Down
2 changes: 2 additions & 0 deletions mkdocs_gen_files/config_items.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from mkdocs.config.config_options import File, OptionallyRequired, ValidationError


Expand Down
16 changes: 9 additions & 7 deletions mkdocs_gen_files/editor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import collections
import os
import os.path
import pathlib
import shutil
from typing import IO, ClassVar, MutableMapping, Optional
from typing import IO, ClassVar, MutableMapping

from mkdocs.config import Config, load_config
from mkdocs.structure.files import File, Files
Expand All @@ -21,7 +23,7 @@ class FilesEditor:
"""The current MkDocs [config](https://www.mkdocs.org/user-guide/plugins/#config)."""
directory: str
"""The base directory for `open()` ([docs_dir](https://www.mkdocs.org/user-guide/configuration/#docs_dir))."""
edit_paths: MutableMapping[str, Optional[str]]
edit_paths: MutableMapping[str, str | None]

def open(self, name: str, mode, buffering=-1, encoding=None, *args, **kwargs) -> IO:
"""Open a file under `docs_dir` virtually.
Expand Down Expand Up @@ -61,11 +63,11 @@ def _get_file(self, name: str, new: bool = False) -> str:

return f.abs_src_path

def set_edit_path(self, name: str, edit_name: Optional[str]) -> None:
def set_edit_path(self, name: str, edit_name: str | None) -> None:
"""Choose a file path to use for the edit URI of this file."""
self.edit_paths[pathlib.PurePath(name).as_posix()] = edit_name and str(edit_name)

def __init__(self, files: Files, config: Config, directory: Optional[str] = None):
def __init__(self, files: Files, config: Config, directory: str | None = None):
self._files: MutableMapping[str, File] = collections.ChainMap(
{}, {pathlib.PurePath(f.src_path).as_posix(): f for f in files}
)
Expand All @@ -75,11 +77,11 @@ def __init__(self, files: Files, config: Config, directory: Optional[str] = None
self.directory = directory
self.edit_paths = {}

_current: ClassVar[Optional["FilesEditor"]] = None
_default: ClassVar[Optional["FilesEditor"]] = None
_current: ClassVar[FilesEditor | None] = None
_default: ClassVar[FilesEditor | None] = None

@classmethod
def current(cls) -> "FilesEditor":
def current(cls) -> FilesEditor:
"""The instance of FilesEditor associated with the currently ongoing MkDocs build.
If used as part of a MkDocs build (*gen-files* plugin), it's an instance using virtual
Expand Down
10 changes: 6 additions & 4 deletions mkdocs_gen_files/nav.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

import dataclasses
import os
from typing import Iterable, Mapping, Optional, Tuple, Union
from typing import Iterable, Mapping


class Nav:
Expand All @@ -11,7 +13,7 @@ class Nav:
def __init__(self):
self._data = {}

def __setitem__(self, keys: Union[str, Tuple[str, ...]], value: str):
def __setitem__(self, keys: str | tuple[str, ...], value: str):
"""Add a link to a file (*value*) into the nav, under the sequence of titles (*keys*).
For example, writing `nav["Foo", "Bar"] = "foo/bar.md"` would mean creating a nav:
Expand Down Expand Up @@ -43,7 +45,7 @@ def __setitem__(self, keys: Union[str, Tuple[str, ...]], value: str):
class Item:
level: int
title: str
filename: Optional[str]
filename: str | None

def items(self) -> Iterable[Item]:
return self._items(self._data, 0)
Expand All @@ -57,7 +59,7 @@ def _items(cls, data: Mapping, level: int) -> Iterable[Item]:

_markdown_escape_chars = tuple("!#()*+-[\\]_`{}")

def build_literate_nav(self, indentation: Union[int, str] = "") -> Iterable[str]:
def build_literate_nav(self, indentation: int | str = "") -> Iterable[str]:
if isinstance(indentation, int):
indentation = " " * indentation
for item in self.items():
Expand Down
2 changes: 2 additions & 0 deletions mkdocs_gen_files/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import pathlib
import runpy
Expand Down

0 comments on commit 9abf1d7

Please sign in to comment.