Skip to content

Commit

Permalink
fix failing tests by different means
Browse files Browse the repository at this point in the history
  • Loading branch information
Cielquan committed Sep 3, 2023
1 parent d65dd44 commit 310a9c7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 42 deletions.
3 changes: 1 addition & 2 deletions src/rstcheck_core/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
def check_file(
source_file: pathlib.Path,
rstcheck_config: config.RstcheckConfig,
*,
overwrite_with_file_config: bool = True,
overwrite_with_file_config: bool = True, # noqa: FBT001,FBT002
) -> list[types.LintError]:
"""Check the given file for issues.
Expand Down
48 changes: 24 additions & 24 deletions src/rstcheck_core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _split_str_validator(value: t.Any) -> list[str] | None: # noqa: ANN401
return [v.strip() for v in value if v.strip()]

msg = "Not a string or list of strings"
raise ValueError(msg)
raise TypeError(msg)


class RstcheckConfigFile(pydantic.BaseModel):
Expand All @@ -86,12 +86,12 @@ class RstcheckConfigFile(pydantic.BaseModel):
"""

report_level: t.Optional[ReportLevel] = None # noqa: UP007
ignore_directives: list[str] | None = None
ignore_roles: list[str] | None = None
ignore_substitutions: list[str] | None = None
ignore_languages: list[str] | None = None
ignore_directives: t.Optional[t.List[str]] = None # noqa: UP007,UP006
ignore_roles: t.Optional[t.List[str]] = None # noqa: UP007,UP006
ignore_substitutions: t.Optional[t.List[str]] = None # noqa: UP007,UP006
ignore_languages: t.Optional[t.List[str]] = None # noqa: UP007,UP006
# NOTE: Pattern type-arg errors pydanic: https://github.com/samuelcolvin/pydantic/issues/2636
ignore_messages: t.Pattern | None = None # type: ignore[type-arg]
ignore_messages: t.Optional[t.Pattern] = None # type: ignore[type-arg] # noqa: UP007

@pydantic.field_validator("report_level", mode="before")
@classmethod
Expand Down Expand Up @@ -128,7 +128,7 @@ def valid_report_level(cls, value: t.Any) -> ReportLevel | None: # noqa: ANN401
return ReportLevel(value)

msg = "Invalid report level"
raise ValueError(msg)
raise TypeError(msg)

@pydantic.field_validator(
"ignore_directives",
Expand Down Expand Up @@ -179,7 +179,7 @@ def join_regex_str(cls, value: t.Any) -> str | t.Pattern[str] | None: # noqa: A
return value

msg = "Not a string or list of strings"
raise ValueError(msg)
raise TypeError(msg)


class RstcheckConfig(RstcheckConfigFile):
Expand All @@ -189,9 +189,9 @@ class RstcheckConfig(RstcheckConfigFile):
:raises pydantic.error_wrappers.ValidationError: If setting is not parsable into correct type
"""

config_path: pathlib.Path | None = None
recursive: bool | None = None
warn_unknown_settings: bool | None = None
config_path: t.Optional[pathlib.Path] = None # noqa: UP007
recursive: t.Optional[bool] = None # noqa: UP007
warn_unknown_settings: t.Optional[bool] = None # noqa: UP007


class _RstcheckConfigINIFile(pydantic.BaseModel):
Expand All @@ -202,12 +202,12 @@ class _RstcheckConfigINIFile(pydantic.BaseModel):
:raises pydantic.error_wrappers.ValidationError: If setting is not parsable into correct type
"""

report_level: str | None = None
ignore_directives: str | None = None
ignore_roles: str | None = None
ignore_substitutions: str | None = None
ignore_languages: str | None = None
ignore_messages: str | None = None
report_level: t.Union[str, int, None] = None # noqa: UP007
ignore_directives: t.Optional[str] = None # noqa: UP007
ignore_roles: t.Optional[str] = None # noqa: UP007
ignore_substitutions: t.Optional[str] = None # noqa: UP007
ignore_languages: t.Optional[str] = None # noqa: UP007
ignore_messages: t.Optional[str] = None # noqa: UP007


def _load_config_from_ini_file(
Expand Down Expand Up @@ -282,12 +282,12 @@ class _RstcheckConfigTOMLFile(
:raises pydantic.error_wrappers.ValidationError: If setting is not parsable into correct type
"""

report_level: str | None = None
ignore_directives: list[str] | None = None
ignore_roles: list[str] | None = None
ignore_substitutions: list[str] | None = None
ignore_languages: list[str] | None = None
ignore_messages: str | list[str] | None = None
report_level: t.Union[str, int, None] = None # noqa: UP007
ignore_directives: t.Optional[t.List[str]] = None # noqa: UP006, UP007
ignore_roles: t.Optional[t.List[str]] = None # noqa: UP006, UP007
ignore_substitutions: t.Optional[t.List[str]] = None # noqa: UP006, UP007
ignore_languages: t.Optional[t.List[str]] = None # noqa: UP006, UP007
ignore_messages: t.Union[t.List[str], str, None] = None # noqa: UP006, UP007


def _load_config_from_toml_file(
Expand Down Expand Up @@ -336,7 +336,7 @@ def _load_config_from_toml_file(
with pathlib.Path(resolved_file).open("rb") as toml_file_handle:
toml_dict = tomllib.load(toml_file_handle)

optional_rstcheck_section = dict[str, t.Any] | None
optional_rstcheck_section = t.Optional[dict[str, t.Any]] # noqa: UP007
rstcheck_section: optional_rstcheck_section = toml_dict.get("tool", {}).get("rstcheck")

if rstcheck_section is None:
Expand Down
12 changes: 6 additions & 6 deletions src/rstcheck_core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def load_config_file(
file;
defaults to :py:obj:`False`
"""
logger.info(
"Load config file for main runner: '%s'.", config_path
)
logger.info("Load config file for main runner: '%s'.", config_path)
file_config = config.load_config_file_from_path(
config_path, warn_unknown_settings=warn_unknown_settings
)
Expand All @@ -90,8 +88,7 @@ def load_config_file(
return

logger.debug(
"Merging config from file into main config. "
"File config is dominant: %s",
"Merging config from file into main config. File config is dominant: %s",
self.overwrite_config,
)
self.config = config.merge_configs(
Expand Down Expand Up @@ -186,7 +183,10 @@ def _run_checks_sync(self) -> list[list[types.LintError]]:
"""
logger.debug("Runnning checks synchronically.")
with _sphinx.load_sphinx_if_available():
return [checker.check_file(file, self.config) for file in self._files_to_check]
return [
checker.check_file(file, self.config, self.overwrite_config)
for file in self._files_to_check
]

def _run_checks_parallel(self) -> list[list[types.LintError]]:
"""Check all files from the file list in parallel and return the errors.
Expand Down
2 changes: 1 addition & 1 deletion src/rstcheck_core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from . import _compat as _t

SourceFileOrString = pathlib.Path | t.Literal["<string>", "<stdin>"]
SourceFileOrString = t.Union[pathlib.Path, t.Literal["<string>", "<stdin>"]] # noqa: UP007
"""Path to source file or if it is a string then '<string>' or '<stdin>'."""


Expand Down
2 changes: 1 addition & 1 deletion tests/checker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ def test__parse_gcc_style_error_message_raises_on_bad_format() -> None:
"""Test ``_parse_gcc_style_error_message`` method raises ``ValueError`` on bad format."""
message = "Foo bar"

with pytest.raises(ValueError, match="Message cannot be parsed."):
with pytest.raises(ValueError, match="^Message cannot be parsed.$"):
checker._parse_gcc_style_error_message(message, "<string>")

@staticmethod
Expand Down
12 changes: 6 additions & 6 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_string_lists_are_whitespace_cleaned(
)
def test_invalid_settings(value: str) -> None:
"""Test invalid settings."""
with pytest.raises(ValueError, match="Not a string or list of strings"):
with pytest.raises(TypeError, match="^Not a string or list of strings$"):
config._split_str_validator(value)


Expand Down Expand Up @@ -174,7 +174,7 @@ def test_valid_report_levels(level: t.Any) -> None: # noqa: ANN401
)
def test_invalid_report_levels(level: t.Any) -> None: # noqa: ANN401
"""Test invalid report levels not accepted by docutils."""
with pytest.raises(ValueError, match="Invalid report level"):
with pytest.raises(TypeError, match="^Invalid report level$"):
config.RstcheckConfigFile(report_level=level)


Expand Down Expand Up @@ -283,7 +283,7 @@ def test_string_lists_are_whitespace_cleaned(
)
def test_invalid_settings(value: str) -> None:
"""Test invalid settings."""
with pytest.raises(ValueError, match="Not a string or list of strings"):
with pytest.raises(TypeError, match="^Not a string or list of strings$"):
config.RstcheckConfigFile(
ignore_languages=value,
ignore_directives=value,
Expand Down Expand Up @@ -378,7 +378,7 @@ def test_list_with_empty_contents(string_list: list[str], full_string: str) -> N
)
def test_invalid_settings(value: str) -> None:
"""Test invalid ignore_messages settings."""
with pytest.raises(ValueError, match="Not a string or list of strings"):
with pytest.raises(TypeError, match="^Not a string or list of strings$"):
config.RstcheckConfigFile(ignore_messages=value)


Expand Down Expand Up @@ -651,7 +651,7 @@ def test_wrong_file_suffix_errors(tmp_path: pathlib.Path) -> None:
conf_file = tmp_path / "config.ini"
conf_file.touch()

with pytest.raises(ValueError, match="File is not a TOML file"):
with pytest.raises(ValueError, match="^File is not a TOML file$"):
config._load_config_from_toml_file(conf_file)

@staticmethod
Expand Down Expand Up @@ -1232,7 +1232,7 @@ def test_raises_on_nonexisting_path() -> None:
"""Test raises FileNotFoundError on path that is not a file or directory."""
conf_file = pathlib.Path("does-not-exist-cfg")

with pytest.raises(FileNotFoundError, match=re.compile("Passed config path not found.")):
with pytest.raises(FileNotFoundError, match="Passed config path not found"):
config.load_config_file_from_path(conf_file)

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/runner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_load_config_file_if_set(mocker: pytest_mock.MockerFixture) -> None:

runner.RstcheckMainRunner([], init_config) # act

mocked_loader.assert_called_once_with(config_file_path, False) # noqa: FBT003
mocked_loader.assert_called_once_with(config_file_path, warn_unknown_settings=False)

@staticmethod
def test_no_load_config_file_if_unset(mocker: pytest_mock.MockerFixture) -> None:
Expand Down Expand Up @@ -324,7 +324,7 @@ def test__run_checks_sync_method(
Test results are returned.
"""
monkeypatch.setattr(checker, "check_file", lambda _0, _1, _2: lint_errors)
monkeypatch.setattr(checker, "check_file", lambda _0, _1, _3: lint_errors)
test_file1 = tmp_path / "rst.rst"
test_file1.touch()
test_file2 = tmp_path / "rst2.rst"
Expand Down

0 comments on commit 310a9c7

Please sign in to comment.