question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding Mypy type annotations to domain folder files

See original GitHub issue

This issue will track the type annotation process of all the core/domain/*.py (and their test files). To get started, ask to be assigned to any unclaimed task. Tasks are claimed by adding the username of the claimer to the end of the task, e.g. @example.

Note: Main code file and its test file must be type-annotated together.

The general procedure to add type annotations is as follows:

  1. Start with the main code file first.
  2. Remove the file to be annotated from the mypy denylist (i.e. from NOT_FULLY_COVERED_FILES in scripts/run_mypy_checks.py). Run the mypy checks on the code file using python -m scripts.run_mypy_checks --files <path/to/file> to understand all the errors mypy is reporting for this file.
  3. Go over them one by one, and fix them by following the detailed instructions as mentioned in our wiki.
  4. PAY ATTENTION to our guidelines for using type ignores in the codebase.
  5. For testing the changes after annotating all the files, run python -m scripts.run_mypy_checks, which runs the type checks on the entire codebase, and ensure that no errors are being reported.
  6. If errors occur in other files due to you change make sure to also fix those other files.

Good examples of already annotated files in the codebase:

  1. Check out the core/storage folder.
  2. Ref PR for domain file type annotation: #14034

If you face problems in understanding a particular case while adding annotations, please go over the tips mentioned in the wiki once, and let us know if you are still stuck!

Tasks/files:

  • core/domain/action_registry.py@sahiljoster32
  • core/domain/activity_services.py@sahiljoster32
  • core/domain/auth_services.py @IamhappyXD
  • core/domain/blog_services.py @IamhappyXD
  • core/domain/calculation_registry.py@sahiljoster32
  • core/domain/classifier_services.py
  • core/domain/classroom_services.py @SD-13
  • core/domain/collection_services.py
  • core/domain/config_services.py@sahiljoster32
  • core/domain/cron_services.py @SD-13
  • core/domain/customization_args_util.py
  • core/domain/draft_upgrade_services.py
  • core/domain/email_manager.py
  • core/domain/email_services.py @jordyparker
  • core/domain/email_subscription_services.py@sahiljoster32
  • core/domain/event_services.py
  • core/domain/exp_fetchers.py
  • core/domain/exp_services.py
  • core/domain/expression_parser.py
  • core/domain/feedback_services.py
  • core/domain/fs_services.py @sahiljoster32
  • core/domain/html_cleaner.py @SD-13
  • core/domain/html_validation_service.py
  • core/domain/image_services.py @gopivaibhav
  • core/domain/image_validation_services.py @hsadia538
  • core/domain/improvements_services.py
  • core/domain/interaction_registry.py
  • core/domain/learner_goals_services.py
  • core/domain/learner_playlist_services.py
  • core/domain/learner_progress_services.py
  • core/domain/moderator_services.py
  • core/domain/object_registry.py
  • core/domain/opportunity_services.py
  • core/domain/platform_feature_services.py
  • core/domain/platform_parameter_list.py @Darsuu
  • core/domain/platform_parameter_list_test.py @Darsuu
  • core/domain/platform_parameter_registry.py @Darsuu
  • core/domain/playthrough_issue_registry.py @Darsuu
  • core/domain/question_fetchers.py @winnie368c
  • core/domain/question_services.py
  • core/domain/rating_services.py @IamhappyXD
  • core/domain/recommendations_services.py
  • core/domain/rights_manager.py
  • core/domain/role_services.py @gopivaibhav
  • core/domain/rte_component_registry.py @hsadia538
  • core/domain/rules_registry.py @hsadia538
  • core/domain/search_services.py
  • core/domain/skill_fetchers.py
  • core/domain/skill_services.py
  • core/domain/stats_services.py
  • core/domain/story_fetchers.py
  • core/domain/story_services.py
  • core/domain/subscription_services.py
  • core/domain/subtopic_page_services.py @ashish-patwal
  • core/domain/suggestion_registry.py
  • core/domain/suggestion_services.py
  • core/domain/summary_services.py
  • core/domain/takeout_service.py @jordyparker
  • core/domain/taskqueue_services.py @jordyparker
  • core/domain/topic_fetchers.py @SD-13
  • core/domain/topic_services.py @SD-13
  • core/domain/translatable_object_registry.py @ashish-patwal
  • core/domain/translation_fetchers.py @sahiljoster32
  • core/domain/translation_services.py @sahiljoster32
  • core/domain/user_query_services.py @SD-13
  • core/domain/user_services.py @SD-13
  • core/domain/visualization_registry.py' @SD-13
  • core/domain/voiceover_services.py @SD-13
  • core/domain/wipeout_service.py @SD-13
  • core/domain/change_domain.py @div-yam
  • core/domain/collection_domain.py @sahiljoster32
  • core/domain/exp_domain.py @Yavnikaa
  • core/domain/fs_domain.py @PranshuSrivastava
  • core/domain/param_domain.py @Asmit2952 (attempted in #14302)
  • core/domain/platform_parameter_domain.py @Asmit2952 (attempted in #14302)
  • core/domain/question_domain.py @Asmit2952 (attempted in #14302)
  • core/domain/skill_domain.py @dhruvshrivastava
  • core/domain/state_domain.py @dhruvshrivastava
  • core/domain/stats_domain.py @winnie368c
  • core/domain/story_domain.py @Sangkyun-Kim15
  • core/domain/subtopic_page_domain.py @ashish-patwal
  • core/domain/user_domain.py@sahiljoster32
  • core/domain/user_query_domain.py @ashish-patwal

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:242 (182 by maintainers)

github_iconTop GitHub Comments

2reactions
vojtechjelinekcommented, Nov 13, 2021

Hi @vojtechjelinek , @sajalasati !

I have a question concerning an annotation for the platform_parameter_domain.py file (develop/core/domain/platform_parameter_domain.py ).

I am working on mypy typing but I am getting some trouble with a function that appeared to be recursive: . I am not sure if mypy can handle recursive function properly. Capture d’écran 2021-11-12 à 23 00 58

We have defined Mapping as below : Mapping = TypedDict ('Mapping', {'filters' : List[PlatformParameterFilter] , 'value_when_matched' : float })

We got this error : core/domain/platform_parameter_domain.py:556: error: Argument 1 to "from_dict" of "PlatformParameterFilter" has incompatible type "PlatformParameterFilter"; expected "Filter_dict" [arg-type]

Thank you

I think the Mapping is wrong as the value of field with key 'filters' should be List[Dict[…]] or List[SomeTypedDict]]. Also, use more descriptive naming for the TypedDict and use the class installation of TypedDict, so in your case:

class PlatformParameterRuleDict(TypedDict):
    filters: …
    value_when_matched: float

Also, you don’t seem to be assigned to that file, so not sure why are you working on it.

2reactions
vojtechjelinekcommented, Oct 22, 2021

I think you should use the alternative syntax for TypedDict’s and define the TypedDicts at a top of a file. So if we have a function that returns {'id': 'abcd', 'active': True, 'count': 1} and represent for example some kind of counter then the type variable should be Counter = TypedDict('Counter', {'id': str, 'active': bool, 'count': int})

Let’s use the class syntax. So if we have a function that returns {'id': 'abcd', 'active': True, 'count': 1} and represent for example some kind of counter then the class should be


class CounterDict(TypedDict)
    id: str
    active: bool
    count: int
Read more comments on GitHub >

github_iconTop Results From Across the Web

mypy: Add type annotations to 'nova.pci' - OpenDev
The 'nova.pci' module is poorly understood and difficult to debug. Start adding type hints to make this a little easier to parse and...
Read more >
Using mypy with an existing codebase
Using mypy with an existing codebase#. This section explains how to get started using mypy with an existing, significant codebase that has little...
Read more >
how to explain domain specific constraints to mypy
1 Answer 1 ; : return my_dict.get('a') ; : ret_val = my_dict.get('a') or my_dict[ ; : return cast(int, my_dict.get( ...
Read more >
PEP 561 – Distributing and Packaging Type Information
“Module” a file containing Python runtime code or stubbed type information. “Package” a directory or directories that namespace Python modules.
Read more >
Python Static Type Checking with Mypy - Linode
You can configure Mypy using a configuration file named mypy.ini . ... Any mypy command launched in that directory behaves as though it's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found