|
1 | 1 | """
|
2 |
| - pint.delegates.formatter._spec_helpers |
3 |
| - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 2 | +pint.delegates.formatter._spec_helpers |
| 3 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 | 4 |
|
5 |
| - Convenient functions to deal with format specifications. |
| 5 | +Convenient functions to deal with format specifications. |
6 | 6 |
|
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. |
9 | 9 | """
|
10 | 10 |
|
11 | 11 | from __future__ import annotations
|
@@ -87,45 +87,61 @@ def remove_custom_flags(spec: str) -> str:
|
87 | 87 | return spec
|
88 | 88 |
|
89 | 89 |
|
| 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 | + |
90 | 98 | @functools.lru_cache
|
91 |
| -def split_format( |
| 99 | +def _split_format( |
92 | 100 | spec: str, default: str, separate_format_defaults: bool = True
|
93 |
| -) -> tuple[str, str]: |
| 101 | +) -> tuple[str, str, list[str]]: |
94 | 102 | """Split format specification into magnitude and unit format."""
|
95 | 103 | mspec = remove_custom_flags(spec)
|
96 | 104 | uspec = extract_custom_flags(spec)
|
97 | 105 |
|
98 | 106 | default_mspec = remove_custom_flags(default)
|
99 | 107 | default_uspec = extract_custom_flags(default)
|
100 | 108 |
|
| 109 | + warns = [] |
101 | 110 | if separate_format_defaults in (False, None):
|
102 | 111 | # should we warn always or only if there was no explicit choice?
|
103 | 112 | # Given that we want to eventually remove the flag again, I'd say yes?
|
104 | 113 | if spec and separate_format_defaults is None:
|
105 | 114 | 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." |
114 | 120 | )
|
115 | 121 | 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." |
124 | 127 | )
|
125 | 128 | elif not spec:
|
126 | 129 | mspec, uspec = default_mspec, default_uspec
|
127 | 130 | else:
|
128 | 131 | mspec = mspec or default_mspec
|
129 | 132 | uspec = uspec or default_uspec
|
130 | 133 |
|
| 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 | + |
131 | 147 | return mspec, uspec
|
0 commit comments