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

add pylint configuration #371

Merged
merged 7 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ python3.pkgs.buildPythonApplication {
mypy
python3.pkgs.setuptools
python3.pkgs.black
python3.pkgs.pylint
ruff
glibcLocales

Expand All @@ -39,7 +40,7 @@ python3.pkgs.buildPythonApplication {
echo -e "\x1b[32m## run ruff\x1b[0m"
ruff .
echo -e "\x1b[32m## run mypy\x1b[0m"
mypy --strict nixpkgs_review
mypy nixpkgs_review

echo -e "\x1b[32m## run nixpkgs-review --help\x1b[0m"

Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion nixpkgs_review/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def read_github_token() -> str | None:
if token:
return token
try:
with open(hub_config_path()) as f:
with open(hub_config_path(), encoding="utf-8") as f:
for line in f:
# Allow substring match as hub uses yaml. Example string we match:
# " - oauth_token: ghp_abcdefghijklmnopqrstuvwxyzABCDEF1234\n"
Expand Down
2 changes: 1 addition & 1 deletion nixpkgs_review/cli/post_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ def post_result_command(args: argparse.Namespace) -> None:
warn(f"Report not found in {report}. Are you in a nixpkgs-review nix-shell?")
sys.exit(1)

with open(report) as f:
with open(report, encoding="utf-8") as f:
report_text = f.read()
github_client.comment_issue(pr, report_text)
2 changes: 1 addition & 1 deletion nixpkgs_review/cli/wip.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def wip_command(args: argparse.Namespace) -> Path:
allow = AllowedFeatures(args.allow)
with Buildenv(allow.aliases, args.extra_nixpkgs_config) as nixpkgs_config:
return review_local_revision(
"rev-%s-dirty" % verify_commit_hash("HEAD"),
f"rev-{verify_commit_hash('HEAD')}-dirty",
args,
allow,
nixpkgs_config,
Expand Down
2 changes: 1 addition & 1 deletion nixpkgs_review/nix.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def nix_build(
def write_shell_expression(
filename: Path, attrs: list[str], system: str, nixpkgs_config: Path
) -> None:
with open(filename, "w+") as f:
with open(filename, "w+", encoding="utf-8") as f:
Copy link
Owner

Choose a reason for hiding this comment

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

What happens if you don't specify encoding?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

https://pylint.pycqa.org/en/latest/user_guide/messages/warning/unspecified-encoding.html

It is better to specify an encoding when opening documents. Using the system default implicitly can create problems on other operating systems. See https://peps.python.org/pep-0597/

f.write(
f"""{{ pkgs ? import ./nixpkgs {{ system = \"{system}\"; config = import {nixpkgs_config}; }} }}:
with pkgs;
Expand Down
8 changes: 5 additions & 3 deletions nixpkgs_review/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def write_error_logs(attrs: list[Attr], directory: Path) -> None:
for path in [f"{attr.drv_path}^*", attr.path]:
if not path:
continue
with open(logs.ensure().joinpath(attr.name + ".log"), "w+") as f:
with open(
logs.ensure().joinpath(attr.name + ".log"), "w+", encoding="utf-8"
) as f:
nix_log = subprocess.run(
[
"nix",
Expand Down Expand Up @@ -121,10 +123,10 @@ def built_packages(self) -> list[str]:
return [a.name for a in self.built]

def write(self, directory: Path, pr: int | None) -> None:
with open(directory.joinpath("report.md"), "w+") as f:
with open(directory.joinpath("report.md"), "w+", encoding="utf-8") as f:
f.write(self.markdown(pr))

with open(directory.joinpath("report.json"), "w+") as f:
with open(directory.joinpath("report.json"), "w+", encoding="utf-8") as f:
f.write(self.json(pr))

write_error_logs(self.attrs, directory)
Expand Down
7 changes: 3 additions & 4 deletions nixpkgs_review/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def apply_unstaged(self, staged: bool = False) -> None:
result = subprocess.run(["git", "apply"], cwd=self.worktree_dir(), input=diff)

if result.returncode != 0:
warn("Failed to apply diff in %s" % self.worktree_dir())
warn(f"Failed to apply diff in {self.worktree_dir()}")
sys.exit(1)

def build_commit(
Expand Down Expand Up @@ -396,11 +396,10 @@ def list_packages(
res = subprocess.run(cmd, stdout=tmp)
if res.returncode != 0:
raise NixpkgsReviewError(
"Failed to list packages: nix-env failed with exit code %d"
% res.returncode
f"Failed to list packages: nix-env failed with exit code {res.returncode}"
)
tmp.flush()
with open(tmp.name) as f:
with open(tmp.name, encoding="utf-8") as f:
return parse_packages_xml(f)


Expand Down
35 changes: 28 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,36 @@ exclude = '''

[tool.mypy]
python_version = "3.10"
warn_redundant_casts = true
disallow_untyped_calls = true
disallow_untyped_defs = true
strict = true
no_implicit_optional = true
# Missing type parameters for generic type "CaptureFixture" [type-arg]
disallow_any_generics = false

[[tool.mypy.overrides]]
module = "setuptools.*"
module = [
"setuptools",
"pytest"
]
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "pytest.*"
ignore_missing_imports = true
[tool.pylint.FORMAT]
output-format = "colorized"

[tool.pylint.messages_control]
disable = [
# Disable failure for TODO items in the codebase (code will always have TODOs).
"fixme",

# Annoying.
"line-too-long",

# Too many missing docstrings.
"missing-docstring",

# Not very important, maybe fix in the future.
"invalid-name",
"too-many-instance-attributes",
"too-few-public-methods",
"too-many-arguments",
"too-many-locals",
]
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ def nixpkgs() -> Iterator[Nixpkgs]:
yield setup_git(nixpkgs_path)


# pytest.fixture is untyped
@pytest.fixture # type: ignore
@pytest.fixture
def helpers() -> type[Helpers]:
return Helpers
14 changes: 9 additions & 5 deletions tests/test_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import shutil
import subprocess
from unittest.mock import MagicMock, mock_open, patch
from unittest.mock import MagicMock, Mock, mock_open, patch

import pytest

Expand All @@ -13,14 +13,14 @@


@patch("nixpkgs_review.utils.shutil.which", return_value=None)
def test_default_to_nix_if_nom_not_found(mock_shutil):
def test_default_to_nix_if_nom_not_found(mock_shutil: Mock) -> None:
return_value = nix_nom_tool()
assert return_value == "nix"
mock_shutil.assert_called_once()


@pytest.mark.skipif(not shutil.which("nom"), reason="`nom` not found in PATH")
def test_pr_local_eval(helpers: Helpers, capfd) -> None:
def test_pr_local_eval(helpers: Helpers, capfd: pytest.CaptureFixture) -> None:
with helpers.nixpkgs() as nixpkgs:
with open(nixpkgs.path.joinpath("pkg1.txt"), "w") as f:
f.write("foo")
Expand All @@ -47,7 +47,9 @@ def test_pr_local_eval(helpers: Helpers, capfd) -> None:


@patch("nixpkgs_review.cli.nix_nom_tool", return_value="nix")
def test_pr_local_eval_missing_nom(mock_tool, helpers: Helpers, capfd) -> None:
def test_pr_local_eval_missing_nom(
mock_tool: Mock, helpers: Helpers, capfd: pytest.CaptureFixture
) -> None:
with helpers.nixpkgs() as nixpkgs:
with open(nixpkgs.path.joinpath("pkg1.txt"), "w") as f:
f.write("foo")
Expand All @@ -74,7 +76,9 @@ def test_pr_local_eval_missing_nom(mock_tool, helpers: Helpers, capfd) -> None:
assert "$ nix build" in captured.out


def test_pr_local_eval_without_nom(helpers: Helpers, capfd) -> None:
def test_pr_local_eval_without_nom(
helpers: Helpers, capfd: pytest.CaptureFixture
) -> None:
with helpers.nixpkgs() as nixpkgs:
with open(nixpkgs.path.joinpath("pkg1.txt"), "w") as f:
f.write("foo")
Expand Down
6 changes: 4 additions & 2 deletions tests/test_wip.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@pytest.mark.skipif(not shutil.which("nom"), reason="`nom` not found in PATH")
def test_wip_command(helpers: Helpers, capfd) -> None:
def test_wip_command(helpers: Helpers, capfd: pytest.CaptureFixture) -> None:
with helpers.nixpkgs() as nixpkgs:
with open(nixpkgs.path.joinpath("pkg1.txt"), "w") as f:
f.write("foo")
Expand All @@ -24,7 +24,9 @@ def test_wip_command(helpers: Helpers, capfd) -> None:
assert "$ nom build" in captured.out


def test_wip_command_without_nom(helpers: Helpers, capfd) -> None:
def test_wip_command_without_nom(
helpers: Helpers, capfd: pytest.CaptureFixture
) -> None:
with helpers.nixpkgs() as nixpkgs:
with open(nixpkgs.path.joinpath("pkg1.txt"), "w") as f:
f.write("foo")
Expand Down