Skip to content

Commit

Permalink
Fix part of oppia#13341: Added Mypy type annotations to the `script d…
Browse files Browse the repository at this point in the history
…irectory`'s pending files. (oppia#16226)

* scripts 2

* added files

* added file but pending test file

* added more files

* nits fixed

* ntis fixed -2

* fix for backend failuer

* nits

* added changes

* nits

* nits -2

* self review -1

* self review - 2

* added changes

* added change for test

* added changes for testing

* reverted all test changes

* confusion created

* fixed union issue

* removed union

* added changes:

* added check for linter

* added comment support

* fix lint for more files

* added fixes for mypy typing and linters

* added changes

* updated branch

* added changes

* added changes

* added changes
  • Loading branch information
sahiljoster32 authored Oct 17, 2022
1 parent 113ef4f commit 64b0a62
Show file tree
Hide file tree
Showing 38 changed files with 1,233 additions and 794 deletions.
16 changes: 8 additions & 8 deletions scripts/build_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def test_minify_func_with_invalid_filepath(self) -> None:
INVALID_INPUT_FILEPATH,
INVALID_OUTPUT_FILEPATH,
INVALID_FILENAME)
# Stubs of 'assertRaisesRegex' does not contain any returncode
# attribute, so because of this MyPy throws an '"Exception" has
# no attribute "returncode"' error. Thus to avoid the error, we
# used ignore here.
# Here we use MyPy ignore because the stubs of 'assertRaisesRegex' do
# not contain any returncode attribute, so because of this MyPy throws
# an '"Exception" has no attribute "returncode"' error. Thus to avoid
# the error, we used ignore here.
# `returncode` is the exit status of the child process.
self.assertEqual(called_process.exception.returncode, 1) # type: ignore[attr-defined]

Expand All @@ -88,10 +88,10 @@ def test_minify_and_create_sourcemap(self) -> None:
'returned non-zero exit status 1') as called_process:
build._minify_and_create_sourcemap( # pylint: disable=protected-access
INVALID_INPUT_FILEPATH, INVALID_OUTPUT_FILEPATH)
# Stubs of 'assertRaisesRegex' does not contain any returncode
# attribute, so because of this MyPy throws an '"Exception" has
# no attribute "returncode"' error. Thus to avoid the error, we
# used ignore here.
# Here we use MyPy ignore because the stubs of 'assertRaisesRegex' do
# not contain any returncode attribute, so because of this MyPy throws
# an '"Exception" has no attribute "returncode"' error. Thus to avoid
# the error, we used ignore here.
# `returncode` is the exit status of the child process.
self.assertEqual(called_process.exception.returncode, 1) # type: ignore[attr-defined]

Expand Down
11 changes: 5 additions & 6 deletions scripts/check_overall_backend_test_coverage_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from core.tests import test_utils
from scripts import check_overall_backend_test_coverage
from scripts import common
from typing import Any


class CheckOverallBackendTestCoverageTests(test_utils.GenericTestBase):
Expand All @@ -48,7 +47,7 @@ class MockProcess:
returncode = 0
stdout = 'No data to report.'
stderr = 'None'
def mock_subprocess_run(*args: Any, **kwargs: Any) -> MockProcess: # pylint: disable=unused-argument
def mock_subprocess_run(*args: str, **kwargs: str) -> MockProcess: # pylint: disable=unused-argument
return MockProcess()
swap_subprocess_run = self.swap_with_checks(
subprocess, 'run', mock_subprocess_run,
Expand All @@ -72,7 +71,7 @@ class MockProcess:
returncode = 1
stdout = 'Some error occured.'
stderr = 'Some error.'
def mock_subprocess_run(*args: Any, **kwargs: Any) -> MockProcess: # pylint: disable=unused-argument
def mock_subprocess_run(*args: str, **kwargs: str) -> MockProcess: # pylint: disable=unused-argument
return MockProcess()
swap_subprocess_run = self.swap_with_checks(
subprocess, 'run', mock_subprocess_run,
Expand All @@ -95,7 +94,7 @@ def test_error_in_parsing_coverage_report_throws_error(self) -> None:
class MockProcess:
returncode = 0
stdout = 'TOTALL 40571 10682 13759 1161 70% '
def mock_subprocess_run(*args: Any, **kwargs: Any) -> MockProcess: # pylint: disable=unused-argument
def mock_subprocess_run(*args: str, **kwargs: str) -> MockProcess: # pylint: disable=unused-argument
return MockProcess()
swap_subprocess_run = self.swap_with_checks(
subprocess, 'run', mock_subprocess_run,
Expand All @@ -116,7 +115,7 @@ def test_overall_backend_coverage_checks_failed(self) -> None:
class MockProcess:
returncode = 0
stdout = 'TOTAL 40571 10682 13759 1161 70% '
def mock_subprocess_run(*args: Any, **kwargs: Any) -> MockProcess: # pylint: disable=unused-argument
def mock_subprocess_run(*args: str, **kwargs: str) -> MockProcess: # pylint: disable=unused-argument
return MockProcess()
swap_subprocess_run = self.swap_with_checks(
subprocess, 'run', mock_subprocess_run,
Expand All @@ -141,7 +140,7 @@ def test_overall_backend_coverage_checks_passed(self) -> None:
class MockProcess:
returncode = 0
stdout = 'TOTAL 40571 0 13759 0 100% '
def mock_subprocess_run(*args: Any, **kwargs: Any) -> MockProcess: # pylint: disable=unused-argument
def mock_subprocess_run(*args: str, **kwargs: str) -> MockProcess: # pylint: disable=unused-argument
return MockProcess()
swap_subprocess_run = self.swap_with_checks(
subprocess, 'run', mock_subprocess_run,
Expand Down
6 changes: 4 additions & 2 deletions scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ def open_new_tab_in_browser_if_possible(url: str) -> None:
def get_remote_alias(remote_urls: List[str]) -> str:
"""Finds the correct alias for the given remote repository URLs."""
git_remote_output = subprocess.check_output(
['git', 'remote', '-v']).decode('utf-8').split('\n')
['git', 'remote', '-v'], encoding='utf-8'
).split('\n')
remote_alias = None
remote_url = None
for remote_url in remote_urls:
Expand Down Expand Up @@ -349,7 +350,8 @@ def get_current_branch_name() -> str:
str. The name of current branch.
"""
git_status_output = subprocess.check_output(
['git', 'status']).decode('utf-8').strip().split('\n')
['git', 'status'], encoding='utf-8'
).strip().split('\n')
branch_message_prefix = 'On branch '
git_status_first_line = git_status_output[0]
assert git_status_first_line.startswith(branch_message_prefix)
Expand Down
95 changes: 62 additions & 33 deletions scripts/common_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@
from . import common


# The pool_size argument is required by Requester.__init__(), but it is missing
# from the typing definition in Requester.pyi. We therefore disable type
# checking here. A PR was opened to PyGithub to fix this, but it was closed due
# to inactivity (the project does not seem very active). Here is the PR:
# Here we use MyPy ignore because the pool_size argument is required by
# Requester.__init__(), but it is missing from the typing definition in
# Requester.pyi. We therefore disable type checking here. A PR was opened
# to PyGithub to fix this, but it was closed due to inactivity (the project
# does not seem very active). Here is the PR:
# https://github.com/PyGithub/PyGithub/pull/2151.
_MOCK_REQUESTER = github.Requester.Requester( # type: ignore
_MOCK_REQUESTER = github.Requester.Requester( # type: ignore[call-arg]
login_or_token=None,
password=None,
jwt=None,
Expand Down Expand Up @@ -288,16 +289,20 @@ def mock_input() -> str:
common.USER_PREFERENCES['open_new_tab_in_browser'] = None

def test_get_remote_alias_with_correct_alias(self) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'remote1 url1\nremote2 url2'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'remote1 url1\nremote2 url2'
with self.swap(
subprocess, 'check_output', mock_check_output
):
self.assertEqual(common.get_remote_alias(['url1']), 'remote1')

def test_get_remote_alias_with_incorrect_alias(self) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'remote1 url1\nremote2 url2'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'remote1 url1\nremote2 url2'
check_output_swap = self.swap(
subprocess, 'check_output', mock_check_output)
with check_output_swap, self.assertRaisesRegex(
Expand Down Expand Up @@ -325,15 +330,19 @@ def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
common.verify_local_repo_is_clean()

def test_get_current_branch_name(self) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch test'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch test'
with self.swap(
subprocess, 'check_output', mock_check_output):
self.assertEqual(common.get_current_branch_name(), 'test')

def test_update_branch_with_upstream(self) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch test'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch test'

def mock_run_cmd(cmd: str) -> str:
return cmd
Expand Down Expand Up @@ -372,79 +381,99 @@ def test_get_current_release_version_number_with_invalid_branch(
def test_is_current_branch_a_hotfix_branch_with_non_hotfix_branch(
self
) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch release-1.2.3'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch release-1.2.3'
with self.swap(
subprocess, 'check_output', mock_check_output):
self.assertEqual(common.is_current_branch_a_hotfix_branch(), False)

def test_is_current_branch_a_hotfix_branch_with_hotfix_branch(self) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch release-1.2.3-hotfix-1'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch release-1.2.3-hotfix-1'
with self.swap(
subprocess, 'check_output', mock_check_output):
self.assertEqual(common.is_current_branch_a_hotfix_branch(), True)

def test_is_current_branch_a_release_branch_with_release_branch(
self
) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch release-1.2.3'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch release-1.2.3'
with self.swap(
subprocess, 'check_output', mock_check_output):
self.assertEqual(common.is_current_branch_a_release_branch(), True)

def test_is_current_branch_a_release_branch_with_hotfix_branch(
self
) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch release-1.2.3-hotfix-1'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch release-1.2.3-hotfix-1'
with self.swap(
subprocess, 'check_output', mock_check_output):
self.assertEqual(common.is_current_branch_a_release_branch(), True)

def test_is_current_branch_a_release_branch_with_maintenance_branch(
self
) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch release-maintenance-1.2.3'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch release-maintenance-1.2.3'
with self.swap(
subprocess, 'check_output', mock_check_output):
self.assertEqual(common.is_current_branch_a_release_branch(), True)

def test_is_current_branch_a_release_branch_with_non_release_branch(
self
) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch test'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch test'
with self.swap(
subprocess, 'check_output', mock_check_output):
self.assertEqual(common.is_current_branch_a_release_branch(), False)

def test_is_current_branch_a_test_branch_with_test_branch(self) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch test-common'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch test-common'
with self.swap(
subprocess, 'check_output', mock_check_output):
self.assertEqual(common.is_current_branch_a_test_branch(), True)

def test_is_current_branch_a_test_branch_with_non_test_branch(self) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch invalid-test'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch invalid-test'
with self.swap(
subprocess, 'check_output', mock_check_output):
self.assertEqual(common.is_current_branch_a_test_branch(), False)

def test_verify_current_branch_name_with_correct_branch(self) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch test'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch test'
with self.swap(
subprocess, 'check_output', mock_check_output):
common.verify_current_branch_name('test')

def test_verify_current_branch_name_with_incorrect_branch(self) -> None:
def mock_check_output(unused_cmd_tokens: List[str]) -> bytes:
return b'On branch invalid'
def mock_check_output(
unused_cmd_tokens: List[str], encoding: str = 'utf-8' # pylint: disable=unused-argument
) -> str:
return 'On branch invalid'
check_output_swap = self.swap(
subprocess, 'check_output', mock_check_output)
with check_output_swap, self.assertRaisesRegex(
Expand Down
4 changes: 2 additions & 2 deletions scripts/create_expression_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import subprocess

from core.tests import test_utils
from typing import Any

from . import common
from . import create_expression_parser
Expand All @@ -33,7 +32,8 @@ class CreateExpressionParserTests(test_utils.GenericTestBase):
def test_expression_parser_is_produced_correctly(self) -> None:
cmd_token_list = []
def mock_check_call(
cmd_tokens: list[str], **unsued_kwargs: Any) -> None: # pylint: disable=unused-argument
cmd_tokens: list[str], **unused_kwargs: str
) -> None: # pylint: disable=unused-argument
cmd_token_list.append(cmd_tokens)

setup_script_args = []
Expand Down
Loading

0 comments on commit 64b0a62

Please sign in to comment.