Skip to content

Commit 381f10f

Browse files
committed
refactor: Prepare loggers for simplification
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.
1 parent f601202 commit 381f10f

25 files changed

+202
-74
lines changed

config/pytest.ini

+4
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ filterwarnings =
1313
# TODO: remove once pytest-xdist 4 is released
1414
ignore:.*rsyncdir:DeprecationWarning:xdist
1515
ignore:.*slated for removal in Python:DeprecationWarning:.*
16+
# YORE: Bump 1.0.0: Remove line.
17+
ignore:.*`get_logger`:DeprecationWarning:_griffe
18+
# YORE: Bump 1.0.0: Remove line.
19+
ignore:.*`name`:DeprecationWarning:_griffe

docs/reference/api/loggers.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@
33
## **Main API**
44

55
::: griffe.DEFAULT_LOG_LEVEL
6+
options:
7+
annotations_path: full
68

7-
::: griffe.get_logger
9+
<!-- YORE: Bump 1.0.0: Uncomment line. -->
10+
<!-- ::: griffe.logger -->
11+
12+
::: griffe.Logger
813

914
::: griffe.LogLevel
1015

1116
## **Advanced API**
1217

18+
::: griffe.patch_logger
19+
20+
## **Deprecated API**
21+
22+
::: griffe.get_logger
23+
1324
::: griffe.patch_loggers

src/_griffe/agents/inspector.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
from _griffe.expressions import safe_get_annotation
3535
from _griffe.extensions.base import Extensions, load_extensions
3636
from _griffe.importer import dynamic_import
37+
38+
# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
39+
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
3740
from _griffe.logger import get_logger
3841
from _griffe.models import Alias, Attribute, Class, Docstring, Function, Module, Parameter, Parameters
3942

@@ -44,7 +47,7 @@
4447
from _griffe.expressions import Expr
4548

4649

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

src/_griffe/agents/nodes/assignments.py

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
import ast
66
from typing import Any, Callable
77

8-
from _griffe.logger import get_logger
9-
10-
# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
11-
_logger = get_logger("griffe.agents.nodes._names")
12-
138

149
def _get_attribute_name(node: ast.Attribute) -> str:
1510
return f"{get_name(node.value)}.{node.attr}"

src/_griffe/agents/nodes/ast.py

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
from typing import Iterator
77

88
from _griffe.exceptions import LastNodeError
9-
from _griffe.logger import get_logger
10-
11-
# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
12-
_logger = get_logger("griffe.agents.nodes._ast")
139

1410

1511
def ast_kind(node: AST) -> str:

src/_griffe/agents/nodes/docstrings.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
import ast
66

7+
# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
8+
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
79
from _griffe.logger import get_logger
810

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

1214

src/_griffe/agents/nodes/exports.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99

1010
from _griffe.agents.nodes.values import get_value
1111
from _griffe.enumerations import LogLevel
12+
13+
# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
14+
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
1215
from _griffe.logger import get_logger
1316

1417
if TYPE_CHECKING:
1518
from _griffe.models import Module
1619

1720

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

2124

src/_griffe/agents/nodes/imports.py

-6
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@
44

55
from typing import TYPE_CHECKING
66

7-
from _griffe.logger import get_logger
8-
97
if TYPE_CHECKING:
108
import ast
119

1210
from _griffe.models import Module
1311

1412

15-
# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
16-
_logger = get_logger("griffe.agents.nodes._imports")
17-
18-
1913
def relative_to_absolute(node: ast.ImportFrom, name: ast.alias, current_module: Module) -> str:
2014
"""Convert a relative import path to an absolute one.
2115

src/_griffe/agents/nodes/parameters.py

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
from typing import Iterable, List, Optional, Tuple, Union
88

99
from _griffe.enumerations import ParameterKind
10-
from _griffe.logger import get_logger
11-
12-
# YORE: Bump 1.0.0: Regex-replace `\.[^"]+` with `` within line.
13-
_logger = get_logger("griffe.agents.nodes._parameters")
1410

1511
ParametersType = List[Tuple[str, Optional[ast.AST], ParameterKind, Optional[Union[str, ast.AST]]]]
1612
"""Type alias for the list of parameters of a function."""

src/_griffe/agents/nodes/runtime.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
from typing import Any, ClassVar, Sequence
99

1010
from _griffe.enumerations import ObjectKind
11+
12+
# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
13+
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
1114
from _griffe.logger import get_logger
1215

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

1619
_builtin_module_names = {_.lstrip("_") for _ in sys.builtin_module_names}

src/_griffe/agents/nodes/values.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import sys
77
from typing import TYPE_CHECKING
88

9+
# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
10+
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
911
from _griffe.logger import get_logger
1012

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

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

2325

src/_griffe/cli.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
from _griffe.extensions.base import load_extensions
3333
from _griffe.git import get_latest_tag, get_repo_root
3434
from _griffe.loader import GriffeLoader, load, load_git
35+
36+
# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
37+
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
3538
from _griffe.logger import get_logger
3639

3740
if TYPE_CHECKING:
@@ -40,7 +43,7 @@
4043

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

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

4649

src/_griffe/diff.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from _griffe.enumerations import BreakageKind, ExplanationStyle, ParameterKind
1313
from _griffe.exceptions import AliasResolutionError
1414
from _griffe.git import _WORKTREE_PREFIX
15+
16+
# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
17+
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
1518
from _griffe.logger import get_logger
1619

1720
if TYPE_CHECKING:
@@ -22,7 +25,7 @@
2225
_POSITIONAL_KEYWORD_ONLY = frozenset((ParameterKind.positional_only, ParameterKind.keyword_only))
2326
_VARIADIC = frozenset((ParameterKind.var_positional, ParameterKind.var_keyword))
2427

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

2831

src/_griffe/docstrings/google.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
from _griffe.expressions import Expr
4545
from _griffe.models import Docstring
4646

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

5051
_section_kind = {

src/_griffe/docstrings/numpy.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
from _griffe.models import Docstring
6464

6565

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

6970
_section_kind = {

src/_griffe/docstrings/sphinx.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
from _griffe.expressions import Expr
3030
from _griffe.models import Docstring
3131

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

3536
# TODO: Examples: from the documentation, we're not sure there is a standard format for examples

src/_griffe/docstrings/utils.py

+56-13
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
from __future__ import annotations
44

5+
import warnings
56
from ast import PyCF_ONLY_AST
67
from contextlib import suppress
7-
from typing import TYPE_CHECKING, Protocol
8+
from typing import TYPE_CHECKING, Protocol, overload
89

910
from _griffe.enumerations import LogLevel
1011
from _griffe.exceptions import BuiltinModuleError
1112
from _griffe.expressions import safe_get_annotation
13+
14+
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
1215
from _griffe.logger import get_logger
1316

1417
if TYPE_CHECKING:
1518
from _griffe.expressions import Expr
1619
from _griffe.models import Docstring
1720

1821

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

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

3236

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

36-
Parameters:
37-
name: The logger name.
3840

39-
Returns:
40-
A function used to log parsing warnings.
41+
# YORE: Bump 1.0.0: Remove block.
42+
@overload
43+
def docstring_warning(name: str) -> DocstringWarningCallable: ...
44+
45+
46+
# YORE: Bump 1.0.0: Remove block.
47+
@overload
48+
def docstring_warning(
49+
docstring: Docstring,
50+
offset: int,
51+
message: str,
52+
log_level: LogLevel = LogLevel.warning,
53+
) -> None: ...
54+
55+
56+
def docstring_warning( # type: ignore[misc]
57+
# YORE: Bump 1.0.0: Remove line.
58+
name: str | None = None,
59+
# YORE: Bump 1.0.0: Replace line with `docstring: Docstring,`.
60+
docstring: Docstring = _sentinel, # type: ignore[assignment]
61+
# YORE: Bump 1.0.0: Replace line with `offset: int,`.
62+
offset: int = _sentinel, # type: ignore[assignment]
63+
# YORE: Bump 1.0.0: Replace line with `message: str,`.
64+
message: str = _sentinel, # type: ignore[assignment]
65+
log_level: LogLevel = LogLevel.warning,
66+
# YORE: Bump 1.0.0: Replace line with `) -> None:`.
67+
) -> DocstringWarningCallable | None:
68+
"""Log a warning when parsing a docstring.
4169
4270
This function logs a warning message by prefixing it with the filepath and line number.
4371
44-
Other parameters: Parameters of the returned function:
45-
docstring (Docstring): The docstring object.
46-
offset (int): The offset in the docstring lines.
47-
message (str): The message to log.
72+
Parameters:
73+
name: Deprecated. If passed, the function returns a callable, and other arguments are ignored.
74+
docstring: The docstring object.
75+
offset: The offset in the docstring lines.
76+
message: The message to log.
77+
78+
Returns:
79+
A function used to log parsing warnings if `name` was passed, else none.
4880
"""
49-
logger = get_logger(name)
81+
# YORE: Bump 1.0.0: Remove block.
82+
if name is not None:
83+
warnings.warn("The `name` parameter is deprecated.", DeprecationWarning, stacklevel=1)
84+
logger = get_logger(name)
85+
else:
86+
if docstring is _sentinel or offset is _sentinel or message is _sentinel:
87+
raise ValueError("Missing required arguments docstring/offset/message.")
88+
logger = get_logger("griffe")
5089

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

61-
return warn
100+
if name is not None:
101+
return warn
102+
103+
warn(docstring, offset, message, log_level)
104+
return None
62105

63106

64107
def parse_docstring_annotation(

src/_griffe/expressions.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
from _griffe.agents.nodes.parameters import get_parameters
1414
from _griffe.enumerations import LogLevel, ParameterKind
1515
from _griffe.exceptions import NameResolutionError
16+
17+
# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
18+
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
1619
from _griffe.logger import get_logger
1720

1821
if TYPE_CHECKING:
@@ -21,7 +24,7 @@
2124
from _griffe.models import Class, Module
2225

2326

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

2730

src/_griffe/finder.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@
2525
from typing import TYPE_CHECKING, ClassVar, Iterator, Sequence, Tuple
2626

2727
from _griffe.exceptions import UnhandledEditableModuleError
28+
29+
# YORE: Bump 1.0.0: Replace `_logger` with `logger` within file.
30+
# YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
2831
from _griffe.logger import get_logger
2932

3033
if TYPE_CHECKING:
3134
from typing import Pattern
3235

3336
from _griffe.models import Module
3437

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

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

0 commit comments

Comments
 (0)