Skip to content

Commit a266633

Browse files
committed
added a docstring to colored_warning and can accept a Warning subclass
1 parent 092f015 commit a266633

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

piccolo/utils/warnings.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from __future__ import annotations
12
from enum import Enum
3+
import typing as t
24
import warnings
35

46
import colorama # type: ignore
@@ -14,7 +16,25 @@ class Level(Enum):
1416

1517

1618
def colored_warning(
17-
message: str, stacklevel: int = 3, level: Level = Level.medium
19+
message: str,
20+
category: t.Type[Warning] = Warning,
21+
stacklevel: int = 3,
22+
level: Level = Level.medium,
1823
):
24+
"""
25+
A wrapper around the stdlib's `warnings.warn`, which colours the output.
26+
27+
:param message:
28+
The message to display to the user
29+
:category:
30+
`Warning` has several subclasses which may be more appropriate, for
31+
example `DeprecationWarning`.
32+
:stacklevel:
33+
Used to determine there the source of the error within the source code.
34+
See the Python docs for more detail.
35+
https://docs.python.org/3/library/warnings.html#warnings.warn
36+
:level:
37+
Used to determine the colour of the text displayed to the user.
38+
"""
1939
colored_message = level.value + message + colorama.Fore.RESET
20-
warnings.warn(colored_message, stacklevel=stacklevel)
40+
warnings.warn(colored_message, category=category, stacklevel=stacklevel)

0 commit comments

Comments
 (0)