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

Improve invalid-slice-index and add invalid-slice-step #7762

Merged
merged 2 commits into from
Nov 14, 2022

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Nov 13, 2022

Description

Followup to pylint-dev/astroid#1844 (comment) by @nelfin.

This PR

  • Improve the invalid-slice-index check to detect invalid indices on string and byte sequences.
"Hallo"["a":"z"]  # invalid-slice-index (2x)
  • Adds a new warning for invalid-slice-step when 0 is passed as step value for common builtin sequences.
["a", "b", "c"][::0]  # invalid-slice-step

@cdce8p cdce8p added Enhancement ✨ Improvement to a component False Negative 🦋 No message is emitted but something is wrong with the code labels Nov 13, 2022
@cdce8p cdce8p added this to the 2.16.0 milestone Nov 13, 2022
@coveralls
Copy link

coveralls commented Nov 13, 2022

Pull Request Test Coverage Report for Build 3457177074

  • 9 of 10 (90.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.0008%) to 95.405%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pylint/checkers/typecheck.py 9 10 90.0%
Files with Coverage Reduction New Missed Lines %
pylint/checkers/typecheck.py 1 96.05%
Totals Coverage Status
Change from base Build 3455918345: 0.0008%
Covered Lines: 17336
Relevant Lines: 18171

💛 - Coveralls

@github-actions

This comment has been minimized.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

LGTM, the error level check are always a pleasure to review :) (Line not covered is due to inference it's hard to test in the current state of the testutil)

@cdce8p
Copy link
Member Author

cdce8p commented Nov 13, 2022

(Line not covered is due to inference it's hard to test in the current state of the testutil)

It's actually a bit more complicated. _check_invalid_slice_index is only called from _check_invalid_sequence_index which includes a pretty strong filter of things too check. So all cases (with custom __getitem__ methods) don't even get to the slice_index check.

I wouldn't want to remove the uncovered line in _check_invalid_slice_index though, as at some point we might want to loosen the strict guard clause a bit.

https://github.com/PyCQA/pylint/blob/435a5a1d8e542d1240b8a66985feb8950fb6d739/pylint/checkers/typecheck.py#L1695-L1701

@github-actions
Copy link
Contributor

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit f0638f8

@cdce8p cdce8p merged commit fbbc9e8 into pylint-dev:main Nov 14, 2022
@cdce8p cdce8p deleted the invalid-slice-index branch November 14, 2022 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component False Negative 🦋 No message is emitted but something is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants