Skip to content

Commit

Permalink
chore(lint): fix pyright issues
Browse files Browse the repository at this point in the history
  • Loading branch information
looztra committed Feb 17, 2025
1 parent 3b034cd commit 5e86625
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 67 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/code_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ jobs:
echo "::group::make test"
make test
echo "::endgroup::"
echo "::group::make test-python-versions"
make test-python-versions
echo "::endgroup::"
- name: Run Integration tests
run: |
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ include $(LOCAL_MK_ROOT)/python-base-venv.mk
include $(LOCAL_MK_ROOT)/python-uv-venv.mk
include $(LOCAL_MK_ROOT)/python-base-app.mk
include $(LOCAL_MK_ROOT)/python-uv-app.mk
include $(LOCAL_MK_ROOT)/project.mk
3 changes: 2 additions & 1 deletion experiments/test_comments_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import sys

import ruamel.yaml
from ruamel.yaml.comments import CommentedMap

comment_column = None
insert = ruamel.yaml.comments.CommentedMap()
insert = CommentedMap()
insert["test"] = "asdf"
insert.yaml_add_eol_comment("Test Comment!", "test", column=comment_column)
insert["second-key"] = "yop"
Expand Down
1 change: 1 addition & 0 deletions poe_tasks.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"lint:all" = [
"ruff:lint",
"pylint",
"pyright",
]
pylint = [
"pylint:run",
Expand Down
18 changes: 9 additions & 9 deletions src/yamkix/comments.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Deal with comments."""

from ruamel.yaml.comments import CommentedBase, CommentedMap, NotNone
from ruamel.yaml.comments import CommentedBase, CommentedMap, CommentedSeq, NotNone
from ruamel.yaml.error import CommentMark
from ruamel.yaml.tokens import CommentToken

Expand Down Expand Up @@ -32,7 +32,7 @@ def yamkix_add_eol_comment(self, comment: str, key=NotNone, column=None) -> None
self._yaml_add_eol_comment(comment_as_list, key=key)


CommentedBase.yaml_add_eol_comment = yamkix_add_eol_comment # pyright: ignore [ reportAttributeAccessIssue]
CommentedBase.yaml_add_eol_comment = yamkix_add_eol_comment # pyright: ignore[reportAttributeAccessIssue]


def process_single_comment(data: CommentedBase, comment: str, key: str, column: int | None) -> None:
Expand All @@ -47,7 +47,7 @@ def fix_for_issue29(data: CommentedMap, key: str) -> None:
data.ca.items[key][3] = None


def process_comments_for_dict(data: CommentedMap, column: int | None = None) -> None:
def process_comments_for_map(data: CommentedMap, column: int | None = None) -> None:
"""Reposition comments when data is a dict."""
if data.ca and data.ca.items:
for key in data.ca.items:
Expand All @@ -61,7 +61,7 @@ def process_comments_for_dict(data: CommentedMap, column: int | None = None) ->
process_comments(val, column=column)


def process_comments_for_list(data: CommentedMap, column: int | None = None) -> None:
def process_comments_for_seq(data: CommentedSeq, column: int | None = None) -> None:
"""Reposition when data is a list."""
if data.ca and data.ca.items:
for key in data.ca.items:
Expand All @@ -73,9 +73,9 @@ def process_comments_for_list(data: CommentedMap, column: int | None = None) ->
process_comments(elem, column=column)


def process_comments(data: CommentedMap, column: int | None = None) -> None:
def process_comments(data: CommentedBase, column: int | None = None) -> None:
"""Reposition comments."""
if isinstance(data, dict):
process_comments_for_dict(data, column=column)
elif isinstance(data, list):
process_comments_for_list(data, column=column)
if isinstance(data, CommentedMap):
process_comments_for_map(data, column=column)
elif isinstance(data, CommentedSeq):
process_comments_for_seq(data, column=column)
6 changes: 2 additions & 4 deletions src/yamkix/yamkix.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,13 @@ def yamkix_dump_all(
one_or_more_items: list, yaml: YAML, dash_inwards: bool, output_file: str | None, spaces_before_comment: int | None
) -> None:
"""Dump all the documents from the input structure."""
if output_file is None:
out = sys.stdout

# Clear the output file if it is a file and it exists
if output_file is not None and (output_file_path := Path(output_file)).is_file: # Walrus baby
with output_file_path.open(mode="w", encoding="UTF-8") as _:
pass
for doc in one_or_more_items:
if output_file is None:
out = sys.stdout
yamkix_dump_one(doc, yaml, dash_inwards, out, spaces_before_comment)
else:
with Path(output_file).open(mode="a", encoding="UTF-8") as out:
Expand All @@ -66,7 +64,7 @@ def yamkix_dump_one(
) -> None:
"""Dump a single document."""
if spaces_before_comment is not None:
process_comments(single_item, column=spaces_before_comment)
process_comments(single_item, column=spaces_before_comment) # pyright: ignore[reportArgumentType]
if dash_inwards and type(single_item).__name__ == "CommentedSeq":
yaml.dump(single_item, out, transform=strip_leading_double_space)
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestArgs(unittest.TestCase):

def test_defaults(self) -> None:
"""Test when no input is provided."""
sut: YamkixConfig = parse_cli({})
sut: YamkixConfig = parse_cli([])
yamkix_default_config = get_default_yamkix_config()

assert sut.parsing_mode == yamkix_default_config.parsing_mode
Expand Down
92 changes: 44 additions & 48 deletions tests/test_comments.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@
"""Test the comments management."""

import unittest
from types import SimpleNamespace

from pytest_mock import MockerFixture
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap

from yamkix.comments import fix_for_issue29


class TestComments(unittest.TestCase):
"""Provide unit tests for the comments package."""

def test_fix_for_issue29_when_not_none(self) -> None:
"""Test fix_for_issue29.
When the data.ca.items[key][3] is not None
and hence needs to be changed
"""
sut_key = "sut"
items = {"key1": 0, sut_key: [0, 1, 2, 3]}
sut_ca = SimpleNamespace(items=items)
sut_data = SimpleNamespace(dont="care", ca=sut_ca)

assert sut_data.ca.items[sut_key][3] is not None
fix_for_issue29(sut_data, sut_key)
assert sut_data.ca.items[sut_key][3] is None

def test_fix_for_issue29_when_none(self) -> None:
"""Test fix_for_issue29.
When the data.ca.items[key][3] is None
and hence doesn't need to be changed
"""
sut_key = "sut"
items = {"key1": 0, sut_key: [0, 1, 2, None]}
sut_ca = SimpleNamespace(items=items)
sut_data = SimpleNamespace(dont="care", ca=sut_ca)

assert sut_data.ca.items[sut_key][3] is None
fix_for_issue29(sut_data, sut_key)
assert sut_data.ca.items[sut_key][3] is None

def test_yamkix_add_eol_comment(self) -> None:
"""Test yamkix_add_eol_comment."""
inp = """\
# example
name:
# details
family: Smith # very common
given: Alice # one of the siblings
"""
yaml = YAML()
code = yaml.load(inp)
assert code["family"] == "Smith"
def test_fix_for_issue29_when_not_none(mocker: MockerFixture) -> None:
"""Test fix_for_issue29.
When the data.ca.items[key][3] is not None
and hence needs to be changed
"""
sut_key = "sut"
items = {"key1": 0, sut_key: [0, 1, 2, 3]}
sut_ca = mocker.Mock(spec=CommentedMap, items=items)
sut_data = mocker.Mock(spec=CommentedMap, dont="care", ca=sut_ca)
assert sut_data.ca.items[sut_key][3] is not None
fix_for_issue29(sut_data, sut_key)
assert sut_data.ca.items[sut_key][3] is None


def test_fix_for_issue29_when_none(mocker: MockerFixture) -> None:
"""Test fix_for_issue29.
When the data.ca.items[key][3] is None
and hence doesn't need to be changed
"""
sut_key = "sut"
items = {"key1": 0, sut_key: [0, 1, 2, None]}
sut_ca = mocker.Mock(spec=CommentedMap, items=items)
sut_data = mocker.Mock(spec=CommentedMap, dont="care", ca=sut_ca)
assert sut_data.ca.items[sut_key][3] is None
fix_for_issue29(sut_data, sut_key)
assert sut_data.ca.items[sut_key][3] is None


def test_yamkix_add_eol_comment() -> None:
"""Test yamkix_add_eol_comment."""
inp = """\
# example
name:
# details
family: Smith # very common
given: Alice # one of the siblings
"""
yaml = YAML()
code = yaml.load(inp)
assert code["family"] == "Smith"
8 changes: 4 additions & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,22 @@ def test_get_spaces_before_comment_from_args_when_int(self, faker: Faker) -> Non
sut = get_spaces_before_comment_from_args(parsed)
assert sut == space_before_comments

def test_get_spaces_before_comment_from_args_when_invalid(self) -> None:
def test_get_spaces_before_comment_from_args_when_invalid(self, faker: Faker) -> None:
"""Test get_spaces_before_comment_from_args.
spaces_before_comment=yolo
"""
parsed = Namespace(spaces_before_comment="yolo")
parsed = Namespace(spaces_before_comment=faker.word())
sut = get_spaces_before_comment_from_args(parsed)
assert sut is None

def test_get_yamkix_config_from_default_parsing_mode(self) -> None:
def test_get_yamkix_config_from_default_parsing_mode(self, faker: Faker) -> None:
"""Test get_yamkix_config_from_default.
change parsing_mode
"""
reference = get_yamkix_config_from_default()
new_val = "zoro"
new_val = faker.word()
sut = get_yamkix_config_from_default(parsing_mode=new_val)
assert sut.parsing_mode == new_val
assert sut.explicit_start == reference.explicit_start
Expand Down
4 changes: 4 additions & 0 deletions toolbox/mk/project.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: test-python-versions
test-python-versions: ## ▶ Run tests for all supported python versions
@echo "Running tests for all supported python versions"
@uv run tox run

0 comments on commit 5e86625

Please sign in to comment.