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 169 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.

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.

core/controllers/contributor_dashboard_test.py Outdated Show resolved Hide resolved
core/controllers/topic_editor.py Outdated Show resolved Hide resolved
core/domain/email_manager.py Outdated Show resolved Hide resolved
Comment on lines 7101 to 7103
'%s%s/story_1' % (
feconf.OPPIA_SITE_URL,
feconf.STORY_EDITOR_URL_PREFIX)
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.

core/domain/exp_services_test.py Outdated Show resolved Hide resolved
core/domain/collection_domain.py Outdated Show resolved Hide resolved
suggestion_models.SCORE_TYPE_TRANSLATION +
suggestion_models.SCORE_CATEGORY_DELIMITER + 'English')
'%s%s%s' % (
suggestion_models.SCORE_TYPE_TRANSLATION,
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.

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.

Please make sure to actually fix the things I mentioned in my previous comments in ALL files you modify. Get rid of .format(, f-strings, nested formatting ('aaaa %s dddd' % ('%s %s' % 'bbbb cccc').

Comment on lines 1456 to 1457
'backend file system at /exploration/%s/assets/image/ %s' % (
exp_id, 'before submitting this translation again.'),
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
'backend file system at /exploration/%s/assets/image/ %s' % (
exp_id, 'before submitting this translation again.'),
'backend file system at /exploration/%s/assets/image/ %s'
'before submitting this translation again.' % exp_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

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.

'on or before the planned date or adjust the planned publication date.'
'<br><br>'
'<ol>%s</ol>'
),
).format(constants.CHAPTER_PUBLICATION_NOTICE_PERIOD_IN_DAYS),
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 use %?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I used %, the variable constants.Chapter.... when into the %s in between the ol tags.

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
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 7045 to 7047
'%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.

Why have nested formatting, can you move '%s%s/story_1' to the string above?

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.

entity_type='exploration', entity_id=self.EXP_ID_3,
original_author_id=self.user_id, subject='Feedback',
status=feedback_models.STATUS_CHOICES_OPEN, message_count=0,
has_suggestion=False)
thread_3.update_timestamps()
thread_3.put()
feedback_services.create_message(
'exploration.' + self.EXP_ID_3 + '.' + self.THREAD_ID,
f'exploration.{self.EXP_ID_3}.{self.THREAD_ID}',
Copy link
Member

Choose a reason for hiding this comment

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

Use %.

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.

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.

PTAL @vojtechjelinek , hope I didn't miss anything!

Comment on lines 1456 to 1457
'backend file system at /exploration/%s/assets/image/ %s' % (
exp_id, 'before submitting this translation again.'),
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.

'on or before the planned date or adjust the planned publication date.'
'<br><br>'
'<ol>%s</ol>'
),
).format(constants.CHAPTER_PUBLICATION_NOTICE_PERIOD_IN_DAYS),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I used %, the variable constants.Chapter.... when into the %s in between the ol tags.

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
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 7045 to 7047
'%s%s/story_1' % (
feconf.OPPIA_SITE_URL, feconf.STORY_EDITOR_URL_PREFIX)
)
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.

entity_type='exploration', entity_id=self.EXP_ID_3,
original_author_id=self.user_id, subject='Feedback',
status=feedback_models.STATUS_CHOICES_OPEN, message_count=0,
has_suggestion=False)
thread_3.update_timestamps()
thread_3.put()
feedback_services.create_message(
'exploration.' + self.EXP_ID_3 + '.' + self.THREAD_ID,
f'exploration.{self.EXP_ID_3}.{self.THREAD_ID}',
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.

Copy link
Member

@U8NWXD U8NWXD left a comment

Choose a reason for hiding this comment

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

I've reviewed everything but the extension code, which I'll get to in the next 48 hrs

Comment on lines -226 to +227
'logged-in-user/subscribe-to-creator-and-view-all-explorations-' +
'by-that-creator',
'logged-in-user/subscribe-to-creator-and-view-all-explorations-'
'by-that-creator',
Copy link
Member

Choose a reason for hiding this comment

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

I think this is actually harder to read without the indentation. When quickly scanning this list, most readers will assume that each line is a list entry, but that's not the case for by-that-creator. I recommend either keeping the indentation or indenting the whole list entry and wrapping in parentheses like this:

'logged-out-user/click-all-links-on-get-started-page',
(
    'logged-in-user/subscribe-to-creator-and-view-all-explorations-'
    'by-that-creator'
),

return '\n'.join(trimmed_error_messages) + '\n'
return '\n%s\n' % ''.join(trimmed_error_messages)
Copy link
Member

Choose a reason for hiding this comment

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

This changes behavior. If trimmed_error_messages were the list [1, 2, 3], the original code would produce 1\n2\n3\n, but your new code will produce \n123\n.

Comment on lines -529 to 530
return '\n'.join(trimmed_error_messages) + '\n'
return '\n%s\n' % trimmed_error_messages

Copy link
Member

Choose a reason for hiding this comment

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

ditto: changes behavior

Comment on lines -179 to 180
'http://localhost:9876/base/core/templates/' +
'http://localhost:9876/base/core/templates/'
'combined-tests.spec.js',
Copy link
Member

Choose a reason for hiding this comment

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

ditto: indent with parentheses to make it clear that these two lines are a single list entry

@oppiabot oppiabot bot unassigned U8NWXD Jun 3, 2024
Copy link

oppiabot bot commented Jun 3, 2024

Unassigning @U8NWXD since the review is done.

Copy link

oppiabot bot commented Jun 4, 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
Member

@U8NWXD U8NWXD left a comment

Choose a reason for hiding this comment

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

Finished reviewing

Comment on lines +2957 to +2960
if any(isinstance(inferred, (astroid.Instance, astroid.Const)) and
isinstance(inferred.pytype(), str) and
'datetime.datetime' in inferred.pytype()
for inferred in [left_inferred, right_inferred]):
Copy link
Member

Choose a reason for hiding this comment

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

Can you leave a comment explaining what case this is designed to handle? Also please fix the style:

Suggested change
if any(isinstance(inferred, (astroid.Instance, astroid.Const)) and
isinstance(inferred.pytype(), str) and
'datetime.datetime' in inferred.pytype()
for inferred in [left_inferred, right_inferred]):
if any(
isinstance(inferred, (astroid.Instance, astroid.Const)) and
isinstance(inferred.pytype(), str) and
'datetime.datetime' in inferred.pytype()
for inferred in [left_inferred, right_inferred]
):

The above change fixes 2 things:

  1. When code inside brackets/parentheses needs to be split multiple lines, the whole contents of the brackets/parentheses should be indented in a single block rather than treating the first line specially and only indenting the later lines. This is enforced by a linter for lists, but the same principle applies here.
  2. Follows the same indentation pattern as the next if statement for long if conditions

with self.checker_test_object.assertNoMessages():
self.checker_test_object.checker.visit_binop(node)

def test_does_not_encourage_string_interpolation_with_datetime_concatenation(self) -> None: # pylint: disable=line-too-long
Copy link
Member

Choose a reason for hiding this comment

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

This isn't really a concatentation; it's more like addition since the timedelta is being added to the timestamp in the datetime object

@U8NWXD U8NWXD removed their assignment Jun 4, 2024
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. PR: don't merge - HAS MERGE CONFLICTS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants