Skip to content

Commit

Permalink
Make type: ignore more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleFromNVIDIA committed Aug 26, 2024
1 parent 8917b35 commit 62f24b6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/rapids_pre_commit_hooks/alpha_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def compose_document(self) -> yaml.Node:
self.get_event()

# Compose the root node.
node = self.compose_node(None, None) # type: ignore
node = self.compose_node(None, None) # type: ignore[arg-type]

# Drop the DOCUMENT-END event.
self.get_event()
Expand Down
6 changes: 3 additions & 3 deletions src/rapids_pre_commit_hooks/pyproject_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def find_value_location(
# look for that in the new document.
node = copied_document
while len(key) > (0 if append else 1):
node = node[key[0]] # type: ignore
node = node[key[0]] # type: ignore[assignment]
key = key[1:]
if append:
node.add(str(placeholder), placeholder_toml)
Expand All @@ -65,8 +65,8 @@ def check_pyproject_license(linter: Linter, args: argparse.Namespace) -> None:
try:
add_project_table = True
project_table = document["project"]
add_project_table = project_table.is_super_table() # type: ignore
license_value = project_table["license"]["text"] # type: ignore
add_project_table = project_table.is_super_table() # type: ignore[union-attr]
license_value = project_table["license"]["text"] # type: ignore[index]
except tomlkit.exceptions.NonExistentKey:
if add_project_table:
loc = (len(linter.content), len(linter.content))
Expand Down
56 changes: 31 additions & 25 deletions test/rapids_pre_commit_hooks/test_copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def mock_target_branch(branch: Any):
remote_1_branch_1 = remote_repo_1.create_head(
"branch-1-renamed", remote_1_master.commit
)
remote_repo_1.head.reference = remote_1_branch_1 # type: ignore
remote_repo_1.head.reference = remote_1_branch_1 # type: ignore[misc]
remote_repo_1.head.reset(index=True, working_tree=True)
write_file(remote_repo_1, "file1.txt", "File 1 modified")
remote_repo_1.index.add(["file1.txt"])
Expand All @@ -330,7 +330,7 @@ def mock_target_branch(branch: Any):
remote_1_branch_2 = remote_repo_1.create_head(
"branch-2", remote_1_master.commit
)
remote_repo_1.head.reference = remote_1_branch_2 # type: ignore
remote_repo_1.head.reference = remote_1_branch_2 # type: ignore[misc]
remote_repo_1.head.reset(index=True, working_tree=True)
write_file(remote_repo_1, "file2.txt", "File 2 modified")
remote_repo_1.index.add(["file2.txt"])
Expand All @@ -339,7 +339,7 @@ def mock_target_branch(branch: Any):
remote_1_branch_3 = remote_repo_1.create_head(
"branch-3", remote_1_master.commit
)
remote_repo_1.head.reference = remote_1_branch_3 # type: ignore
remote_repo_1.head.reference = remote_1_branch_3 # type: ignore[misc]
remote_repo_1.head.reset(index=True, working_tree=True)
write_file(remote_repo_1, "file3.txt", "File 3 modified")
remote_repo_1.index.add(["file3.txt"])
Expand All @@ -351,7 +351,7 @@ def mock_target_branch(branch: Any):
remote_1_branch_4 = remote_repo_1.create_head(
"branch-4", remote_1_master.commit
)
remote_repo_1.head.reference = remote_1_branch_4 # type: ignore
remote_repo_1.head.reference = remote_1_branch_4 # type: ignore[misc]
remote_repo_1.head.reset(index=True, working_tree=True)
write_file(remote_repo_1, "file4.txt", "File 4 modified")
remote_repo_1.index.add(["file4.txt"])
Expand All @@ -363,7 +363,7 @@ def mock_target_branch(branch: Any):
remote_1_branch_7 = remote_repo_1.create_head(
"branch-7", remote_1_master.commit
)
remote_repo_1.head.reference = remote_1_branch_7 # type: ignore
remote_repo_1.head.reference = remote_1_branch_7 # type: ignore[misc]
remote_repo_1.head.reset(index=True, working_tree=True)
write_file(remote_repo_1, "file7.txt", "File 7 modified")
remote_repo_1.index.add(["file7.txt"])
Expand All @@ -379,7 +379,7 @@ def mock_target_branch(branch: Any):
remote_2_branch_3 = remote_repo_2.create_head(
"branch-3", remote_2_master.commit
)
remote_repo_2.head.reference = remote_2_branch_3 # type: ignore
remote_repo_2.head.reference = remote_2_branch_3 # type: ignore[misc]
remote_repo_2.head.reset(index=True, working_tree=True)
write_file(remote_repo_2, "file3.txt", "File 3 modified")
remote_repo_2.index.add(["file3.txt"])
Expand All @@ -391,7 +391,7 @@ def mock_target_branch(branch: Any):
remote_2_branch_4 = remote_repo_2.create_head(
"branch-4", remote_2_master.commit
)
remote_repo_2.head.reference = remote_2_branch_4 # type: ignore
remote_repo_2.head.reference = remote_2_branch_4 # type: ignore[misc]
remote_repo_2.head.reset(index=True, working_tree=True)
write_file(remote_repo_2, "file4.txt", "File 4 modified")
remote_repo_2.index.add(["file4.txt"])
Expand All @@ -403,7 +403,7 @@ def mock_target_branch(branch: Any):
remote_2_branch_5 = remote_repo_2.create_head(
"branch-5", remote_2_master.commit
)
remote_repo_2.head.reference = remote_2_branch_5 # type: ignore
remote_repo_2.head.reference = remote_2_branch_5 # type: ignore[misc]
remote_repo_2.head.reset(index=True, working_tree=True)
write_file(remote_repo_2, "file5.txt", "File 5 modified")
remote_repo_2.index.add(["file5.txt"])
Expand Down Expand Up @@ -433,7 +433,7 @@ def mock_target_branch(branch: Any):
with branch_1.config_writer() as w:
w.set_value("remote", "unconventional/remote/name/1")
w.set_value("merge", "branch-1-renamed")
git_repo.head.reference = branch_1 # type: ignore
git_repo.head.reference = branch_1 # type: ignore[misc]
git_repo.head.reset(index=True, working_tree=True)
git_repo.index.remove("file1.txt", working_tree=True)
git_repo.index.commit(
Expand All @@ -442,7 +442,7 @@ def mock_target_branch(branch: Any):
)

branch_6 = git_repo.create_head("branch-6", remote_1.refs["master"])
git_repo.head.reference = branch_6 # type: ignore
git_repo.head.reference = branch_6 # type: ignore[misc]
git_repo.head.reset(index=True, working_tree=True)
git_repo.index.remove(["file6.txt"], working_tree=True)
git_repo.index.commit("Remove file6.txt")
Expand All @@ -451,15 +451,15 @@ def mock_target_branch(branch: Any):
with branch_7.config_writer() as w:
w.set_value("remote", "unconventional/remote/name/1")
w.set_value("merge", "branch-7")
git_repo.head.reference = branch_7 # type: ignore
git_repo.head.reference = branch_7 # type: ignore[misc]
git_repo.head.reset(index=True, working_tree=True)
git_repo.index.remove(["file7.txt"], working_tree=True)
git_repo.index.commit(
"Remove file7.txt",
commit_date=datetime.datetime(2024, 2, 1, tzinfo=datetime.timezone.utc),
)

git_repo.head.reference = main # type: ignore
git_repo.head.reference = main # type: ignore[misc]
git_repo.head.reset(index=True, working_tree=True)

with mock_target_branch("branch-1"):
Expand Down Expand Up @@ -614,7 +614,7 @@ def file_contents(verbed: str) -> str:
git_repo.index.commit("Remove modified.txt")

pr_branch = git_repo.create_head("pr", "HEAD~")
git_repo.head.reference = pr_branch # type: ignore
git_repo.head.reference = pr_branch # type: ignore[misc]
git_repo.head.reset(index=True, working_tree=True)

write_file("copied_2.txt", file_contents("copied"))
Expand Down Expand Up @@ -662,7 +662,7 @@ def file_contents(verbed: str) -> str:
target_branch = git_repo.heads["master"]
merge_base = git_repo.merge_base(target_branch, "HEAD")[0]
old_files = {
blob.path: blob # type: ignore
blob.path: blob # type: ignore[union-attr]
for blob in merge_base.tree.traverse(lambda b, _: isinstance(b, git.Blob))
}

Expand Down Expand Up @@ -698,17 +698,23 @@ def file_contents(verbed: str) -> str:
if old:
with open(fn(new), "rb") as f:
new_contents = f.read()
old_contents = old_files[old].data_stream.read() # type: ignore
old_contents = old_files[old].data_stream.read() # type: ignore[union-attr]
assert new_contents != old_contents
assert changed_files[new].data_stream.read() == old_contents # type: ignore
assert (
changed_files[new].data_stream.read() # type: ignore[union-attr]
== old_contents
)

for new, old in superfluous.items():
if old:
with open(fn(new), "rb") as f:
new_contents = f.read()
old_contents = old_files[old].data_stream.read() # type: ignore
old_contents = old_files[old].data_stream.read() # type: ignore[union-attr]
assert new_contents == old_contents
assert changed_files[new].data_stream.read() == old_contents # type: ignore
assert (
changed_files[new].data_stream.read() # type: ignore[union-attr]
== old_contents
)


def test_get_changed_files_multiple_merge_bases(git_repo: git.Repo) -> None:
Expand All @@ -727,7 +733,7 @@ def write_file(filename: str, contents: str):
git_repo.index.commit("Initial commit")

branch_1 = git_repo.create_head("branch-1", "master")
git_repo.head.reference = branch_1 # type: ignore
git_repo.head.reference = branch_1 # type: ignore[misc]
git_repo.index.reset(index=True, working_tree=True)
write_file("file1.txt", "File 1 modified\n")
git_repo.index.add("file1.txt")
Expand All @@ -737,7 +743,7 @@ def write_file(filename: str, contents: str):
)

branch_2 = git_repo.create_head("branch-2", "master")
git_repo.head.reference = branch_2 # type: ignore
git_repo.head.reference = branch_2 # type: ignore[misc]
git_repo.index.reset(index=True, working_tree=True)
write_file("file2.txt", "File 2 modified\n")
git_repo.index.add("file2.txt")
Expand All @@ -747,7 +753,7 @@ def write_file(filename: str, contents: str):
)

branch_1_2 = git_repo.create_head("branch-1-2", "master")
git_repo.head.reference = branch_1_2 # type: ignore
git_repo.head.reference = branch_1_2 # type: ignore[misc]
git_repo.index.reset(index=True, working_tree=True)
write_file("file1.txt", "File 1 modified\n")
write_file("file2.txt", "File 2 modified\n")
Expand All @@ -759,7 +765,7 @@ def write_file(filename: str, contents: str):
)

branch_3 = git_repo.create_head("branch-3", "master")
git_repo.head.reference = branch_3 # type: ignore
git_repo.head.reference = branch_3 # type: ignore[misc]
git_repo.index.reset(index=True, working_tree=True)
write_file("file1.txt", "File 1 modified\n")
write_file("file2.txt", "File 2 modified\n")
Expand Down Expand Up @@ -873,21 +879,21 @@ def file_contents_modified(num: int) -> str:
git_repo.index.commit("Initial commit")

branch_1 = git_repo.create_head("branch-1", "master")
git_repo.head.reference = branch_1 # type: ignore
git_repo.head.reference = branch_1 # type: ignore[misc]
git_repo.head.reset(index=True, working_tree=True)
write_file("file1.txt", file_contents_modified(1))
git_repo.index.add(["file1.txt"])
git_repo.index.commit("Update file1.txt")

branch_2 = git_repo.create_head("branch-2", "master")
git_repo.head.reference = branch_2 # type: ignore
git_repo.head.reference = branch_2 # type: ignore[misc]
git_repo.head.reset(index=True, working_tree=True)
write_file("file2.txt", file_contents_modified(2))
git_repo.index.add(["file2.txt"])
git_repo.index.commit("Update file2.txt")

pr = git_repo.create_head("pr", "branch-1")
git_repo.head.reference = pr # type: ignore
git_repo.head.reference = pr # type: ignore[misc]
git_repo.head.reset(index=True, working_tree=True)
write_file("file3.txt", file_contents_modified(3))
git_repo.index.add(["file3.txt"])
Expand Down
2 changes: 1 addition & 1 deletion test/test_pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def list_files(top: str) -> Generator[str, None, None]:

branch_dir = os.path.join(example_dir, "branch")
if os.path.exists(branch_dir):
git_repo.head.reference = git_repo.create_head( # type: ignore
git_repo.head.reference = git_repo.create_head( # type: ignore[misc]
"branch", git_repo.head.commit
)
git_repo.index.remove(list(list_files(master_dir)), working_tree=True)
Expand Down

0 comments on commit 62f24b6

Please sign in to comment.