Skip to content

Commit fce1d32

Browse files
authored
chore(deps-dev): update mypy requirement from >=1.19.1,<1.20 to >=2.0.0,<3.0.0 and fix and adjust configuration and fix code according to the newly uncovered type errors (#1437)
Signed-off-by: Jens Troeger <jens.troeger@light-speed.de>
1 parent b9727a4 commit fce1d32

11 files changed

Lines changed: 47 additions & 35 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ repos:
6767
language: python
6868
files: ^src/macaron/|^tests/
6969
types: [text, python]
70-
exclude: ^tests/malware_analyzer/pypi/resources/sourcecode_samples.*
70+
pass_filenames: false
7171
args: [--show-traceback, --config-file, pyproject.toml]
7272

7373
# Enable a whole bunch of useful helper hooks, too.

‎pyproject.toml‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ actions = [
7676
]
7777
dev = [
7878
"flit >=3.2.0,<4.0.0",
79-
"mypy >=1.19.1,<1.20",
79+
"mypy >=2.0.0,<3.0.0",
8080
"types-pyyaml >=6.0.4,<7.0.0",
8181
"types-requests >=2.25.6,<3.0.0",
8282
"types-jsonschema >=4.22.0,<5.0.0",
@@ -169,6 +169,18 @@ exclude = [
169169

170170
# https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml
171171
[tool.mypy]
172+
files = [
173+
"src/macaron/",
174+
"tests/",
175+
]
176+
exclude = [
177+
"tests/malware_analyzer/pypi/resources/sourcecode_samples/",
178+
]
179+
mypy_path = [
180+
"src/",
181+
]
182+
num_workers = 4
183+
explicit_package_bases = true
172184
show_error_codes = true
173185
show_column_numbers = true
174186
check_untyped_defs = true
@@ -187,13 +199,13 @@ disallow_untyped_decorators = true
187199

188200
[[tool.mypy.overrides]]
189201
module = [
190-
"pytest.*", # https://github.com/pytest-dev/pytest/issues/7469
191202
"pydriller.*",
192203
"gitdb.*",
193204
"yamale.*",
194205
"problog.*",
195206
]
196-
ignore_missing_imports = true
207+
# ignore_missing_imports = true
208+
follow_untyped_imports = true
197209

198210

199211
# https://pylint.pycqa.org/en/latest/user_guide/configuration/index.html

‎src/macaron/parsers/actionparser.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022 - 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2022 - 2026, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""This module contains the parser for GitHub Actions Workflow files."""
@@ -37,7 +37,7 @@ def parse(workflow_path: str) -> Workflow:
3737
When parsing fails with errors.
3838
"""
3939
try:
40-
parse_result = yamale.make_data(workflow_path, parser="ruamel")
40+
parse_result = yamale.make_data(workflow_path, parser="ruamel") # type: ignore[no-untyped-call]
4141
except OSError as error:
4242
raise ParseError("Cannot parse GitHub Workflow: " + workflow_path) from error
4343

‎src/macaron/parsers/yaml/loader.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022 - 2025, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2022 - 2026, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""This module contains the loader for YAML files."""
@@ -38,7 +38,7 @@ def _load_yaml_content(path: os.PathLike | str) -> list:
3838
"""
3939
try:
4040
logger.debug("Loading yaml from file %s", path)
41-
return list(yamale.make_data(path))
41+
return list(yamale.make_data(path)) # type: ignore[no-untyped-call]
4242
except YAMLError as error:
4343
abs_path = os.path.abspath(path)
4444

@@ -74,7 +74,7 @@ def validate_yaml_data(cls, schema: Schema, data: list) -> bool:
7474
"""
7575
try:
7676
logger.debug("Validate data %s with schema %s.", str(data), str(schema.dict))
77-
yamale.validate(schema, data)
77+
yamale.validate(schema, data) # type: ignore[no-untyped-call]
7878
return True
7979
except yamale.YamaleError as error:
8080
logger.debug("Yaml data validation failed.")
@@ -84,7 +84,7 @@ def validate_yaml_data(cls, schema: Schema, data: list) -> bool:
8484
return False
8585

8686
@classmethod
87-
def load(cls, path: os.PathLike | str, schema: Schema = None) -> Any:
87+
def load(cls, path: os.PathLike | str, schema: Schema | None = None) -> Any:
8888
"""Load and return a Python object from a yaml file.
8989
9090
If ``schema`` is provided. This method will validate the loaded content against the

‎src/macaron/repo_finder/repo_utils.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ def get_repo_tags(git_obj: Git) -> dict[str, str]:
209209

210210
# Retrieve tags using a Git subprocess.
211211
repository_path = git_obj.repo.working_tree_dir
212-
if not os.path.isdir(repository_path):
213-
logger.debug("")
212+
if not repository_path or not os.path.isdir(repository_path):
213+
logger.debug("Invalid repository path: %s", repository_path)
214214
return {}
215215
try:
216216
result = subprocess.run(

‎src/macaron/slsa_analyzer/checks/detect_malicious_metadata_check.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ def evaluate_heuristic_results(
190190
problog_code = f"{facts}\n\n{self.malware_rules_problog_model}"
191191
logger.debug("Problog model used for evaluation:\n %s", problog_code)
192192

193-
problog_model = PrologString(problog_code)
194-
problog_results: dict[Term, float] = get_evaluatable().create_from(problog_model).evaluate()
193+
problog_model = PrologString(problog_code) # type: ignore[no-untyped-call]
194+
problog_results: dict[Term, float] = get_evaluatable().create_from(problog_model).evaluate() # type: ignore[no-untyped-call]
195195

196-
confidence = problog_results.pop(Term(self.problog_result_access), 0.0)
196+
confidence = problog_results.pop(Term(self.problog_result_access), 0.0) # type: ignore[no-untyped-call]
197197
if confidence > 0: # a rule was triggered
198198
for term, conf in problog_results.items():
199199
if term.args:

‎src/macaron/util.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def check_rate_limit(response: Response) -> None:
471471
remains = int(response.headers["X-RateLimit-Remaining"]) if "X-RateLimit-Remaining" in response.headers else 2
472472

473473
if remains <= 1:
474-
rate_limit_reset = response.headers.get("X-RateLimit-Reset", default="")
474+
rate_limit_reset = response.headers.get("X-RateLimit-Reset", "")
475475

476476
if not rate_limit_reset:
477477
return

‎tests/parsers/yaml/test_yaml_loader.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2022 - 2025, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2022 - 2026, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""This module test the yaml loader functions."""
@@ -40,21 +40,21 @@ def test_validate_yaml_data(self) -> None:
4040
"""Test the validate yaml data method."""
4141
# We are not testing the behavior of yamale methods
4242
# so the schema and data can be empty.
43-
mock_schema = Schema({})
43+
mock_schema = Schema({}) # type: ignore[no-untyped-call]
4444
mock_data: list = []
4545

4646
# No errors
4747
with patch("yamale.validate", return_value=[]):
4848
assert YamlLoader.validate_yaml_data(mock_schema, mock_data)
4949

5050
# Errors exist
51-
with patch("yamale.validate", side_effect=YamaleError(results=[])):
51+
with patch("yamale.validate", side_effect=YamaleError(results=[])): # type: ignore[no-untyped-call]
5252
assert not YamlLoader.validate_yaml_data(mock_schema, mock_data)
5353

5454
def test_load(self) -> None:
5555
"""Test the load method of YamlLoader."""
5656
schema_file = os.path.join(self.RESOURCES_DIR, "schema.yaml")
57-
schema: Schema = yamale.make_schema(schema_file)
57+
schema: Schema = yamale.make_schema(schema_file) # type: ignore[no-untyped-call]
5858

5959
assert YamlLoader.load(os.path.join(self.RESOURCES_DIR, "invalid.yaml")) == {None: None}
6060
assert not YamlLoader.load(os.path.join(self.RESOURCES_DIR, "invalid.yaml"), schema)

‎tests/repo_finder/test_commit_finder.py‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
import os
88
import re
99
import shutil
10-
from typing import Any
1110

1211
import hypothesis
1312
import pytest
1413
from hypothesis import given, settings
1514
from hypothesis.strategies import DataObject, data, text
1615
from packageurl import PackageURL
17-
from pydriller.git import Git
16+
from pydriller.git import Git, GitCommit
1817

1918
from macaron.repo_finder import commit_finder
2019
from macaron.repo_finder.commit_finder import AbstractPurlType, determine_optional_suffix_index
@@ -117,24 +116,24 @@ def mocked_repo_() -> Git:
117116

118117

119118
@pytest.fixture(name="mocked_repo_commit")
120-
def mocked_repo_commit_(mocked_repo: Git) -> Any:
119+
def mocked_repo_commit_(mocked_repo: Git) -> GitCommit:
121120
"""Add a commit to the mocked repository."""
122121
return mocked_repo.repo.index.commit(message="Commit_0")
123122

124123

125124
@pytest.fixture(name="mocked_repo_empty_commit")
126-
def mocked_repo_empty_commit_(mocked_repo: Git) -> Any:
125+
def mocked_repo_empty_commit_(mocked_repo: Git) -> GitCommit:
127126
"""Add an empty commit to the mocked repository."""
128127
return mocked_repo.repo.index.commit(message="Empty_Commit")
129128

130129

131130
@pytest.fixture(name="mocked_repo_expanded")
132-
def mocked_repo_expanded_(mocked_repo: Git, mocked_repo_commit: Any, mocked_repo_empty_commit: Any) -> Any:
131+
def mocked_repo_expanded_(mocked_repo: Git, mocked_repo_commit: GitCommit, mocked_repo_empty_commit: GitCommit) -> Git:
133132
"""Add tags to the mocked repository."""
134133
mocked_repo.repo.create_tag("4.5", mocked_repo_commit.hexsha)
135134

136135
# Create a tag from a tree.
137-
mocked_repo.repo.create_tag("1.0", ref=mocked_repo.repo.heads.master.commit.tree)
136+
mocked_repo.repo.create_tag("1.0", mocked_repo.repo.heads.master.commit.tree.hexsha)
138137

139138
# Add a tag with unicode version.
140139
mocked_repo.repo.create_tag(UNICODE_VERSION, mocked_repo_commit.hexsha)
@@ -200,7 +199,7 @@ def test_commit_finder_tag_failure(
200199
)
201200
def test_commit_finder_success_commit(
202201
mocked_repo_expanded: Git,
203-
mocked_repo_commit: Any,
202+
mocked_repo_commit: GitCommit,
204203
purl_string: str,
205204
) -> None:
206205
"""Test Commit Finder on mocked repository that should match valid PURLs."""
@@ -223,15 +222,15 @@ def test_commit_finder_success_commit(
223222
],
224223
)
225224
def test_commit_finder_success_empty_commit(
226-
mocked_repo_expanded: Git, mocked_repo_empty_commit: Any, purl_string: str
225+
mocked_repo_expanded: Git, mocked_repo_empty_commit: GitCommit, purl_string: str
227226
) -> None:
228227
"""Test Commit Finder on mocked repository that should match value PURLs."""
229228
match, outcome = commit_finder.find_commit(mocked_repo_expanded, PackageURL.from_string(purl_string))
230229
assert match == mocked_repo_empty_commit.hexsha
231230
assert outcome == CommitFinderInfo.MATCHED
232231

233232

234-
def test_commit_finder_repo_purl_success(mocked_repo_expanded: Git, mocked_repo_commit: Any) -> None:
233+
def test_commit_finder_repo_purl_success(mocked_repo_expanded: Git, mocked_repo_commit: GitCommit) -> None:
235234
"""Test Commit Finder on mocked repository using a repo type PURL."""
236235
match, outcome = commit_finder.find_commit(
237236
mocked_repo_expanded, PackageURL.from_string(f"pkg:github/apache/maven@{mocked_repo_commit.hexsha}")
@@ -254,7 +253,7 @@ def test_commit_finder_optional_suffixes(version: str, parts: list, expected: in
254253
assert determine_optional_suffix_index(version, parts) == expected
255254

256255

257-
def test_get_repo_tags(mocked_repo_empty_commit: Any) -> None:
256+
def test_get_repo_tags(mocked_repo_empty_commit: GitCommit) -> None:
258257
"""Test the get repo tags utils function."""
259258
# Create the repository object.
260259
repo = Git(os.path.join(REPO_DIR))

‎tests/slsa_analyzer/git_service/test_gitlab.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
# Copyright (c) 2023 - 2024, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2023 - 2026, Oracle and/or its affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
33

44
"""Tests for the GitLab git service."""
55

6+
import collections.abc
67
import os
78
from pathlib import Path
89
from unittest import mock
@@ -117,7 +118,7 @@ def test_self_hosted_gitlab_without_env_set(tmp_path: Path) -> None:
117118

118119

119120
@pytest.fixture(name="self_hosted_gitlab")
120-
def self_hosted_gitlab_repo_fixture(request: pytest.FixtureRequest) -> Git:
121+
def self_hosted_gitlab_repo_fixture(request: pytest.FixtureRequest) -> collections.abc.Iterable[Git]:
121122
"""Create a mock GitLab self_hosted repo.
122123
123124
This fixture expects ONE parameter of type STR which will be the origin remote url initialized for the self_hosted
@@ -153,7 +154,7 @@ def self_hosted_gitlab_repo_fixture(request: pytest.FixtureRequest) -> Git:
153154

154155
yield gitlab_repo
155156

156-
gitlab_repo.clear()
157+
gitlab_repo.clear() # type: ignore[no-untyped-call]
157158

158159

159160
# The indirect parameter is used to note that ``self_hosted_gitlab`` should be passed to the ``self_hosted_gitlab``

0 commit comments

Comments
 (0)