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

Fix part of #14033: Added Mypy type annotations to some files. #14469

Merged
merged 53 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
d74038b
Fixes part of #9749: Migrate few instances of angular-html-bind (#14263)
srijanreddy98 Nov 27, 2021
354d901
Merge remote-tracking branch 'upstream/develop' into develop
sahiljoster32 Nov 28, 2021
7930e4f
Merge remote-tracking branch 'upstream/develop' into develop
sahiljoster32 Dec 4, 2021
2ff1fcb
Merge remote-tracking branch 'upstream/develop' into develop
sahiljoster32 Dec 8, 2021
7da71af
Merge remote-tracking branch 'upstream/develop' into develop
sahiljoster32 Dec 9, 2021
6afee7e
Merge remote-tracking branch 'upstream/develop' into develop
sahiljoster32 Dec 11, 2021
f6a3c12
Merge remote-tracking branch 'upstream/develop' into develop
sahiljoster32 Dec 12, 2021
5dc96d8
pending with tests
sahiljoster32 Dec 18, 2021
bffe408
pending with caching_services
sahiljoster32 Dec 19, 2021
f65c8ba
lint 1
sahiljoster32 Dec 19, 2021
39fbb95
lint 2
sahiljoster32 Dec 19, 2021
c34365a
lint 3
sahiljoster32 Dec 19, 2021
7fec54c
pending with test
sahiljoster32 Dec 20, 2021
1885375
lint
sahiljoster32 Dec 20, 2021
9667b75
done
sahiljoster32 Dec 21, 2021
13f1b09
fixed models type
sahiljoster32 Dec 21, 2021
8a67ca0
nits
sahiljoster32 Dec 21, 2021
c4c2a94
removed conflicts
sahiljoster32 Dec 21, 2021
44a0126
added changes
sahiljoster32 Dec 21, 2021
d11cfaf
lint 1
sahiljoster32 Dec 21, 2021
62b2e5e
lint again :_(
sahiljoster32 Dec 21, 2021
9b7a5b5
added changes -1
sahiljoster32 Dec 23, 2021
fe36b85
changes -2
sahiljoster32 Dec 23, 2021
fd656c8
changes -3
sahiljoster32 Dec 28, 2021
28669b1
changes -4
sahiljoster32 Jan 4, 2022
94084f6
added changes -3
sahiljoster32 Jan 4, 2022
601d151
fixing backend tests
sahiljoster32 Jan 4, 2022
44e8825
added changes -4
sahiljoster32 Jan 4, 2022
2e91bd8
changes -5
sahiljoster32 Jan 6, 2022
c647552
added changes
sahiljoster32 Jan 8, 2022
20c8315
Merge remote-tracking branch 'upstream/develop' into mytypes
sahiljoster32 Jan 8, 2022
176807f
added literals
sahiljoster32 Jan 8, 2022
5252178
added overload decorator
sahiljoster32 Jan 9, 2022
6b6ead6
Merge remote-tracking branch 'upstream/develop' into mytypes
sahiljoster32 Jan 12, 2022
6a3f29b
added changes
sahiljoster32 Jan 13, 2022
1d19b94
added changes
sahiljoster32 Jan 14, 2022
4d29919
removed conflicts
sahiljoster32 Jan 14, 2022
a85bc4f
added changes -12
sahiljoster32 Jan 14, 2022
3d99ce3
added changes
sahiljoster32 Jan 15, 2022
53a8d9a
removed merge conflicts
sahiljoster32 Jan 15, 2022
e4017ee
lint
sahiljoster32 Jan 15, 2022
9537ed6
fixed backend test
sahiljoster32 Jan 15, 2022
b943be9
added changes
sahiljoster32 Jan 16, 2022
2f62fd9
lint
sahiljoster32 Jan 16, 2022
c71a403
added changes
sahiljoster32 Jan 16, 2022
d0a9978
test coverage
sahiljoster32 Jan 16, 2022
4ca7c69
test coverage -2
sahiljoster32 Jan 16, 2022
2840a8a
added overload
sahiljoster32 Jan 17, 2022
69cac5d
Update core/domain/caching_services.py
sahiljoster32 Jan 17, 2022
995563c
added changes
sahiljoster32 Jan 18, 2022
c0b4738
Merge branch 'develop' of https://github.com/oppia/oppia into mytypes
sahiljoster32 Jan 18, 2022
b299913
Merge remote-tracking branch 'origin/mytypes' into mytypes
sahiljoster32 Jan 18, 2022
085e97e
corrected comment
sahiljoster32 Jan 18, 2022
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
Prev Previous commit
Next Next commit
done
  • Loading branch information
sahiljoster32 committed Dec 21, 2021
commit 9667b756a918a182c18a1c2f30286545183f80ec
22 changes: 13 additions & 9 deletions core/domain/caching_services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from core.platform import models
from core.tests import test_utils

from typing import Any, Dict

memory_cache_services = models.Registry.import_cache_services()


Expand Down Expand Up @@ -249,34 +251,34 @@ def test_serialization_and_deserialization_returns_the_same_object(
'exp_id_1', title='A title', category='A category'))
self.assertEqual(
default_exploration.to_dict(),
deserialize(serialize(default_exploration)).to_dict())
deserialize(serialize(default_exploration)).to_dict()) # type: ignore[operator]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this ignore needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

similar to above, but here we are invoking method on object which was returned by deserialize

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mytype don't know that this is mapped with function object.

Copy link
Contributor

Choose a reason for hiding this comment

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

Again I think if you type the SERIALIZE_FUNCTIONS it should help

Copy link
Contributor Author

@sahiljoster32 sahiljoster32 Dec 28, 2021

Choose a reason for hiding this comment

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

in SERIALIZE_FUNCTIONS keys are str, but every value is lambda functions. so, every function returns string using serialize method on object. like :-
if we call CACHE_NAMESPACE_SKILL on SERIALIZE_FUNCTIONS it returns JSON-encoded str encoding all of the information composing the skill object.

CACHE_NAMESPACE_SKILL: lambda x: x.serialize(),

Copy link
Contributor Author

@sahiljoster32 sahiljoster32 Dec 28, 2021

Choose a reason for hiding this comment

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

shall i create a new function taking lambda x: x.serialize() as body of that function and return that x as a casted string, then call that function on every SERIALIZATION_FUNCTIONS's value.
Then assign SERIALIZATION_FUNCTIONS dict[str, str]

is that ok ??!

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure what you mean, it should be possible to just type the serialize function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!, ignore stmt removed.


def test_invalid_namespace_raises_error(self) -> None:
invalid_namespace = 'invalid'
key_value_mapping = {'a': '1', 'b': '2', 'c': '3'}

with self.assertRaisesRegexp(
with self.assertRaisesRegexp(# type: ignore[no-untyped-call]
ValueError,
'Invalid namespace: %s.' % invalid_namespace):
caching_services.set_multi(
invalid_namespace, None,
key_value_mapping)

with self.assertRaisesRegexp(
with self.assertRaisesRegexp(# type: ignore[no-untyped-call]
ValueError,
'Invalid namespace: %s.' % invalid_namespace):
caching_services.get_multi(
invalid_namespace, None,
['a', 'b', 'c'])

with self.assertRaisesRegexp(
with self.assertRaisesRegexp(# type: ignore[no-untyped-call]
ValueError,
'Invalid namespace: %s.' % invalid_namespace):
caching_services.delete_multi(
invalid_namespace, None, ['a', 'b', 'c'])

invalid_sub_namespace = 'sub:namespace'
with self.assertRaisesRegexp(
with self.assertRaisesRegexp(# type: ignore[no-untyped-call]
ValueError,
'Sub-namespace %s cannot contain \':\'.' % invalid_sub_namespace):
caching_services.get_multi(
Expand Down Expand Up @@ -390,7 +392,7 @@ def test_partial_fetches_returns_correct_elements(self) -> None:

self.assertEqual(
default_exploration.to_dict(),
result.get(exploration_id).to_dict())
result.get(exploration_id).to_dict()) # type: ignore[union-attr]
vojtechjelinek marked this conversation as resolved.
Show resolved Hide resolved

self.assertFalse(nonexistent_exploration_id in result)

Expand Down Expand Up @@ -437,7 +439,7 @@ def test_queries_to_wrong_sub_namespace_returns_none(self) -> None:
'1',
[exploration_id])
self.assertEqual(
existent_result.get(exploration_id).to_dict(),
existent_result.get(exploration_id).to_dict(),# type: ignore[union-attr]
vojtechjelinek marked this conversation as resolved.
Show resolved Hide resolved
default_exploration.to_dict())

def test_set_multi_returns_true_for_successful_insert_into_cache(
Expand Down Expand Up @@ -570,7 +572,8 @@ def test_config_properties_identically_cached_in_dev_and_test_environment(
server should be the same as the string that is set to the testing cache
on the testing server.
"""
def mock_memory_cache_services_set_multi(id_value_mapping) -> None:
def mock_memory_cache_services_set_multi(
id_value_mapping: Dict[str, str]) -> None:
vojtechjelinek marked this conversation as resolved.
Show resolved Hide resolved
# This mock asserts that for the same config domain attribute
# containing unicode characters, the string that is set to the cache
# in the testing environment is the same as the string set to the
Expand Down Expand Up @@ -648,7 +651,8 @@ def test_explorations_identically_cached_in_dev_and_test_environment(
default_exploration = exp_domain.Exploration.from_dict(
self.exploration_dict_with_unicode_characters)

def mock_memory_cache_services_set_multi(id_value_mapping) -> None:
def mock_memory_cache_services_set_multi(
id_value_mapping: Dict[str, Any]) -> None:
# The json encoded string is the string that is set to the cache
# when an exploration is created in the development server. This
# mock asserts that for the same exploration, the string
Expand Down
9 changes: 5 additions & 4 deletions core/domain/collection_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,11 @@ def to_dict(self):

@classmethod
def create_default_collection(
cls, collection_id, title=feconf.DEFAULT_COLLECTION_TITLE,
category=feconf.DEFAULT_COLLECTION_CATEGORY,
objective=feconf.DEFAULT_COLLECTION_OBJECTIVE,
language_code=constants.DEFAULT_LANGUAGE_CODE):
cls, collection_id: str,
title: str =feconf.DEFAULT_COLLECTION_TITLE,
category: str =feconf.DEFAULT_COLLECTION_CATEGORY,
objective: str =feconf.DEFAULT_COLLECTION_OBJECTIVE,
language_code: str =constants.DEFAULT_LANGUAGE_CODE):
vojtechjelinek marked this conversation as resolved.
Show resolved Hide resolved
"""Returns a Collection domain object with default values.

Args:
Expand Down
25 changes: 16 additions & 9 deletions core/domain/exp_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import collections
import copy
import datetime
import json
import re
import string
Expand All @@ -41,6 +42,8 @@
from core.domain import state_domain
from core.platform import models

from typing import Any, Dict

(exp_models,) = models.Registry.import_models([models.NAMES.exploration])


Expand Down Expand Up @@ -620,11 +623,13 @@ def __init__(

@classmethod
def create_default_exploration(
cls, exploration_id, title=feconf.DEFAULT_EXPLORATION_TITLE,
init_state_name=feconf.DEFAULT_INIT_STATE_NAME,
category=feconf.DEFAULT_EXPLORATION_CATEGORY,
objective=feconf.DEFAULT_EXPLORATION_OBJECTIVE,
language_code=constants.DEFAULT_LANGUAGE_CODE):
cls, exploration_id: str,
title: str=feconf.DEFAULT_EXPLORATION_TITLE,
init_state_name: str=feconf.DEFAULT_INIT_STATE_NAME,
category: str=feconf.DEFAULT_EXPLORATION_CATEGORY,
objective: str=feconf.DEFAULT_EXPLORATION_OBJECTIVE,
language_code: str=constants.DEFAULT_LANGUAGE_CODE
) -> Exploration:
vojtechjelinek marked this conversation as resolved.
Show resolved Hide resolved
"""Returns a Exploration domain object with default values.

'title', 'init_state_name', 'category', 'objective' if not provided are
Expand Down Expand Up @@ -661,9 +666,11 @@ def create_default_exploration(

@classmethod
def from_dict(
cls, exploration_dict,
exploration_version=0, exploration_created_on=None,
exploration_last_updated=None):
cls, exploration_dict: Dict[str, Any],
exploration_version: int =0,
exploration_created_on: datetime.datetime=None,
exploration_last_updated: datetime.datetime=None
) -> Exploration:
"""Return a Exploration domain object from a dict.

Args:
Expand Down Expand Up @@ -2327,7 +2334,7 @@ def to_yaml(self):

return python_utils.yaml_from_dict(exp_dict)

def to_dict(self):
def to_dict(self) -> Dict[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the typing, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

"""Returns a copy of the exploration as a dictionary. It includes all
necessary information to represent the exploration.

Expand Down
4 changes: 3 additions & 1 deletion core/domain/platform_parameter_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from core.constants import constants
from core.domain import change_domain

from typing import Any, Dict

SERVER_MODES = python_utils.create_enum('dev', 'test', 'prod') # pylint: disable=invalid-name
FEATURE_STAGES = SERVER_MODES # pylint: disable=invalid-name
DATA_TYPES = python_utils.create_enum('bool', 'string', 'number') # pylint: disable=invalid-name
Expand Down Expand Up @@ -756,7 +758,7 @@ def _validate_feature_flag(self):
'production environment.')

@classmethod
def from_dict(cls, param_dict):
def from_dict(cls, param_dict: Dict[str, Any]):
vojtechjelinek marked this conversation as resolved.
Show resolved Hide resolved
"""Returns an PlatformParameter object from a dict.

Args:
Expand Down
10 changes: 7 additions & 3 deletions core/domain/skill_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from core.domain import html_validation_service
from core.domain import state_domain

from typing import Any, Dict, List
vojtechjelinek marked this conversation as resolved.
Show resolved Hide resolved

# Do not modify the values of these constants. This is to preserve backwards
# compatibility with previous change dicts.
SKILL_PROPERTY_DESCRIPTION = 'description'
Expand Down Expand Up @@ -288,7 +290,7 @@ def validate(self):
class Rubric:
"""Domain object describing a skill rubric."""

def __init__(self, difficulty, explanations):
def __init__(self, difficulty: str, explanations: List[str]):
"""Initializes a Rubric domain object.

Args:
Expand Down Expand Up @@ -755,7 +757,7 @@ def validate(self):
'Expected a value for all_questions_merged when '
'superseding_skill_id is set.')

def to_dict(self):
def to_dict(self) -> Dict[str, Any]:
"""Returns a dict representing this Skill domain object.

Returns:
Expand Down Expand Up @@ -883,7 +885,9 @@ def from_dict(
return skill

@classmethod
def create_default_skill(cls, skill_id, description, rubrics):
def create_default_skill(
cls, skill_id: str, description: str, rubrics: List[Rubric]
) -> Skill:
"""Returns a skill domain object with default values. This is for
the frontend where a default blank skill would be shown to the user
when the skill is created for the first time.
Expand Down
9 changes: 6 additions & 3 deletions core/domain/story_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from core.domain import html_cleaner
from core.domain import html_validation_service

from typing import Any, Dict

vojtechjelinek marked this conversation as resolved.
Show resolved Hide resolved
# Do not modify the values of these constants. This is to preserve backwards
# compatibility with previous change dicts.
STORY_PROPERTY_TITLE = 'title'
Expand Down Expand Up @@ -861,7 +863,7 @@ def has_exploration(self, exp_id):
return True
return False

def to_dict(self):
def to_dict(self) -> Dict[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the typing and add ignore to the callsite

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

"""Returns a dict representing this Story domain object.

Returns:
Expand Down Expand Up @@ -976,8 +978,9 @@ def from_dict(

@classmethod
def create_default_story(
cls, story_id, title, description, corresponding_topic_id,
url_fragment):
cls, story_id: str, title: str,
description: str, corresponding_topic_id: str,
url_fragment: str) -> Story:
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the typing and add ignore to the callsite

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

"""Returns a story domain object with default values. This is for
the frontend where a default blank story would be shown to the user
when the story is created for the first time.
Expand Down
7 changes: 5 additions & 2 deletions core/domain/topic_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from core.domain import subtopic_page_domain
from core.domain import user_services

from typing import Any, Dict

CMD_CREATE_NEW = feconf.CMD_CREATE_NEW
CMD_CHANGE_ROLE = feconf.CMD_CHANGE_ROLE
CMD_REMOVE_MANAGER_ROLE = feconf.CMD_REMOVE_MANAGER_ROLE
Expand Down Expand Up @@ -561,7 +563,7 @@ def __init__(
self.practice_tab_is_displayed = practice_tab_is_displayed
self.page_title_fragment_for_web = page_title_fragment_for_web

def to_dict(self):
def to_dict(self) -> Dict[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the typing and add ignore to the callsite

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

"""Returns a dict representing this Topic domain object.

Returns:
Expand Down Expand Up @@ -1132,7 +1134,8 @@ def validate(self, strict=False):

@classmethod
def create_default_topic(
cls, topic_id, name, url_fragment, description):
cls, topic_id: str, name: str, url_fragment: str, description: str
) -> Topic:
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the typing and add ignore to the callsite

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

"""Returns a topic domain object with default values. This is for
the frontend where a default blank topic would be shown to the user
when the topic is created for the first time.
Expand Down
4 changes: 2 additions & 2 deletions core/storage/collection/gae_models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_get_deletion_policy(self) -> None:
base_models.DELETION_POLICY.NOT_APPLICABLE)

def test_get_collection_count(self) -> None:
collection = collection_domain.Collection.create_default_collection( # type: ignore[no-untyped-call]
collection = collection_domain.Collection.create_default_collection(
'id', title='A title',
category='A Category', objective='An Objective')
collection_services.save_new_collection('id', collection) # type: ignore[no-untyped-call]
Expand All @@ -69,7 +69,7 @@ def test_get_collection_count(self) -> None:
self.assertEqual(num_collections, 1)

def test_reconstitute(self) -> None:
collection = collection_domain.Collection.create_default_collection( # type: ignore[no-untyped-call]
collection = collection_domain.Collection.create_default_collection(
'id', title='A title',
category='A Category', objective='An Objective')
collection_services.save_new_collection('id', collection) # type: ignore[no-untyped-call]
Expand Down
4 changes: 2 additions & 2 deletions core/storage/exploration/gae_models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_get_deletion_policy(self) -> None:
base_models.DELETION_POLICY.NOT_APPLICABLE)

def test_get_exploration_count(self) -> None:
exploration = exp_domain.Exploration.create_default_exploration( # type: ignore[no-untyped-call]
exploration = exp_domain.Exploration.create_default_exploration(
'id', title='A Title',
category='A Category', objective='An Objective')
exp_services.save_new_exploration('id', exploration) # type: ignore[no-untyped-call]
Expand All @@ -72,7 +72,7 @@ def test_get_exploration_count(self) -> None:
self.assertEqual(saved_exploration.objective, 'An Objective')

def test_reconstitute(self) -> None:
exploration = exp_domain.Exploration.create_default_exploration( # type: ignore[no-untyped-call]
exploration = exp_domain.Exploration.create_default_exploration(
'id', title='A Title',
category='A Category', objective='An Objective')
exp_services.save_new_exploration('id', exploration) # type: ignore[no-untyped-call]
Expand Down
4 changes: 2 additions & 2 deletions core/storage/topic/gae_models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_that_subsidiary_models_are_created_when_new_model_is_saved(
)

def test_get_by_name(self) -> None:
topic = topic_domain.Topic.create_default_topic( # type: ignore[no-untyped-call]
topic = topic_domain.Topic.create_default_topic(
self.TOPIC_ID, self.TOPIC_NAME, 'name', 'description')
topic_services.save_new_topic(feconf.SYSTEM_COMMITTER_ID, topic) # type: ignore[no-untyped-call]
topic_model = topic_models.TopicModel.get_by_name(self.TOPIC_NAME)
Expand All @@ -111,7 +111,7 @@ def test_get_by_name(self) -> None:
self.assertEqual(topic_model.id, self.TOPIC_ID)

def test_get_by_url_fragment(self) -> None:
topic = topic_domain.Topic.create_default_topic( # type: ignore[no-untyped-call]
topic = topic_domain.Topic.create_default_topic(
self.TOPIC_ID, self.TOPIC_NAME, 'name-two', 'description')
topic_services.save_new_topic(feconf.SYSTEM_COMMITTER_ID, topic) # type: ignore[no-untyped-call]
topic_model = topic_models.TopicModel.get_by_name(self.TOPIC_NAME)
Expand Down
1 change: 0 additions & 1 deletion scripts/run_mypy_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
'core/domain/blog_services_test.py',
'core/domain/blog_validators.py',
'core/domain/blog_validators_test.py',
'core/domain/caching_services_test.py',
'core/domain/calculation_registry.py',
'core/domain/calculation_registry_test.py',
'core/domain/change_domain.py',
Expand Down