Skip to content

Commit

Permalink
chore: typing
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Oct 23, 2024
1 parent bf17035 commit 773777c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/jinjarope/envglobals.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


if TYPE_CHECKING:
from collections.abc import Sequence
from collections.abc import Callable, Sequence
import os


Expand Down Expand Up @@ -150,7 +150,7 @@ def utcnow() -> datetime.datetime:
return datetime.datetime.now(datetime.UTC)


ENV_GLOBALS = {
ENV_GLOBALS: dict[str, Callable[..., Any]] = {
"range": range,
"zip": zip,
"set": set,
Expand Down
5 changes: 4 additions & 1 deletion src/jinjarope/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ def add_template_path(self, *path: str | os.PathLike[str]):
new_loader = loaders.FileSystemLoader(p)
self._add_loader(new_loader)

def _add_loader(self, new_loader: jinja2.BaseLoader | dict | str | os.PathLike[str]):
def _add_loader(
self,
new_loader: jinja2.BaseLoader | dict[str, str] | str | os.PathLike[str],
):
match new_loader:
case dict():
new_loader = loaders.DictLoader(new_loader)
Expand Down
7 changes: 2 additions & 5 deletions src/jinjarope/iterfilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from collections.abc import Callable, Generator, Iterable, Mapping
import itertools
import operator
from typing import Any, TypeVar


Expand Down Expand Up @@ -212,14 +213,12 @@ def natsort(
reverse: Whether to reverse the sort order
ignore_case: Whether to ignore case for sorting
"""
from operator import attrgetter

from natsort import natsorted, ns

alg = ns.IGNORECASE
if not ignore_case:
alg = ns.LOWERCASEFIRST
key_fn = attrgetter(key) if isinstance(key, str) else key
key_fn = operator.attrgetter(key) if isinstance(key, str) else key
return natsorted(val, key=key_fn, reverse=reverse, alg=alg)


Expand All @@ -246,8 +245,6 @@ def keyfunc(x):
return x

elif isinstance(key, str):
import operator

keyfunc = operator.attrgetter(key)
else:
keyfunc = key
Expand Down
6 changes: 3 additions & 3 deletions src/jinjarope/mdfilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Callable
import re
import types
from typing import Literal
from typing import Any, Literal


HEADER_REGEX = re.compile(r"^(#{1,6}) (.*)", flags=re.MULTILINE)
Expand Down Expand Up @@ -113,7 +113,7 @@ def shift_header_levels(text: str, level_shift: int) -> str:
if not level_shift:
return text

def mod_header(match: re.Match, levels: int) -> str:
def mod_header(match: re.Match[str], levels: int) -> str:
header_str = match[1]
if levels > 0:
header_str += levels * "#"
Expand All @@ -126,7 +126,7 @@ def mod_header(match: re.Match, levels: int) -> str:

def autoref_link(
text: str | None = None,
link: str | types.ModuleType | Callable | None = None,
link: str | types.ModuleType | Callable[..., Any] | None = None,
) -> str:
"""Create a markdown autorefs-style link (used by MkDocs / MkDocStrings).
Expand Down
2 changes: 1 addition & 1 deletion src/jinjarope/regexfilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def re_search(
*args: str,
ignorecase: bool = False,
multiline: bool = False,
) -> list | None | str:
) -> list[str] | None | str:
"""Perform re.search and return the list of matches or a backref.
Filter adapted from Ansible
Expand Down

0 comments on commit 773777c

Please sign in to comment.