2
2
3
3
from __future__ import annotations
4
4
5
+ import warnings
5
6
from ast import PyCF_ONLY_AST
6
7
from contextlib import suppress
7
- from typing import TYPE_CHECKING , Protocol
8
+ from typing import TYPE_CHECKING , Protocol , overload
8
9
9
10
from _griffe .enumerations import LogLevel
10
11
from _griffe .exceptions import BuiltinModuleError
11
12
from _griffe .expressions import safe_get_annotation
13
+
14
+ # YORE: Bump 1.0.0: Replace `get_logger` with `logger` within line.
12
15
from _griffe .logger import get_logger
13
16
14
17
if TYPE_CHECKING :
15
18
from _griffe .expressions import Expr
16
19
from _griffe .models import Docstring
17
20
18
21
22
+ # YORE: Bump 1.0.0: Remove block.
19
23
class DocstringWarningCallable (Protocol ):
20
24
"""A callable that logs a warning message."""
21
25
@@ -30,23 +34,58 @@ def __call__(self, docstring: Docstring, offset: int, message: str, log_level: L
30
34
"""
31
35
32
36
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 ()
35
39
36
- Parameters:
37
- name: The logger name.
38
40
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.
41
69
42
70
This function logs a warning message by prefixing it with the filepath and line number.
43
71
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.
48
80
"""
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" )
50
89
51
90
def warn (docstring : Docstring , offset : int , message : str , log_level : LogLevel = LogLevel .warning ) -> None :
52
91
try :
@@ -58,7 +97,11 @@ def warn(docstring: Docstring, offset: int, message: str, log_level: LogLevel =
58
97
log = getattr (logger , log_level .value )
59
98
log (f"{ prefix } :{ (docstring .lineno or 0 )+ offset } : { message } " )
60
99
61
- return warn
100
+ if name is not None :
101
+ return warn
102
+
103
+ warn (docstring , offset , message , log_level )
104
+ return None
62
105
63
106
64
107
def parse_docstring_annotation (
0 commit comments