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 issue #8423 Lint concatenation checker #19908

Open
wants to merge 124 commits into
base: develop
Choose a base branch
from

Conversation

Helper2020
Copy link
Contributor

@Helper2020 Helper2020 commented Mar 8, 2024

Overview

  1. This PR fixes or fixes part of Implement new linter checks #8423].
  2. This PR adds Add a lint check to prevent the usage of string concatenation and promote string interpolation.

Essential Checklist

  • The PR title starts with "Fix #bugnum: " or "Fix part of #bugnum: ...", followed by a short, clear summary of the changes.
  • I have followed the instructions for making a code change.
  • I have assigned the correct reviewers to this PR (or will leave a comment with the phrase "@{{reviewer_username}} PTAL" if I don't have permissions to assign reviewers directly).
  • The linter/Karma presubmit checks pass on my local machine, and my PR follows the coding style guide).
  • "Allow edits from maintainers" is checked. (See here for instructions on how to enable it.)

@Helper2020 Helper2020 requested a review from a team as a code owner March 8, 2024 22:27
Copy link

oppiabot bot commented Mar 8, 2024

Hi @Helper2020, can you complete the following:

  1. The body of this PR is missing the overview section, please update it to include the overview.
  2. The body of this PR is missing the proof that changes are correct section, please update it to include the section.
    Thanks!

Copy link

oppiabot bot commented Mar 8, 2024

Assigning @vojtechjelinek for the first pass review of this PR. Thanks!

@Helper2020
Copy link
Contributor Author

@seanlip PTAL, @U8NWXD PTAL

@oppiabot oppiabot bot assigned seanlip and U8NWXD and unassigned Helper2020 Mar 8, 2024
Copy link

oppiabot bot commented Mar 8, 2024

Unassigning @Helper2020 since a re-review was requested. @Helper2020, please make sure you have addressed all review comments. Thanks!

Copy link
Member

@seanlip seanlip left a comment

Choose a reason for hiding this comment

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

Thanks @Helper2020! Could you please fix the lint checks that are failing on CI? Thanks.

@seanlip seanlip assigned Helper2020 and unassigned seanlip Mar 9, 2024
Copy link
Member

@vojtechjelinek vojtechjelinek left a comment

Choose a reason for hiding this comment

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

Thanks! Left a few comments.

Comment on lines 2954 to 2956
if (isinstance(left_inferred.value, str) and
isinstance(right_inferred.value, str)):
self.add_message('prefer-string-interpolation', node=node)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (isinstance(left_inferred.value, str) and
isinstance(right_inferred.value, str)):
self.add_message('prefer-string-interpolation', node=node)
if (
isinstance(left_inferred.value, str) and
isinstance(right_inferred.value, str)
):
self.add_message('prefer-string-interpolation', node=node)

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.

scripts/linters/pylint_extensions_test.py Show resolved Hide resolved
Copy link

oppiabot bot commented Mar 10, 2024

Unassigning @vojtechjelinek since the review is done.

@U8NWXD
Copy link
Member

U8NWXD commented Mar 10, 2024

@Helper2020 please correct the issues flagged by Oppiabot in #19908 (comment):

Hi @Helper2020, can you complete the following:

  1. The body of this PR is missing the overview section, please update it to include the overview.
  2. The body of this PR is missing the proof that changes are correct section, please update it to include the section.
    Thanks!

I'm de-assigning myself for now until these issues and CI checks are fixed

@U8NWXD U8NWXD removed their assignment Mar 10, 2024
@Helper2020
Copy link
Contributor Author

@vojtechjelinek I'm not sure how I can handle errors from .infer() without try-except blocks. Any suggestions?

@vojtechjelinek
Copy link
Member

@vojtechjelinek I'm not sure how I can handle errors from .infer() without try-except blocks. Any suggestions?

Why can't you use try-except?

@Helper2020
Copy link
Contributor Author

@vojtechjelinek I'm not sure how I can handle errors from .infer() without try-except blocks. Any suggestions?

Why can't you use try-except?

I can, just according to the coding style guide, raising exceptions are rarely used.

@Helper2020
Copy link
Contributor Author

@StephenYu2018 PTAL, @vojtechjelinek PTAL, @seanlip PTAL

Copy link
Contributor

@StephenYu2018 StephenYu2018 left a comment

Choose a reason for hiding this comment

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

I have no new changes to request. Please address my remaining reviewer comments, as I've left some new comments under those threads. Once that's done, I'll approve these changes.

Copy link
Member

@seanlip seanlip left a comment

Choose a reason for hiding this comment

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

Thanks @Helper2020. Looks great! I just left a few notes on my codeowner files, PTAL.

score_category: str = ('%s%s%s' % (
suggestion_models.SCORE_TYPE_TRANSLATION,
suggestion_models.SCORE_CATEGORY_DELIMITER, 'English')
)
Copy link
Member

Choose a reason for hiding this comment

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

Deindent by 4 (to match opening scope).

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.

suggestion_models.SCORE_TYPE_TRANSLATION +
suggestion_models.SCORE_CATEGORY_DELIMITER + 'English')
'%s%s%s' % (
suggestion_models.SCORE_TYPE_TRANSLATION,
Copy link
Member

Choose a reason for hiding this comment

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

Indent this line and the next by 4 from the previous line, since the ( introduces a new indentation scope.

@@ -410,7 +410,7 @@ def _get_trimmed_error_output(html_lint_output: str) -> str:

for line in html_output_lines:
trimmed_error_messages.append(line)
return '\n'.join(trimmed_error_messages) + '\n'
return '\n%s%s' % (''.join(trimmed_error_messages), '\n')
Copy link
Member

Choose a reason for hiding this comment

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

Why not just put the last \n in the main string literal?

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.

@@ -526,7 +526,7 @@ def _get_trimmed_error_output(eslint_output: str) -> str:
else:
error_message = line
trimmed_error_messages.append(error_message)
return '\n'.join(trimmed_error_messages) + '\n'
return '\n%s%s' % (trimmed_error_messages, '\n')
Copy link
Member

Choose a reason for hiding this comment

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

Ditto, put the last \n in the main string literal

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.

@@ -2919,6 +2919,55 @@ def visit_importfrom(self, node: astroid.nodes.ImportFrom) -> None:
self.add_message('disallowed-text-import', node=node)


# TODO(#16567): Here we use MyPy ignore because of the incomplete typing of
Copy link
Member

Choose a reason for hiding this comment

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

Strike the first "of"

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.

# (Class cannot subclass 'BaseChecker' (has type 'Any')),
# we added an ignore here.
class PreventStringConcatenationChecker(checkers.BaseChecker): # type: ignore[misc]
"""Checks for string concactenation and encourage string interpolation."""
Copy link
Member

Choose a reason for hiding this comment

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

encourage --> encourages

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.

Args: node: astroid.BinOp The binary operation node to check.
"""

if isinstance(node, astroid.BinOp) and node.op == '+':
Copy link
Member

Choose a reason for hiding this comment

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

Not done? (I don't see the changes in the code.)

'-linux-x64.tar.gz',
'https://nodejs.org/dist/v%s/node-v%s%s' % (
common.NODE_VERSION, common.NODE_VERSION,
'-linux-x64.tar.gz'),
Copy link
Member

Choose a reason for hiding this comment

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

Why not just add this to the main string, since it is hardcoded?

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.

@seanlip seanlip assigned Helper2020 and unassigned seanlip May 26, 2024
Copy link

oppiabot bot commented May 28, 2024

Hi @Helper2020. Due to recent changes in the "develop" branch, this PR now has a merge conflict. Please follow this link if you need help resolving the conflict, so that the PR can be merged. Thanks!

Copy link
Contributor Author

@Helper2020 Helper2020 left a comment

Choose a reason for hiding this comment

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

Copy link
Member

@vojtechjelinek vojtechjelinek left a comment

Choose a reason for hiding this comment

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

Thanks! Took a pass. Please make sure to check if similar stuff that I mention in my comments occurs in other places too.

Comment on lines +2008 to +2010
score_category: str = ('%s%s%s' % (
suggestion_models.SCORE_TYPE_TRANSLATION,
suggestion_models.SCORE_CATEGORY_DELIMITER, 'English'))
Copy link
Member

Choose a reason for hiding this comment

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

Put the last string into the main string. Ditto elsewhere.

@@ -736,7 +736,7 @@ def put(self, topic_id: str) -> None:
topic_id: str. The ID of the topic.
"""
assert self.normalized_payload is not None
topic_url = feconf.TOPIC_EDITOR_URL_PREFIX + '/' + topic_id
topic_url = '%s%s%s' % (feconf.TOPIC_EDITOR_URL_PREFIX, '/', topic_id)
Copy link
Member

Choose a reason for hiding this comment

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

Put the '/' into the main string. Ditto elsewhere.

@@ -898,7 +898,7 @@ def _convert_collection_contents_v3_dict_to_v4_dict(
# this key MyPy throw an error.
skill_names.update(node['prerequisite_skills']) # type: ignore[misc]
skill_names_to_ids = {
name: _SKILL_ID_PREFIX + str(index)
name: '{}{}'.format(_SKILL_ID_PREFIX, index)
Copy link
Member

Choose a reason for hiding this comment

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

Why d you use format and not % here?

Copy link
Member

Choose a reason for hiding this comment

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

Ditto elsewhere

Comment on lines +184 to +196
EXPLORATION_ROLE_MANAGER: ('%s%s%s' % (
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_manage'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_edit'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_play'])
),
EXPLORATION_ROLE_EDITOR: ('%s%s' % (
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_edit'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_play'])
),
EXPLORATION_ROLE_VOICE_ARTIST: ('%s%s' % (
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_voiceover'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_play'])
),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
EXPLORATION_ROLE_MANAGER: ('%s%s%s' % (
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_manage'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_edit'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_play'])
),
EXPLORATION_ROLE_EDITOR: ('%s%s' % (
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_edit'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_play'])
),
EXPLORATION_ROLE_VOICE_ARTIST: ('%s%s' % (
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_voiceover'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_play'])
),
EXPLORATION_ROLE_MANAGER: ('%s%s%s' % (
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_manage'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_edit'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_play'])
),
EXPLORATION_ROLE_EDITOR: ('%s%s' % (
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_edit'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_play'])
),
EXPLORATION_ROLE_VOICE_ARTIST: ('%s%s' % (
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_voiceover'],
_EDITOR_ROLE_EMAIL_HTML_RIGHTS['can_play'])
),

Comment on lines +2168 to +2172
story_link = ('%s%s/%s' % (
str(feconf.OPPIA_SITE_URL),
str(feconf.STORY_EDITOR_URL_PREFIX),
overdue_story.id)
)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
story_link = ('%s%s/%s' % (
str(feconf.OPPIA_SITE_URL),
str(feconf.STORY_EDITOR_URL_PREFIX),
overdue_story.id)
)
story_link = ('%s%s/%s' % (
str(feconf.OPPIA_SITE_URL),
str(feconf.STORY_EDITOR_URL_PREFIX),
overdue_story.id
))

Copy link
Member

Choose a reason for hiding this comment

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

Ditto elsewhere

Comment on lines +7101 to +7103
'%s%s/story_1' % (
feconf.OPPIA_SITE_URL,
feconf.STORY_EDITOR_URL_PREFIX)
Copy link
Member

Choose a reason for hiding this comment

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

Please get rid of this format inside format and just modify the main string.

@@ -6372,14 +6372,14 @@ def test_get_story_id_linked_to_exploration(self) -> None:
change_list = [
story_domain.StoryChange({
'cmd': story_domain.CMD_ADD_STORY_NODE,
'node_id': story_domain.NODE_ID_PREFIX + '1',
'node_id': f'{story_domain.NODE_ID_PREFIX}1',
Copy link
Member

Choose a reason for hiding this comment

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

Why use f-strings and not %-format?

Copy link
Member

Choose a reason for hiding this comment

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

Ditto elsewhere

Copy link

oppiabot bot commented May 31, 2024

Unassigning @vojtechjelinek since the review is done.

Copy link
Contributor

@StephenYu2018 StephenYu2018 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link

oppiabot bot commented May 31, 2024

Unassigning @StephenYu2018 since they have already approved the PR.

Copy link

oppiabot bot commented May 31, 2024

Assigning @BenHenning, @U8NWXD for code owner reviews. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Affects datastore layer Labels to indicate a PR that changes the datastore layer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants