oppia: [BUG]: Get rid of setattr in the codebase and add a lint rule to prohibit it.
Describe the bug
The use of setattr is messing up our static type checking using mypy. To keep the code stable, it would be nice to get rid of it.
URL of the page where the issue is observed.
N/A
Steps To Reproduce
We use setattr in two places in the codebase, as well as a bunch of test files (not listed here):
~/core/domain/change_domain.py:
214
215 for attribute_name in cmd_attribute_names:
216: setattr(self, attribute_name, change_dict.get(attribute_name))
217
~/core/domain/wipeout_service.py:
1411 """
1412 for model in models_to_pseudonymize:
1413: setattr(
1414 model,
1415 name_of_property_containing_user_ids,
The aim of this issue is to remove setattr from the codebase and add a lint check to prohibit its use in the future.
Expected Behavior
There should be no occurrences of setattr in the codebase.
Additionally, if a developer adds setattr to the codebase, we should have a CI lint error and prevent merging that PR.
Screenshots/Videos
No response
What device are you using?
Desktop
Operating System
Linux
What browsers are you seeing the problem on?
Other
Browser version
N/A
Additional context
(From https://github.com/oppia/oppia/issues/21159) Here’s an example of mypy missing some type errors from commit 192f0a9a4866debac160015bc949130aaae6a7fe:
In the create_suggestion method, the change_cmd parameter is annotated with Mapping[str, change_domain.AcceptableChangeDictTypes]. The AcceptableChangeDictTypes union type includes a variety of types such as str, bool, float, int, None, List[str], and several domain-specific dictionary types. This annotation indicates that change_cmd can contain any of these types.
Normally, if a variable were declared with the type AcceptableChangeDictTypes and later passed to a function expecting a str, mypy would flag this as a potential error because AcceptableChangeDictTypes can also include types like List[str], which are not compatible with a str type.
However, in this case, setattr is used to assign a value from change_dict which could be of any type included in AcceptableChangeDictTypes to an attribute that is declared to be of type str. Since setattr bypasses mypy type checking, the assignment of a List[str] to an instance variable declared to be of type str (in a subclass of BaseChange) does not throw a type error. Then, mypy assumes that the attribute, which is declared as str, will always be of type str, even though it might actually hold a List[str] or another type from AcceptableChangeDictTypes.
This is why mypy does not catch the type mismatch when a function like _get_plain_text_from_html_content_string, which expects a str, encounters an attribute that is, in fact, a List[str]. The use of setattr bypasses mypy’s static type checking, leading to potential runtime type errors that mypy does not detect.
This bug is the case study for this tutorial, which has a more detailed analysis: https://github.com/oppia/oppia/wiki/Tutorial-Learn-to-Figure-Out-the-Reproduction-Steps-for-a-Server-Error
Tips for developers
Before addressing the bug, please identify which PR caused the issue (you can follow the steps here). If you identify the PR, comment on the issue with a link to it. If not, mention the commit hash of the oldest commit you saw the bug on (and the month and year it was made in).
Then, please leave a comment with details of the approach that you plan to take to fix the issue (see example).
Note: If this is your first Oppia issue, please make sure to follow our guidelines for choosing an issue and setting things up. You will also need to show a demo of the fix working correctly on your local machine. Thanks!
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 15 (6 by maintainers)
I agree – it’s OK to open a sample PR for this and link to it here as proof that you understand and know how to fix the issue. Thanks for checking.
Yes, an approach like that seems reasonable. We should fix any issues that mypy subsequently discovers as part of the PR which implements this.
Feel free to reply to this thread with a proof-of-concept if you’d like to take this up.