Skip to content

Commit

Permalink
Merge pull request #12 from rstcheck/create-ignore-dict-creator
Browse files Browse the repository at this point in the history
add ignore dict constructor func
  • Loading branch information
Cielquan committed May 29, 2022
2 parents 5759d99 + 7b9b79a commit 6fe3ab8
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ NOTE: please use them in this order.

- Non-existing paths are filtered out before checking and are logged as warning ([#10](https://github.com/rstcheck/rstcheck-core/pull/10))
- Use `<stdin>` for source in error messages instead of `-` ([#11](https://github.com/rstcheck/rstcheck-core/pull/11))
- Add constructor function for `IgnoreDict` ([#12](https://github.com/rstcheck/rstcheck-core/pull/12))

## v1.0.0rc1 (2022-05-28)

Expand Down
18 changes: 7 additions & 11 deletions src/rstcheck_core/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ def _create_ignore_dict_from_config(rstcheck_config: config.RstcheckConfig) -> t
:param rstcheck_config: Config to extract ignore settings from
:return: :py:class:`rstcheck_core.types.IgnoreDict`
"""
return types.IgnoreDict(
return types.construct_ignore_dict(
messages=rstcheck_config.ignore_messages,
languages=rstcheck_config.ignore_languages or [],
directives=rstcheck_config.ignore_directives or [],
roles=rstcheck_config.ignore_roles or [],
substitutions=rstcheck_config.ignore_substitutions or [],
languages=rstcheck_config.ignore_languages,
directives=rstcheck_config.ignore_directives,
roles=rstcheck_config.ignore_roles,
substitutions=rstcheck_config.ignore_substitutions,
)


Expand All @@ -170,9 +170,7 @@ def check_source(
if isinstance(source_origin, pathlib.Path) and source_origin.name == "-":
source_origin = "<stdin>"
logger.info(f"Check source from '{source_origin}'")
ignores = ignores or types.IgnoreDict(
messages=None, languages=[], directives=[], roles=[], substitutions=[]
)
ignores = ignores or types.construct_ignore_dict()
ignores["directives"].extend(
inline_config.find_ignored_directives(source, source_origin, warn_unknown_settings)
)
Expand Down Expand Up @@ -350,9 +348,7 @@ def __init__( # pylint: disable=too-many-arguments
self.checkers: t.List[types.CheckerRunFunction] = []
self.source = source
self.source_origin = source_origin
self.ignores = ignores or types.IgnoreDict(
messages=None, languages=[], directives=[], roles=[], substitutions=[]
)
self.ignores = ignores or types.construct_ignore_dict()
self.report_level = report_level
self.warn_unknown_settings = warn_unknown_settings
self.code_block_checker = CodeBlockChecker(
Expand Down
29 changes: 29 additions & 0 deletions src/rstcheck_core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ class IgnoreDict(_t.TypedDict):
substitutions: t.List[str]


def construct_ignore_dict(
# NOTE: Pattern type-arg errors pydanic: https://github.com/samuelcolvin/pydantic/issues/2636
messages: t.Optional[t.Pattern] = None, # type: ignore[type-arg]
languages: t.Optional[t.List[str]] = None,
directives: t.Optional[t.List[str]] = None,
roles: t.Optional[t.List[str]] = None,
substitutions: t.Optional[t.List[str]] = None,
) -> IgnoreDict:
"""Create an :py:class:`IgnoreDict` with passed values or defaults.
:param messages: Value for :py:attr:`IgnoreDict.messages`;
:py:obj:`None` results in an empty list; defaults to :py:obj:`None`
:param directives: Value for :py:attr:`IgnoreDict.directives`;
:py:obj:`None` results in an empty list; defaults to :py:obj:`None`
:param roles: Value for :py:attr:`IgnoreDict.roles`;
:py:obj:`None` results in an empty list; defaults to :py:obj:`None`
:param substitutions: Value for :py:attr:`IgnoreDict.substitutions`;
:py:obj:`None` results in an empty list; defaults to :py:obj:`None`
:return: :py:class:`IgnoreDict` with passed values or defaults
"""
return IgnoreDict(
messages=messages,
languages=languages if languages is not None else [],
directives=directives if directives is not None else [],
roles=roles if roles is not None else [],
substitutions=substitutions if substitutions is not None else [],
)


CheckerRunFunction = t.Callable[..., YieldedLintError]
"""Function to run checks.
Expand Down
34 changes: 6 additions & 28 deletions tests/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test__create_ignore_dict_from_config() -> None:
test_config,
)

assert result == types.IgnoreDict(
assert result == types.construct_ignore_dict(
messages=ignore_messages_re,
languages=ignore_languages,
directives=ignore_directives,
Expand Down Expand Up @@ -202,12 +202,8 @@ def test_lint_error_skipped_on_set_ignore() -> None:
Test
===
"""
ignores = types.IgnoreDict(
messages=re.compile(r"Possible title underline, too short for the title"),
languages=[],
directives=[],
roles=[],
substitutions=[],
ignores = types.construct_ignore_dict(
messages=re.compile(r"Possible title underline, too short for the title")
)

result = list(checker.check_source(source, ignores=ignores))
Expand Down Expand Up @@ -281,13 +277,7 @@ def test_code_block_no_error_on_set_ignore_pre310() -> None:
print(
"""
ignores = types.IgnoreDict(
messages=re.compile(r"unexpected EOF while parsing"),
languages=[],
directives=[],
roles=[],
substitutions=[],
)
ignores = types.construct_ignore_dict(messages=re.compile(r"unexpected EOF while parsing"))

result = list(checker.check_source(source, ignores=ignores))

Expand All @@ -302,13 +292,7 @@ def test_code_block_no_error_on_set_ignore() -> None:
print(
"""
ignores = types.IgnoreDict(
messages=re.compile(r"'\(' was never closed"),
languages=[],
directives=[],
roles=[],
substitutions=[],
)
ignores = types.construct_ignore_dict(messages=re.compile(r"'\(' was never closed"))

result = list(checker.check_source(source, ignores=ignores))

Expand All @@ -322,13 +306,7 @@ def test_stdin_message() -> None:
print(
"""
ignores = types.IgnoreDict(
messages=None,
languages=[],
directives=[],
roles=[],
substitutions=[],
)
ignores = types.construct_ignore_dict()

result = list(checker.check_source(source, source_file=pathlib.Path("-"), ignores=ignores))

Expand Down
40 changes: 40 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Tests for ``types`` module."""
import re

from rstcheck_core import types


class TestIgnoreDictConstructor:
"""Test ``construct_ignore_dict`` function."""

@staticmethod
def test_no_args() -> None:
"""Test construction of IgnoreDict with default values."""
result = types.construct_ignore_dict()

assert result == types.IgnoreDict(
messages=None,
languages=[],
directives=[],
roles=[],
substitutions=[],
)

@staticmethod
def test_all_args() -> None:
"""Test construction of IgnoreDict with set values."""
result = types.construct_ignore_dict(
messages=re.compile("msg"),
languages=["lang"],
directives=["dir"],
roles=["role"],
substitutions=["sub"],
)

assert result == types.IgnoreDict(
messages=re.compile("msg"),
languages=["lang"],
directives=["dir"],
roles=["role"],
substitutions=["sub"],
)

0 comments on commit 6fe3ab8

Please sign in to comment.