Skip to content

Commit

Permalink
Replace a few type: ignore by casts
Browse files Browse the repository at this point in the history
  • Loading branch information
dmerejkowsky committed Jun 7, 2018
1 parent 35fe630 commit 5d1620d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
10 changes: 5 additions & 5 deletions tsrc/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import getpass
import uuid
from typing import List, Optional
from typing import cast, List, Optional

import github3
from github3.repos.repo import Repository
Expand Down Expand Up @@ -31,7 +31,7 @@ def get_previous_token() -> Optional[str]:
github_auth = auth.get("github")
if not github_auth:
return None
return github_auth.get("token") # type: ignore
return cast(Optional[str], github_auth.get("token"))


def generate_token() -> str:
Expand All @@ -46,13 +46,13 @@ def generate_token() -> str:
note = "tsrc-" + str(uuid.uuid4())
note_url = "https://supertanker.github.io/tsrc"

def ask_2fa() -> None:
return ui.ask_string("2FA code: ") # type: ignore
def ask_2fa() -> str:
return cast(str, ui.ask_string("2FA code: "))

authorization = github3.authorize(username, password, scopes,
note=note, note_url=note_url,
two_factor_callback=ask_2fa)
return authorization.token # type: ignore
return cast(str, authorization.token)


def save_token(token: str) -> None:
Expand Down
15 changes: 8 additions & 7 deletions tsrc/gitlab.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" Tiny wrapper for gitlab REST API """

import json
from typing import Any, Dict, Iterable, List, Optional, NewType
from typing import cast, Any, Dict, Iterable, List, Optional, NewType
import urllib.parse

import requests
Expand Down Expand Up @@ -130,7 +130,7 @@ def find_opened_merge_request(self, project_id: int,
"per_page": "100" # Maximum number of items allowed in pagination
})
previous_mrs = list() # type: List[MergeRequest]
previous_mrs = self.make_paginated_get_request(url, params=params) # type: ignore
previous_mrs = cast(List[MergeRequest], self.make_paginated_get_request(url, params=params))
for mr in previous_mrs:
if mr["source_branch"] == source_branch:
return mr
Expand All @@ -148,14 +148,14 @@ def create_merge_request(self, project_id: int, source_branch: str, *, title: st
})
result = self.make_request("POST", url, data=data)
ui.info("done", ui.check)
return result # type: ignore
return cast(MergeRequest, result)

def update_merge_request(self, merge_request: MergeRequest,
**kwargs: str) -> MergeRequest:
project_id = merge_request["target_project_id"]
merge_request_iid = merge_request["iid"]
url = "/projects/%s/merge_requests/%s" % (project_id, merge_request_iid)
return self.make_request("PUT", url, data=RequestData(kwargs)) # type: ignore
return cast(MergeRequest, self.make_request("PUT", url, data=RequestData(kwargs)))

def accept_merge_request(self, merge_request: MergeRequest) -> None:
project_id = merge_request["project_id"]
Expand All @@ -175,13 +175,14 @@ def get_active_users(self) -> List[str]:
if total > 100:
raise TooManyUsers()
else:
return response.json() # type: ignore
return cast(List[str], response.json())

def get_group_members(self, group: str, query: Optional[str] = None) -> List[Any]:
params = RequestParams({"query": query})
return self.make_request("GET", "/groups/%s/members" % group, params=params) # type: ignore
response = self.make_request("GET", "/groups/%s/members" % group, params=params)
return cast(List[Any], response)

def get_project_members(self, project_id: int, query: Optional[str] = None) -> List[Any]:
params = RequestParams({"query": query})
res = self.make_request("GET", "/projects/%s/members" % project_id, params=params)
return res # type: ignore
return cast(List[Any], res)
5 changes: 3 additions & 2 deletions tsrc/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import operator
import os
from typing import Any, Dict, List, NewType, Optional, Tuple
from typing import cast, Any, Dict, List, NewType, Optional, Tuple

from path import Path
import schema
Expand Down Expand Up @@ -128,6 +128,7 @@ def load(manifest_path: Path) -> Manifest:
})
parsed = tsrc.config.parse_config_file(manifest_path, manifest_schema)
parsed = ManifestConfig(parsed) # type: ignore
as_manifest_config = cast(ManifestConfig, parsed)
res = Manifest()
res.load(parsed) # type: ignore
res.load(as_manifest_config)
return res
5 changes: 3 additions & 2 deletions tsrc/test/cli/test_init.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import cast, Any


import tsrc.cli
Expand All @@ -9,7 +9,8 @@


def repo_exists(workspace_path: Path, repo: str) -> bool:
return workspace_path.joinpath(repo).exists() # type: ignore
res = workspace_path.joinpath(repo).exists()
return cast(bool, res)


def assert_cloned(workspace_path: Path, repo: str) -> None:
Expand Down
6 changes: 3 additions & 3 deletions tsrc/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import argparse
import stat
import textwrap
from typing import Iterable, List, Tuple, Dict, Any, Optional, NewType
from typing import cast, Iterable, List, Tuple, Dict, Any, Optional, NewType

import attr
from path import Path
Expand Down Expand Up @@ -106,7 +106,7 @@ def get_gitlab_url(self) -> str:
gitlab_config = self.manifest.gitlab
if not gitlab_config:
raise tsrc.Error("No gitlab configuration found in manifest")
return gitlab_config["url"] # type: ignore
return cast(str, gitlab_config["url"])

def get_url(self, src: str) -> str:
assert self.manifest, "manifest is empty. Did you call load()?"
Expand Down Expand Up @@ -430,7 +430,7 @@ def check_branch(self, repo: tsrc.Repo, repo_path: Path) -> None:
except tsrc.Error:
raise tsrc.Error("Not on any branch")

# is repo.branch allowed to be None ?
# FIXME: is repo.branch allowed to be None ?
if current_branch and current_branch != repo.branch:
self.bad_branches.append((repo.src, current_branch, repo.branch)) # type: ignore

Expand Down

0 comments on commit 5d1620d

Please sign in to comment.