Should I assume all the type checking are not ignored? #18041
-
TL;DR: If there are some branches of if statements that can only be covered by tests using the Hi, I am new to Oppia and am currently working on issue #14219 for I noticed there are 3 if statements (see below) whose false branches are not covered. However, I find that they are checking the existence of fields in an instance of a subtype of So should I 1. just remove the three if statements as they will never fail, or 2. use Here is the related code: def validate_change_dict_for_blog_post(
change_dict: blog_services.BlogPostChangeDict
) -> blog_services.BlogPostChangeDict:
""" Docstring omitted
"""
if 'title' in change_dict: # 1
blog_domain.BlogPost.require_valid_title(
change_dict['title'], True)
if 'thumbnail_filename' in change_dict: # 2
blog_domain.BlogPost.require_valid_thumbnail_filename(
change_dict['thumbnail_filename'])
if 'tags' in change_dict: # 3
blog_domain.BlogPost.require_valid_tags(
change_dict['tags'], False)
# rest of the function is omitted The three commented if statements are checking if certain fields are present in the class BlogPostChangeDict(TypedDict):
"""Dictionary representing the change_dict for BlogPost domain object."""
title: str
content: str
tags: List[str]
thumbnail_filename: str So the given fields are required to exist by the type system. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
In this case you should write tests to cover those Once we have the backend code fully typed and are confident that we can rely on the type checking, we can ignore these |
Beta Was this translation helpful? Give feedback.
-
Oops, I just noticed another question. What should be the expected behavior then? Should my tests expect |
Beta Was this translation helpful? Give feedback.
-
It seems like I will have to modify the function. However, I am uncertain whether this should be incorporated in the same PR since the objective of it is to solely enhance the coverage. Moreover, are there any specific preferences or guidelines that I should follow while writing the exception message, or can I simply draft it based on what I consider to be informative? |
Beta Was this translation helpful? Give feedback.
-
Raising errors noisily seems to break some other tests, e.g. |
Beta Was this translation helpful? Give feedback.
@INFCode Thanks for catching this! The test is making an incorrect assumption and should be modified. You can also add a test with an invalid change dict for this case to ensure it results in an error.
(Btw, this is a case where the updates you're making caught a bug in the tests/code, so that's something to celebrate!)