Skip to content

Commit

Permalink
A whole lotta linting
Browse files Browse the repository at this point in the history
  • Loading branch information
AndydeCleyre committed Sep 18, 2024
1 parent 5b15be6 commit 3dfdfc1
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 49 deletions.
2 changes: 0 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def test(session: Session):
"""Run all tests."""
session.install('-U', 'pip')
session.install('-U', '.[test,toml]', 'coverage')
session.run('pip', 'list')
session.run('coverage', 'run', '-p', '-m', 'ward', *session.posargs)


Expand All @@ -32,7 +31,6 @@ def test_without_toml(session: Session):
"""Run tests without optional TOML support installed."""
session.install('-U', 'pip')
session.install('-U', '.[test-without-toml]', 'coverage')
session.run('pip', 'list')
session.run('coverage', 'run', '-p', '-m', 'ward', *session.posargs)


Expand Down
4 changes: 2 additions & 2 deletions nt2/casters.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ def _str_to_num(informal_num: str) -> int | float:
num = float(informal_num)
except ValueError as e:
for prefix, base in {'0x': 16, '0o': 8, '0b': 2}.items():
if re.match(f"[+-]?{prefix}", informal_num, re.I):
if re.match(f"[+-]?{prefix}", informal_num, re.IGNORECASE):
try:
num = int(informal_num, base)
except Exception: # pragma: no cover
raise ValueError(': '.join(e.args)) from None
else:
return num
raise e # pragma: no cover
raise # pragma: no cover
try:
inum = int(num)
except (ValueError, OverflowError):
Expand Down
34 changes: 19 additions & 15 deletions nt2/dumpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import sys
from json import dump as _jdump, dumps as _jdumps, loads as _jloads
from json.decoder import JSONDecodeError
from pathlib import Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pathlib import Path

from plumbum import LocalPath
from textwrap import indent
from typing import Sequence, TextIO, cast
from typing import BinaryIO, Sequence, TextIO, cast

from nestedtext import dump as _ntdump, dumps as _ntdumps, load as _ntload
from plumbum import LocalPath
from rich.console import Console as RichConsole
from rich.syntax import Syntax as RichSyntax
from ruamel.yaml.scalarstring import walk_tree as use_multiline_syntax
Expand Down Expand Up @@ -110,8 +114,8 @@ def ydump(data: dict | list):
out_stream = io.StringIO()
try:
YAML_EDITOR.dump(data, out_stream)
except Exception as e:
raise e
except Exception:
raise
else:
_syntax_print(out_stream.getvalue(), 'yaml')
finally:
Expand Down Expand Up @@ -140,9 +144,9 @@ def tdump(data: dict):
"""
_require_toml_support()
if sys.stdout.isatty():
_syntax_print(_tdumps(data, multiline_strings=True), 'toml') # type: ignore
_syntax_print(_tdumps(data, multiline_strings=True), 'toml') # pyright: ignore [reportPossiblyUnboundVariable]
else:
print(_tdumps(data, multiline_strings=True), end='') # type: ignore
print(_tdumps(data, multiline_strings=True), end='') # pyright: ignore [reportPossiblyUnboundVariable]


def jloads(content: str) -> dict | list:
Expand Down Expand Up @@ -238,7 +242,7 @@ def dump_yaml_to_schema(*input_files: LocalPath):
_dump_typed_data_to_schema(typed_data)
else:
for f in input_files:
with open(f, encoding='utf-8') as ifile:
with f.open(encoding='utf-8') as ifile:
typed_data = yload(ifile)
_dump_typed_data_to_schema(typed_data)

Expand All @@ -252,12 +256,12 @@ def dump_toml_to_schema(*input_files: LocalPath):
"""
_require_toml_support()
if not input_files:
typed_data = tloads(sys.stdin.read()) # type: ignore
typed_data = tloads(sys.stdin.read()) # pyright: ignore [reportPossiblyUnboundVariable]
_dump_typed_data_to_schema(typed_data)
else:
for f in input_files:
with open(f, 'rb') as ifile:
typed_data = tload(ifile) # type: ignore
with f.open('rb') as ifile:
typed_data = tload(cast(BinaryIO, ifile)) # pyright: ignore [reportPossiblyUnboundVariable]
_dump_typed_data_to_schema(typed_data)


Expand All @@ -275,7 +279,7 @@ def dump_yaml_to_nestedtext(*input_files: LocalPath):
ntdump(data)
else:
for f in input_files:
with open(f, encoding='utf-8') as ifile:
with f.open(encoding='utf-8') as ifile:
data = yload(ifile)
data = converter.unstructure(data)
ntdump(data)
Expand All @@ -291,13 +295,13 @@ def dump_toml_to_nestedtext(*input_files: LocalPath):
_require_toml_support()
converter = mk_stringy_converter()
if not input_files:
data = tloads(sys.stdin.read()) # type: ignore
data = tloads(sys.stdin.read()) # pyright: ignore [reportPossiblyUnboundVariable]
data = converter.unstructure(data)
ntdump(data)
else:
for f in input_files:
with open(f, 'rb') as ifile:
data = tload(ifile) # type: ignore
with f.open('rb') as ifile:
data = tload(cast(BinaryIO, ifile)) # pyright: ignore [reportPossiblyUnboundVariable]
data = converter.unstructure(data)
ntdump(data)

Expand Down
23 changes: 14 additions & 9 deletions nt2/ui.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@

import sys
from json import JSONDecodeError
from typing import cast
from typing import ClassVar, cast

from nestedtext import NestedTextError, load as ntload
from plumbum.cli import Application, ExistingFile, Flag, SwitchAttr
from plumbum.colors import blue, green, magenta, yellow # type: ignore
from plumbum.colors import (
blue, # pyright: ignore [reportAttributeAccessIssue]
green, # pyright: ignore [reportAttributeAccessIssue]
magenta, # pyright: ignore [reportAttributeAccessIssue]
yellow, # pyright: ignore [reportAttributeAccessIssue]
)
from rich import inspect as _rich_inspect
from rich.console import Console as RichConsole
from ruamel.yaml.parser import ParserError as YAMLParserError
Expand Down Expand Up @@ -66,7 +71,7 @@ class _ColorApp(Application):
PROGNAME = green
VERSION = __version__ | blue
COLOR_USAGE = green
COLOR_GROUPS = {'Meta-switches': magenta, 'Switches': yellow, 'Subcommands': blue}
COLOR_GROUPS: ClassVar = {'Meta-switches': magenta, 'Switches': yellow, 'Subcommands': blue}
ALLOW_ABBREV = True


Expand Down Expand Up @@ -137,7 +142,7 @@ class NestedTextToJSON(_NestedTextToTypedFormat, _NestedTextToTypedFormatSupport
nt2json --int People.age --boolean 'People."is a wizard"' example.nt
"""

def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201,ANN101
def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201
try:
for schema_file in cast(list, self.schema_files):
schema = cast(dict, ntload(schema_file))
Expand Down Expand Up @@ -176,7 +181,7 @@ class NestedTextToYAML(
nt2yaml --int People.age --boolean 'People."is a wizard"' example.nt
"""

def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201,ANN101
def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201
try:
for schema_file in cast(list, self.schema_files):
schema = cast(dict, ntload(schema_file))
Expand Down Expand Up @@ -213,7 +218,7 @@ class NestedTextToTOML(_NestedTextToTypedFormat, _NestedTextToTypedFormatSupport
nt2toml --int People.age --boolean 'People."is a wizard"' example.nt
"""

def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201,ANN101
def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201
try:
for schema_file in cast(list, self.schema_files):
schema = cast(dict, ntload(schema_file))
Expand Down Expand Up @@ -242,7 +247,7 @@ class JSONToNestedText(_TypedFormatToSchema):
cat example.json | json2nt
"""

def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201,ANN101
def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201
try:
if not self.to_schema:
dump_json_to_nestedtext(*input_files)
Expand All @@ -263,7 +268,7 @@ class YAMLToNestedText(_TypedFormatToSchema):
cat example.yml | yaml2nt
"""

def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201,ANN101
def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201
try:
if not self.to_schema:
dump_yaml_to_nestedtext(*input_files)
Expand All @@ -284,7 +289,7 @@ class TOMLToNestedText(_TypedFormatToSchema):
cat example.yml | toml2nt
"""

def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201,ANN101
def main(self, *input_files: ExistingFile): # type: ignore # noqa: D102,ANN201
try:
if not self.to_schema:
dump_toml_to_nestedtext(*input_files)
Expand Down
9 changes: 5 additions & 4 deletions nt2/yamlpath_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
from collections import defaultdict
from datetime import date, datetime, time
from types import SimpleNamespace
from typing import Iterable
from typing import TYPE_CHECKING, Iterable

try:
from types import NoneType
except ImportError:
NoneType = type(None)

from ruamel.yaml.main import YAML
if TYPE_CHECKING:
from ruamel.yaml.main import YAML
from yamlpath.wrappers.nodecoords import NodeCoords
from yamlpath import Processor, YAMLPath
from yamlpath.common import Parsers
from yamlpath.exceptions import YAMLPathException
from yamlpath.wrappers import ConsolePrinter
from yamlpath.wrappers.nodecoords import NodeCoords


def mk_yaml_editor() -> YAML:
Expand Down Expand Up @@ -73,7 +74,7 @@ def non_null_matches(surgeon: Processor, *query_paths: str) -> Iterable[NodeCoor
yield from matches


def _schema_entry_type(obj: int | float | bool | None | datetime | date | time) -> str:
def _schema_entry_type(obj: float | bool | None | datetime | date | time) -> str:
# -> Literal['number', 'boolean', 'null', 'date']
if isinstance(obj, bool):
return 'boolean'
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ quote-style = "preserve"
skip-magic-trailing-comma = true

[tool.ruff.lint]
select = ["F", "E", "W", "C90", "I", "N", "D", "UP", "YTT", "ANN", "S", "FBT", "B", "SIM"]
select = ["ALL"]
ignore = ["BLE", "COM812", "EM", "FIX", "ISC001", "PERF203", "PGH003", "Q000", "T20", "TD002", "TD003", "TRY003", "TRY302"]

[tool.ruff.lint.flake8-annotations]
suppress-none-returning = true
Expand All @@ -65,6 +66,9 @@ split-on-trailing-comma = false
[tool.ruff.lint.pydocstyle]
convention = "pep257"

[tool.ruff.lint.pylint]
max-args = 6

[tool.taskipy.tasks]
fmt = "nox -s fmt"
lock = "nox -s lock"
Expand Down
25 changes: 14 additions & 11 deletions test/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
Each version herein returns the stdout as a `str` for convenience in tests.
"""

from __future__ import annotations

import io
import sys
from typing import Sequence, Union, cast
from typing import TYPE_CHECKING, Sequence, cast

try:
from typing import TypeAlias
except ImportError:
from typing import Any as TypeAlias

from plumbum import LocalPath
if TYPE_CHECKING:
from plumbum import LocalPath
from plumbum.cli import Application as _Application

from nt2.ui import (
Expand All @@ -43,7 +46,7 @@
def _run_app(
app_class: Application,
*cli_args: LocalPath,
**cli_kwargs: Union[str, LocalPath, Sequence[str], bool],
**cli_kwargs: str | LocalPath | Sequence[str] | bool,
) -> str:
"""
Invoke `app_class` with given flags, and return stdout content.
Expand All @@ -64,8 +67,8 @@ def _run_app(
try:
sys.stdout = fake_stdout
app, main_result = app_class.invoke(*cli_args, **cli_kwargs)
except Exception as e: # pragma: no cover
raise e
except Exception: # pragma: no cover
raise
else:
output = fake_stdout.getvalue()
finally:
Expand All @@ -74,7 +77,7 @@ def _run_app(
return output


def json2nt(*cli_args: LocalPath, **cli_kwargs: Union[str, bool]) -> str:
def json2nt(*cli_args: LocalPath, **cli_kwargs: str | bool) -> str:
"""
Invoke `JSONToNestedText` in a test-friendly way.
Expand All @@ -88,7 +91,7 @@ def json2nt(*cli_args: LocalPath, **cli_kwargs: Union[str, bool]) -> str:
return _run_app(JSONToNestedText, *cli_args, **cli_kwargs)


def nt2json(*cli_args: LocalPath, **cli_kwargs: Union[str, LocalPath, Sequence[str]]) -> str:
def nt2json(*cli_args: LocalPath, **cli_kwargs: str | LocalPath | Sequence[str]) -> str:
"""
Invoke `NestedTextToJSON` in a test-friendly way.
Expand All @@ -102,7 +105,7 @@ def nt2json(*cli_args: LocalPath, **cli_kwargs: Union[str, LocalPath, Sequence[s
return _run_app(NestedTextToJSON, *cli_args, **cli_kwargs)


def nt2yaml(*cli_args: LocalPath, **cli_kwargs: Union[str, LocalPath, Sequence[str]]) -> str:
def nt2yaml(*cli_args: LocalPath, **cli_kwargs: str | LocalPath | Sequence[str]) -> str:
"""
Invoke `NestedTextToYAML` in a test-friendly way.
Expand All @@ -116,7 +119,7 @@ def nt2yaml(*cli_args: LocalPath, **cli_kwargs: Union[str, LocalPath, Sequence[s
return _run_app(NestedTextToYAML, *cli_args, **cli_kwargs)


def yaml2nt(*cli_args: LocalPath, **cli_kwargs: Union[str, bool]) -> str:
def yaml2nt(*cli_args: LocalPath, **cli_kwargs: str | bool) -> str:
"""
Invoke `YAMLToNestedText` in a test-friendly way.
Expand All @@ -130,7 +133,7 @@ def yaml2nt(*cli_args: LocalPath, **cli_kwargs: Union[str, bool]) -> str:
return _run_app(YAMLToNestedText, *cli_args, **cli_kwargs)


def nt2toml(*cli_args: LocalPath, **cli_kwargs: Union[str, LocalPath, Sequence[str]]) -> str:
def nt2toml(*cli_args: LocalPath, **cli_kwargs: str | LocalPath | Sequence[str]) -> str:
"""
Invoke `NestedTextToTOML` in a test-friendly way.
Expand All @@ -144,7 +147,7 @@ def nt2toml(*cli_args: LocalPath, **cli_kwargs: Union[str, LocalPath, Sequence[s
return _run_app(NestedTextToTOML, *cli_args, **cli_kwargs)


def toml2nt(*cli_args: LocalPath, **cli_kwargs: Union[str, bool]) -> str:
def toml2nt(*cli_args: LocalPath, **cli_kwargs: str | bool) -> str:
"""
Invoke `TOMLToNestedText` in a test-friendly way.
Expand Down
6 changes: 4 additions & 2 deletions test/test_toml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Test TOML <-> NestedText."""

from typing import Dict, Sequence, cast
from __future__ import annotations

from typing import Sequence, cast

from plumbum import LocalPath, local
from ward import skip, test
Expand Down Expand Up @@ -114,7 +116,7 @@ def _(
def _(
input_file: LocalPath = input_file,
expected_file: LocalPath = expected_file,
casting_args: Dict[str, Sequence[str]] = casting_args,
casting_args: dict[str, Sequence[str]] = casting_args,
):
output = nt2toml(input_file, **casting_args)
assert_file_content(expected_file, output)
Expand Down
9 changes: 6 additions & 3 deletions test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

from __future__ import annotations

from pathlib import Path
from typing import Sequence, TextIO, cast
from typing import TYPE_CHECKING, Sequence, TextIO, cast

if TYPE_CHECKING:
from pathlib import Path

from plumbum import LocalPath

from nestedtext import load as ntload
from plumbum import LocalPath
from ward.expect import assert_equal


Expand Down

0 comments on commit 3dfdfc1

Please sign in to comment.