Skip to content

Commit a150cc0

Browse files
committed
feat: enable underscore integer separation for decimal values (ints)
1 parent d5c173e commit a150cc0

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

docs/index.md

+27-3
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ none_value5: null
315315
Default: `quote_basic_values: bool = False`<br>
316316
Environment variable override:
317317
```bash
318-
export YAMLFIX_quote_basic_values="false"
318+
export YAMLFIX_QUOTE_BASIC_VALUES="false"
319319
```
320320

321321
Per default `ruyaml` will quote only values where it is necessary to explicitly define the type of a value. This is the case for numbers and boolean values for example. If your `yaml`-file contains a value of type string that would look like a number, then this value needs to be quoted.
@@ -341,7 +341,7 @@ stringList: ["abc", "123"]
341341
Default: `quote_keys_and_basic_values: bool = False`<br>
342342
Environment variable override:
343343
```bash
344-
export YAMLFIX_quote_keys_and_basic_values="false"
344+
export YAMLFIX_QUOTE_KEYS_AND_BASIC_VALUES="false"
345345
```
346346

347347
Similar to the [quote basic values](#quote-basic-values) configuration option, this option, in addition to the values themselves, quotes the keys as well. For example:
@@ -363,7 +363,7 @@ list: [item, item]
363363
Default: `quote_representation: str = "'"`<br>
364364
Environment variable override:
365365
```bash
366-
export YAMLFIX_quote_representation="'"
366+
export YAMLFIX_QUOTE_REPRESENTATION="'"
367367
```
368368

369369
Configure which quotation string is used for quoting values. For example:
@@ -378,6 +378,30 @@ key: 'value'
378378
key: "value"
379379
```
380380

381+
### Underscore Integer
382+
383+
Default: `underscore_integer: bool = False`<br>
384+
Environment variable override:
385+
```bash
386+
export YAMLFIX_UNDERSCORE_INTEGER="true"
387+
```
388+
389+
Configure whether to add underscores to big integer values or not. For example:
390+
391+
```yaml
392+
---
393+
small_integer: 10
394+
big_integer: 1000
395+
```
396+
397+
would be converted to
398+
399+
```yaml
400+
---
401+
small_integer: 10
402+
big_integer: 1_000
403+
```
404+
381405
# References
382406

383407
As most open sourced programs, `yamlfix` is standing on the shoulders of giants,

src/yamlfix/adapters.py

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ruyaml.main import YAML
99
from ruyaml.nodes import MappingNode, Node, ScalarNode, SequenceNode
1010
from ruyaml.representer import RoundTripRepresenter
11+
from ruyaml.scalarint import ScalarInt
1112
from ruyaml.tokens import CommentToken
1213

1314
from yamlfix.model import YamlfixConfig
@@ -123,6 +124,20 @@ def represent_str(self, data: Any) -> ScalarNode: # noqa: ANN401
123124
"tag:yaml.org,2002:str", data, self.config.quote_representation
124125
)
125126

127+
def represent_int(self, data: Any) -> Any:
128+
"""Configure the SafeRepresenter integer representation to use underscore\
129+
separation for big integers."""
130+
if not self.config.underscore_integer:
131+
return super().represent_int(data)
132+
133+
return self.represent_scalar("tag:yaml.org,2002:int", f"{data:_d}")
134+
135+
def represent_scalar_int(self, data: Any) -> Any:
136+
"""Configure the RoundTripRepresenter integer representation to use underscore\
137+
separation for big integers."""
138+
data._underscore = self.config.underscore_integer
139+
return super().represent_scalar_int(data)
140+
126141
def represent_mapping(
127142
self, tag: Any, mapping: Any, flow_style: Optional[Any] = None # noqa: ANN401
128143
) -> MappingNode:
@@ -314,6 +329,8 @@ def _apply_simple_value_quotations(self, value_node: Node) -> None:
314329

315330
YamlfixRepresenter.add_representer(type(None), YamlfixRepresenter.represent_none)
316331
YamlfixRepresenter.add_representer(str, YamlfixRepresenter.represent_str)
332+
YamlfixRepresenter.add_representer(int, YamlfixRepresenter.represent_int)
333+
YamlfixRepresenter.add_representer(ScalarInt, YamlfixRepresenter.represent_scalar_int)
317334

318335

319336
class SourceCodeFixer:

src/yamlfix/model.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ class YamlfixConfig(ConfigSchema):
2222
quote_basic_values: bool = False
2323
quote_keys_and_basic_values: bool = False
2424
quote_representation: str = "'"
25+
underscore_integer: bool = False

tests/unit/test_adapter_yaml.py

+22
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,25 @@ def test_empty_list_inline_comment_indentation(self) -> None:
582582
result = fix_code(source, config)
583583

584584
assert result == source
585+
586+
def test_underscore_for_decimal_integers(self) -> None:
587+
"""Make underscore for decimal integers configurable."""
588+
source = dedent(
589+
"""\
590+
small_ints: [1, 10, 100]
591+
big_ints: [1000, 10000, 1000000, 100000000000000000000]
592+
"""
593+
)
594+
fixed_source = dedent(
595+
"""\
596+
---
597+
small_ints: [1, 10, 100]
598+
big_ints: [1_000, 10_000, 1_000_000, 100_000_000_000_000_000_000]
599+
""" # noqa: E501
600+
)
601+
config = YamlfixConfig()
602+
config.underscore_integer = True
603+
604+
result = fix_code(source, config)
605+
606+
assert result == fixed_source

0 commit comments

Comments
 (0)