diff --git a/pelican/__init__.py b/pelican/__init__.py index ead3fa7a..a69e574b 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -266,7 +266,7 @@ class PrintSettings(argparse.Action): for setting in values: if setting in settings: # Only add newline between setting name and value if dict - if isinstance(settings[setting], (dict, tuple, list)): + if isinstance(settings[setting], dict | tuple | list): setting_format = "\n{}:\n{}" else: setting_format = "\n{}: {}" diff --git a/pelican/contents.py b/pelican/contents.py index a06a1a6a..8494145a 100644 --- a/pelican/contents.py +++ b/pelican/contents.py @@ -5,7 +5,7 @@ import logging import os import re from html import unescape -from typing import Any, Optional +from typing import Any from urllib.parse import ParseResult, unquote, urljoin, urlparse, urlunparse try: @@ -45,7 +45,7 @@ class Content: """ - default_template: Optional[str] = None + default_template: str | None = None mandatory_properties: tuple[str, ...] = () @deprecated_attribute(old="filename", new="source_path", since=(3, 2, 0)) @@ -55,10 +55,10 @@ class Content: def __init__( self, content: str, - metadata: Optional[dict[str, Any]] = None, - settings: Optional[Settings] = None, - source_path: Optional[str] = None, - context: Optional[dict[Any, Any]] = None, + metadata: dict[str, Any] | None = None, + settings: Settings | None = None, + source_path: str | None = None, + context: dict[Any, Any] | None = None, ): if metadata is None: metadata = {} @@ -241,7 +241,7 @@ class Content: ) return metadata - def _expand_settings(self, key: str, klass: Optional[str] = None) -> str: + def _expand_settings(self, key: str, klass: str | None = None) -> str: if not klass: klass = self.__class__.__name__ fq_key = (f"{klass}_{key}").upper() @@ -281,10 +281,10 @@ class Content: # XXX Put this in a different location. if what in {"filename", "static", "attach"}: - def _get_linked_content(key: str, url: ParseResult) -> Optional[Content]: + def _get_linked_content(key: str, url: ParseResult) -> Content | None: nonlocal value - def _find_path(path: str) -> Optional[Content]: + def _find_path(path: str) -> Content | None: if path.startswith("/"): path = path[1:] else: @@ -494,9 +494,7 @@ class Content: else: return self.default_template - def get_relative_source_path( - self, source_path: Optional[str] = None - ) -> Optional[str]: + def get_relative_source_path(self, source_path: str | None = None) -> str | None: """Return the relative path (from the content path) to the given source_path. diff --git a/pelican/generators.py b/pelican/generators.py index bf1d1cb8..3b867dfb 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -7,7 +7,6 @@ from collections import defaultdict from functools import partial from itertools import chain, groupby from operator import attrgetter -from typing import Optional from jinja2 import ( BaseLoader, @@ -158,7 +157,7 @@ class Generator: return False def get_files( - self, paths, exclude: Optional[list[str]] = None, extensions=None + self, paths, exclude: list[str] | None = None, extensions=None ) -> set[str]: """Return a list of files to use, based on rules @@ -253,7 +252,7 @@ class Generator: # return the name of the class for logging purposes return self.__class__.__name__ - def _check_disabled_readers(self, paths, exclude: Optional[list[str]]) -> None: + def _check_disabled_readers(self, paths, exclude: list[str] | None) -> None: """Log warnings for files that would have been processed by disabled readers.""" for fil in self.get_files( paths, exclude=exclude, extensions=self.readers.disabled_extensions diff --git a/pelican/settings.py b/pelican/settings.py index 2f3f8fa1..98b1357e 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -9,7 +9,7 @@ import sys from os.path import isabs from pathlib import Path from types import ModuleType -from typing import Any, Optional +from typing import Any from pelican.log import LimitFilter from pelican.paginator import PaginationRule @@ -185,7 +185,7 @@ PYGMENTS_RST_OPTIONS = None def read_settings( - path: Optional[str] = None, override: Optional[Settings] = None + path: str | None = None, override: Settings | None = None ) -> Settings: settings = override or {} @@ -230,7 +230,7 @@ def read_settings( return settings -def get_settings_from_module(module: Optional[ModuleType] = None) -> Settings: +def get_settings_from_module(module: ModuleType | None = None) -> Settings: """Loads settings from a module, returns a dictionary.""" context = {} diff --git a/pelican/utils.py b/pelican/utils.py index 0dd36a1f..a5e08538 100644 --- a/pelican/utils.py +++ b/pelican/utils.py @@ -11,7 +11,14 @@ import shutil import traceback import unicodedata import urllib -from collections.abc import Collection, Generator, Hashable, Iterable, Sequence +from collections.abc import ( + Callable, + Collection, + Generator, + Hashable, + Iterable, + Sequence, +) from contextlib import contextmanager from functools import partial from html import entities @@ -21,7 +28,6 @@ from operator import attrgetter from typing import ( TYPE_CHECKING, Any, - Callable, ) import dateutil.parser @@ -234,7 +240,7 @@ def get_date(string: str) -> datetime.datetime: @contextmanager -def pelican_open(filename: str, mode: str = "r") -> Generator[str, None, None]: +def pelican_open(filename: str, mode: str = "r") -> Generator[str]: """Open a file and return its content""" # utf-8-sig will clear any BOM if present @@ -932,7 +938,7 @@ def maybe_pluralize(count: int, singular: str, plural: str) -> str: @contextmanager def temporary_locale( temp_locale: str | None = None, lc_category: int = locale.LC_ALL -) -> Generator[None, None, None]: +) -> Generator[None]: """ Enable code to run in a context with a temporary locale Resets the locale back when exiting context.