Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable underscore integer separation for decimal values (ints) #204

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 27 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ none_value5: null
Default: `quote_basic_values: bool = False`<br>
Environment variable override:
```bash
export YAMLFIX_quote_basic_values="false"
export YAMLFIX_QUOTE_BASIC_VALUES="false"
```

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.
Expand All @@ -341,7 +341,7 @@ stringList: ["abc", "123"]
Default: `quote_keys_and_basic_values: bool = False`<br>
Environment variable override:
```bash
export YAMLFIX_quote_keys_and_basic_values="false"
export YAMLFIX_QUOTE_KEYS_AND_BASIC_VALUES="false"
```

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:
Expand All @@ -363,7 +363,7 @@ list: [item, item]
Default: `quote_representation: str = "'"`<br>
Environment variable override:
```bash
export YAMLFIX_quote_representation="'"
export YAMLFIX_QUOTE_REPRESENTATION="'"
```

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

### Underscore Integer

Default: `underscore_integer: bool = False`<br>
Environment variable override:
```bash
export YAMLFIX_UNDERSCORE_INTEGER="true"
```

Configure whether to add underscores to big integer values or not. For example:

```yaml
---
small_integer: 10
big_integer: 1000
```

would be converted to

```yaml
---
small_integer: 10
big_integer: 1_000
```

# References

As most open sourced programs, `yamlfix` is standing on the shoulders of giants,
Expand Down
18 changes: 18 additions & 0 deletions src/yamlfix/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ruyaml.main import YAML
from ruyaml.nodes import MappingNode, Node, ScalarNode, SequenceNode
from ruyaml.representer import RoundTripRepresenter
from ruyaml.scalarint import ScalarInt
from ruyaml.tokens import CommentToken

from yamlfix.model import YamlfixConfig
Expand Down Expand Up @@ -123,6 +124,21 @@ def represent_str(self, data: Any) -> ScalarNode: # noqa: ANN401
"tag:yaml.org,2002:str", data, self.config.quote_representation
)

def represent_int(self, data: Any) -> Any: # noqa: ANN401
"""Configure the SafeRepresenter integer representation to use underscore\
separation for big integers."""
if not self.config.underscore_integer:
return super().represent_int(data)

return self.represent_scalar("tag:yaml.org,2002:int", f"{data:_d}")

def represent_scalar_int(self, data: Any) -> Any: # noqa: ANN401
"""Configure the RoundTripRepresenter integer representation to use underscore\
separation for big integers."""
# provide the internal `_underscore` flag, that is used in insert_underscore
data._underscore = self.config.underscore_integer # noqa: W0212
return super().represent_scalar_int(data)

def represent_mapping(
self, tag: Any, mapping: Any, flow_style: Optional[Any] = None # noqa: ANN401
) -> MappingNode:
Expand Down Expand Up @@ -314,6 +330,8 @@ def _apply_simple_value_quotations(self, value_node: Node) -> None:

YamlfixRepresenter.add_representer(type(None), YamlfixRepresenter.represent_none)
YamlfixRepresenter.add_representer(str, YamlfixRepresenter.represent_str)
YamlfixRepresenter.add_representer(int, YamlfixRepresenter.represent_int)
YamlfixRepresenter.add_representer(ScalarInt, YamlfixRepresenter.represent_scalar_int)


class SourceCodeFixer:
Expand Down
1 change: 1 addition & 0 deletions src/yamlfix/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ class YamlfixConfig(ConfigSchema):
quote_basic_values: bool = False
quote_keys_and_basic_values: bool = False
quote_representation: str = "'"
underscore_integer: bool = False
22 changes: 22 additions & 0 deletions tests/unit/test_adapter_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,25 @@ def test_empty_list_inline_comment_indentation(self) -> None:
result = fix_code(source, config)

assert result == source

def test_underscore_for_decimal_integers(self) -> None:
"""Make underscore for decimal integers configurable."""
source = dedent(
"""\
small_ints: [1, 10, 100]
big_ints: [1000, 10000, 1000000, 100000000000000000000]
"""
)
fixed_source = dedent(
"""\
---
small_ints: [1, 10, 100]
big_ints: [1_000, 10_000, 1_000_000, 100_000_000_000_000_000_000]
""" # noqa: E501
)
config = YamlfixConfig()
config.underscore_integer = True

result = fix_code(source, config)

assert result == fixed_source
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make sure I'd add a test where the source has underscores, the config is set to undersscore_integer = False and make sure that the reverse procedure works well too

        fixed_source = dedent(
            """\
            small_ints: [1, 10, 100]
            big_ints: [1000, 10000, 1000000, 100000000000000000000]
            """
        )
        source = dedent(
            """\
            ---
            small_ints: [1, 10, 100]
            big_ints: [1_000, 10_000, 1_000_000, 100_000_000_000_000_000_000]
            """  # noqa: E501
        )

        config = YamlfixConfig()
        result = fix_code(source, config)
        assert result == fixed_source