Skip to content

Commit c9f7e91

Browse files
committed
fix: split split_format to keep lru_cache but use warning every time
1 parent bdbd502 commit c9f7e91

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

pint/delegates/formatter/_spec_helpers.py

+39-23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
2-
pint.delegates.formatter._spec_helpers
3-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2+
pint.delegates.formatter._spec_helpers
3+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44
5-
Convenient functions to deal with format specifications.
5+
Convenient functions to deal with format specifications.
66
7-
:copyright: 2022 by Pint Authors, see AUTHORS for more details.
8-
:license: BSD, see LICENSE for more details.
7+
:copyright: 2022 by Pint Authors, see AUTHORS for more details.
8+
:license: BSD, see LICENSE for more details.
99
"""
1010

1111
from __future__ import annotations
@@ -87,45 +87,61 @@ def remove_custom_flags(spec: str) -> str:
8787
return spec
8888

8989

90+
##########
91+
# This weird way of defining split format
92+
# is the only reasonable way I foudn to use
93+
# lru_cache in a function that might emit warning
94+
# and do it every time.
95+
# TODO: simplify it when there are no warnings.
96+
97+
9098
@functools.lru_cache
91-
def split_format(
99+
def _split_format(
92100
spec: str, default: str, separate_format_defaults: bool = True
93-
) -> tuple[str, str]:
101+
) -> tuple[str, str, list[str]]:
94102
"""Split format specification into magnitude and unit format."""
95103
mspec = remove_custom_flags(spec)
96104
uspec = extract_custom_flags(spec)
97105

98106
default_mspec = remove_custom_flags(default)
99107
default_uspec = extract_custom_flags(default)
100108

109+
warns = []
101110
if separate_format_defaults in (False, None):
102111
# should we warn always or only if there was no explicit choice?
103112
# Given that we want to eventually remove the flag again, I'd say yes?
104113
if spec and separate_format_defaults is None:
105114
if not uspec and default_uspec:
106-
warnings.warn(
107-
(
108-
"The given format spec does not contain a unit formatter."
109-
" Falling back to the builtin defaults, but in the future"
110-
" the unit formatter specified in the `default_format`"
111-
" attribute will be used instead."
112-
),
113-
DeprecationWarning,
115+
warns.append(
116+
"The given format spec does not contain a unit formatter."
117+
" Falling back to the builtin defaults, but in the future"
118+
" the unit formatter specified in the `default_format`"
119+
" attribute will be used instead."
114120
)
115121
if not mspec and default_mspec:
116-
warnings.warn(
117-
(
118-
"The given format spec does not contain a magnitude formatter."
119-
" Falling back to the builtin defaults, but in the future"
120-
" the magnitude formatter specified in the `default_format`"
121-
" attribute will be used instead."
122-
),
123-
DeprecationWarning,
122+
warns.append(
123+
"The given format spec does not contain a magnitude formatter."
124+
" Falling back to the builtin defaults, but in the future"
125+
" the magnitude formatter specified in the `default_format`"
126+
" attribute will be used instead."
124127
)
125128
elif not spec:
126129
mspec, uspec = default_mspec, default_uspec
127130
else:
128131
mspec = mspec or default_mspec
129132
uspec = uspec or default_uspec
130133

134+
return mspec, uspec, warns
135+
136+
137+
def split_format(
138+
spec: str, default: str, separate_format_defaults: bool = True
139+
) -> tuple[str, str]:
140+
"""Split format specification into magnitude and unit format."""
141+
142+
mspec, uspec, warns = _split_format(spec, default, separate_format_defaults)
143+
144+
for warn_msg in warns:
145+
warnings.warn(warn_msg, DeprecationWarning)
146+
131147
return mspec, uspec

0 commit comments

Comments
 (0)