Skip to content

Commit

Permalink
refactor: Prepare loggers for simplification
Browse files Browse the repository at this point in the history
Before this commit we supported having multiple loggers. After this commit we only use one single, global logger, with the name `"griffe"`. It allows us to simplify a few other things related to logging.

To keep this backward compatible before v1, we mainly used Yore comments.
  • Loading branch information
pawamoy committed Jul 14, 2024
1 parent f601202 commit 381f10f
Show file tree
Hide file tree
Showing 25 changed files with 202 additions and 74 deletions.
4 changes: 4 additions & 0 deletions config/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ filterwarnings =
# TODO: remove once pytest-xdist 4 is released
ignore:.*rsyncdir:DeprecationWarning:xdist
ignore:.*slated for removal in Python:DeprecationWarning:.*
# YORE: Bump 1.0.0: Remove line.
ignore:.*`get_logger`:DeprecationWarning:_griffe
# YORE: Bump 1.0.0: Remove line.
ignore:.*`name`:DeprecationWarning:_griffe
13 changes: 12 additions & 1 deletion docs/reference/api/loggers.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@
## **Main API**

::: griffe.DEFAULT_LOG_LEVEL
options:
annotations_path: full

::: griffe.get_logger
<!-- YORE: Bump 1.0.0: Uncomment line. -->
<!-- ::: griffe.logger -->

::: griffe.Logger

::: griffe.LogLevel

## **Advanced API**

::: griffe.patch_logger

## **Deprecated API**

::: griffe.get_logger

::: griffe.patch_loggers
5 changes: 4 additions & 1 deletion src/_griffe/agents/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
from _griffe.expressions import safe_get_annotation
from _griffe.extensions.base import Extensions, load_extensions
from _griffe.importer import dynamic_import

# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
from _griffe.logger import get_logger
from _griffe.models import Alias, Attribute, Class, Docstring, Function, Module, Parameter, Parameters

Expand All @@ -44,7 +47,7 @@
from _griffe.expressions import Expr


# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Remove line.
_logger = get_logger("griffe.agents.inspector")
_empty = Signature.empty

Expand Down
5 changes: 0 additions & 5 deletions src/_griffe/agents/nodes/assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
import ast
from typing import Any, Callable

from _griffe.logger import get_logger

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
_logger = get_logger("griffe.agents.nodes._names")


def _get_attribute_name(node: ast.Attribute) -> str:
return f"{get_name(node.value)}.{node.attr}"
Expand Down
4 changes: 0 additions & 4 deletions src/_griffe/agents/nodes/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
from typing import Iterator

from _griffe.exceptions import LastNodeError
from _griffe.logger import get_logger

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
_logger = get_logger("griffe.agents.nodes._ast")


def ast_kind(node: AST) -> str:
Expand Down
4 changes: 3 additions & 1 deletion src/_griffe/agents/nodes/docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

import ast

# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
from _griffe.logger import get_logger

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Remove line.
_logger = get_logger("griffe.agents.nodes._docstrings")


Expand Down
5 changes: 4 additions & 1 deletion src/_griffe/agents/nodes/exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

from _griffe.agents.nodes.values import get_value
from _griffe.enumerations import LogLevel

# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
from _griffe.logger import get_logger

if TYPE_CHECKING:
from _griffe.models import Module


# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Remove line.
_logger = get_logger("griffe.agents.nodes._all")


Expand Down
6 changes: 0 additions & 6 deletions src/_griffe/agents/nodes/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@

from typing import TYPE_CHECKING

from _griffe.logger import get_logger

if TYPE_CHECKING:
import ast

from _griffe.models import Module


# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
_logger = get_logger("griffe.agents.nodes._imports")


def relative_to_absolute(node: ast.ImportFrom, name: ast.alias, current_module: Module) -> str:
"""Convert a relative import path to an absolute one.
Expand Down
4 changes: 0 additions & 4 deletions src/_griffe/agents/nodes/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
from typing import Iterable, List, Optional, Tuple, Union

from _griffe.enumerations import ParameterKind
from _griffe.logger import get_logger

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
_logger = get_logger("griffe.agents.nodes._parameters")

ParametersType = List[Tuple[str, Optional[ast.AST], ParameterKind, Optional[Union[str, ast.AST]]]]
"""Type alias for the list of parameters of a function."""
Expand Down
5 changes: 4 additions & 1 deletion src/_griffe/agents/nodes/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
from typing import Any, ClassVar, Sequence

from _griffe.enumerations import ObjectKind

# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
from _griffe.logger import get_logger

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Remove line.
_logger = get_logger("griffe.agents.nodes._runtime")

_builtin_module_names = {_.lstrip("_") for _ in sys.builtin_module_names}
Expand Down
4 changes: 3 additions & 1 deletion src/_griffe/agents/nodes/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import sys
from typing import TYPE_CHECKING

# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
from _griffe.logger import get_logger

# YORE: EOL 3.8: Replace block with line 4.
Expand All @@ -17,7 +19,7 @@
if TYPE_CHECKING:
from pathlib import Path

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Remove line.
_logger = get_logger("griffe.agents.nodes._values")


Expand Down
5 changes: 4 additions & 1 deletion src/_griffe/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
from _griffe.extensions.base import load_extensions
from _griffe.git import get_latest_tag, get_repo_root
from _griffe.loader import GriffeLoader, load, load_git

# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
from _griffe.logger import get_logger

if TYPE_CHECKING:
Expand All @@ -40,7 +43,7 @@

DEFAULT_LOG_LEVEL = os.getenv("GRIFFE_LOG_LEVEL", "INFO").upper()

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Remove line.
_logger = get_logger("griffe.cli")


Expand Down
5 changes: 4 additions & 1 deletion src/_griffe/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from _griffe.enumerations import BreakageKind, ExplanationStyle, ParameterKind
from _griffe.exceptions import AliasResolutionError
from _griffe.git import _WORKTREE_PREFIX

# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
from _griffe.logger import get_logger

if TYPE_CHECKING:
Expand All @@ -22,7 +25,7 @@
_POSITIONAL_KEYWORD_ONLY = frozenset((ParameterKind.positional_only, ParameterKind.keyword_only))
_VARIADIC = frozenset((ParameterKind.var_positional, ParameterKind.var_keyword))

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Remove line.
_logger = get_logger("griffe.diff")


Expand Down
3 changes: 2 additions & 1 deletion src/_griffe/docstrings/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
from _griffe.expressions import Expr
from _griffe.models import Docstring

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Regex-replace `\b_warn\b` with `docstring_warning` within file.
# YORE: Bump 1.0.0: Remove line.
_warn = docstring_warning("griffe.docstrings.google")

_section_kind = {
Expand Down
3 changes: 2 additions & 1 deletion src/_griffe/docstrings/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
from _griffe.models import Docstring


# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Regex-replace `\b_warn\b` with `docstring_warning` within file.
# YORE: Bump 1.0.0: Remove line.
_warn = docstring_warning("griffe.docstrings.numpy")

_section_kind = {
Expand Down
3 changes: 2 additions & 1 deletion src/_griffe/docstrings/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
from _griffe.expressions import Expr
from _griffe.models import Docstring

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Regex-replace `\b_warn\b` with `docstring_warning` within file.
# YORE: Bump 1.0.0: Remove line.
_warn = docstring_warning("griffe.docstrings.sphinx")

# TODO: Examples: from the documentation, we're not sure there is a standard format for examples
Expand Down
69 changes: 56 additions & 13 deletions src/_griffe/docstrings/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

from __future__ import annotations

import warnings
from ast import PyCF_ONLY_AST
from contextlib import suppress
from typing import TYPE_CHECKING, Protocol
from typing import TYPE_CHECKING, Protocol, overload

from _griffe.enumerations import LogLevel
from _griffe.exceptions import BuiltinModuleError
from _griffe.expressions import safe_get_annotation

# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
from _griffe.logger import get_logger

if TYPE_CHECKING:
from _griffe.expressions import Expr
from _griffe.models import Docstring


# YORE: Bump 1.0.0: Remove block.
class DocstringWarningCallable(Protocol):
"""A callable that logs a warning message."""

Expand All @@ -30,23 +34,58 @@ def __call__(self, docstring: Docstring, offset: int, message: str, log_level: L
"""


def docstring_warning(name: str) -> DocstringWarningCallable:
"""Create and return a warn function.
# YORE: Bump 1.0.0: Remove line.
_sentinel = object()

Parameters:
name: The logger name.

Returns:
A function used to log parsing warnings.
# YORE: Bump 1.0.0: Remove block.
@overload
def docstring_warning(name: str) -> DocstringWarningCallable: ...


# YORE: Bump 1.0.0: Remove block.
@overload
def docstring_warning(
docstring: Docstring,
offset: int,
message: str,
log_level: LogLevel = LogLevel.warning,
) -> None: ...


def docstring_warning( # type: ignore[misc]
# YORE: Bump 1.0.0: Remove line.
name: str | None = None,
# YORE: Bump 1.0.0: Replace line with `docstring: Docstring,`.
docstring: Docstring = _sentinel, # type: ignore[assignment]
# YORE: Bump 1.0.0: Replace line with `offset: int,`.
offset: int = _sentinel, # type: ignore[assignment]
# YORE: Bump 1.0.0: Replace line with `message: str,`.
message: str = _sentinel, # type: ignore[assignment]
log_level: LogLevel = LogLevel.warning,
# YORE: Bump 1.0.0: Replace line with `) -> None:`.
) -> DocstringWarningCallable | None:
"""Log a warning when parsing a docstring.
This function logs a warning message by prefixing it with the filepath and line number.
Other parameters: Parameters of the returned function:
docstring (Docstring): The docstring object.
offset (int): The offset in the docstring lines.
message (str): The message to log.
Parameters:
name: Deprecated. If passed, the function returns a callable, and other arguments are ignored.
docstring: The docstring object.
offset: The offset in the docstring lines.
message: The message to log.
Returns:
A function used to log parsing warnings if `name` was passed, else none.
"""
logger = get_logger(name)
# YORE: Bump 1.0.0: Remove block.
if name is not None:
warnings.warn("The `name` parameter is deprecated.", DeprecationWarning, stacklevel=1)
logger = get_logger(name)
else:
if docstring is _sentinel or offset is _sentinel or message is _sentinel:
raise ValueError("Missing required arguments docstring/offset/message.")
logger = get_logger("griffe")

def warn(docstring: Docstring, offset: int, message: str, log_level: LogLevel = LogLevel.warning) -> None:
try:
Expand All @@ -58,7 +97,11 @@ def warn(docstring: Docstring, offset: int, message: str, log_level: LogLevel =
log = getattr(logger, log_level.value)
log(f"{prefix}:{(docstring.lineno or 0)+offset}: {message}")

return warn
if name is not None:
return warn

warn(docstring, offset, message, log_level)
return None


def parse_docstring_annotation(
Expand Down
5 changes: 4 additions & 1 deletion src/_griffe/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from _griffe.agents.nodes.parameters import get_parameters
from _griffe.enumerations import LogLevel, ParameterKind
from _griffe.exceptions import NameResolutionError

# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
from _griffe.logger import get_logger

if TYPE_CHECKING:
Expand All @@ -21,7 +24,7 @@
from _griffe.models import Class, Module


# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Remove line.
_logger = get_logger("griffe.expressions")


Expand Down
5 changes: 4 additions & 1 deletion src/_griffe/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
from typing import TYPE_CHECKING, ClassVar, Iterator, Sequence, Tuple

from _griffe.exceptions import UnhandledEditableModuleError

# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
from _griffe.logger import get_logger

if TYPE_CHECKING:
from typing import Pattern

from _griffe.models import Module

# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
# YORE: Bump 1.0.0: Remove line.
_logger = get_logger("griffe.finder")

_editable_editables_patterns = [re.compile(pat) for pat in (r"^__editables_\w+\.py$", r"^_editable_impl_\w+\.py$")]
Expand Down
Loading

0 comments on commit 381f10f

Please # to comment.