Skip to content

Conversation

DanielNoord
Copy link
Collaborator

  • Write a good description on what the PR does.
  • Add an entry to the change log describing the change in
    doc/whatsnew/2/2.15/index.rst (or doc/whatsnew/2/2.14/full.rst
    if the change needs backporting in 2.14). If necessary you can write
    details or offer examples on how the new change is supposed to work.
  • If you used multiple emails or multiple names when contributing, add your mails
    and preferred name in script/.contributors_aliases.json

Type of Changes

Type
✨ New feature

Description

Closes #4725.

@DanielNoord DanielNoord added the Enhancement ✨ Improvement to a component label Jun 20, 2022
@coveralls
Copy link

coveralls commented Jun 20, 2022

Pull Request Test Coverage Report for Build 2534764659

  • 12 of 12 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 95.288%

Totals Coverage Status
Change from base Build 2529265553: 0.003%
Covered Lines: 16604
Relevant Lines: 17425

💛 - Coveralls

@github-actions

This comment has been minimized.

@DanielNoord
Copy link
Collaborator Author

I checked some the primer changes and they seem fine.

One issue is the second example in astroid. If the StopIteration is caught in the function that is calling the function then you don't really need a default. But I guess we consider that a bad pattern and you should just re-raise? I feel like we have other checks that also rely on this logic.

@Pierre-Sassoulas
Copy link
Member

This one (using next() without default value in __next__) seems like a false positive: https://github.com/pandas-dev/pandas/blob/main/pandas/io/parsers/python_parser.py#L1256

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.

Related PEP that I think we should talk about : https://peps.python.org/pep-0479/

I think we need to check the scope of the routine where the next is used to verify that the StopIteration is caught (except if it's a function that is supposed to raise StopIteration like __next__). We also need to check that py-version is > 3.7. Raìsing a RuntimeError implicitly is bad, doing anything else explicitly after catching the StopIterationis okay.

@DanielNoord
Copy link
Collaborator Author

DanielNoord commented Jun 21, 2022

This one (using next() without default value in __next__) seems like a false positive: https://github.com/pandas-dev/pandas/blob/main/pandas/io/parsers/python_parser.py#L1256

I don't think __next__ ever has a default value, see https://docs.python.org/3/library/stdtypes.html#iterator.__next__.

Edit: Never mind. You meant that raising StopIteration within __next__ is fine. Agreed.

@DanielNoord
Copy link
Collaborator Author

Related PEP that I think we should talk about : https://peps.python.org/pep-0479/

👍

I think we need to check the scope of the routine where the next is used to verify that the StopIteration is caught (except if it's a function that is supposed to raise StopIteration like __next__). We also need to check that py-version is > 3.7. Raìsing a RuntimeError implicitly is bad, doing anything else explicitly after catching the StopIterationis okay.

Shouldn't that be a different check? As this is a Warning and RuntimeError probably warrants an Error level?

@github-actions

This comment has been minimized.

Co-authored-by: Pierre Sassoulas <[email protected]>
@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.

There's still a lot of false positive in most of the primer repository. I'm wondering if we should:

  • infer the value of the generator so we do not raise if it's obvious it will always be possible to call next on it (Using next directly is reasonable to if the generator is defined in the same scope ?)
  • Put this in an extension, currently it's a default check and seeing the number of warning in working libraries it's a little too naive or opinionated right now

@DanielNoord
Copy link
Collaborator Author

  • infer the value of the generator so we do not raise if it's obvious it will always be possible to call next on it (Using next directly is reasonable to if the generator is defined in the same scope ?)

I'm going to think a bit about this and see if I can come up with something. On the other hand, isn't this also what a linter is for: catching code that might work now, but is not always safe in the future? Stuff like unspecified-encoding can also be redundant if you're only running your code on Linux.

@Pierre-Sassoulas
Copy link
Member

For some thing we also assume the user knows what he does, i.e. we rejected a check for dict access without using get (#6901). It can raise a KeyError if the key does not exists. To be fair, the implementer did not want to check that KeyError were caught, but to forbid direct access entirely, which admittedly is a lot more opinionated than what we're doing here with next. But this feel like the same type of check and we did not create the unguarded-direct-dict-access check (maybe because it's not what the original implementer wanted and it would be a valuable check if someone want to implement it).

@DanielNoord
Copy link
Collaborator Author

For some thing we also assume the user knows what he does, i.e. we rejected a check for dict access without using get (#6901). It can raise a KeyError if the key does not exists. To be fair, the implementer did not want to check that KeyError were caught, but to forbid direct access entirely, which admittedly is a lot more opinionated than what we're doing here with next. But this feel like the same type of check and we did not create the unguarded-direct-dict-access check (maybe because it's not what the original implementer wanted and it would be a valuable check if someone want to implement it).

Yeah, I'm just wondering what options users have to signal that they know the risk of StopIteration and are willing to "take it".

  1. One is obviously guarding with an except.
  2. Another is guarding in all calling functions > We would raise here due to limitations in astroid and this not actually being safe
  3. ...

Using a variable that's defined in the same scope seems like it should also be one, but I don't think astroid allows us to make sure that that variable won't be exhausted.

@Pierre-Sassoulas
Copy link
Member

3 ...

I'd say this one could be "not activating the extensions that check this" or "disabling the default check". I think if we want to minimize false positives against false negatives the former is better.

Using a variable that's defined in the same scope seems like it should also be one, but I don't think astroid allows us to make sure that that variable won't be exhausted.

Can we infer the generator length and compare against the number of call to next ?

@DanielNoord
Copy link
Collaborator Author

@jacobtylerwalls Do you have an opinion about this? I'm still not sure whether I think this should be turned on by default. Take for example the following hit on sentry:
https://github.com/getsentry/sentry/blob/master/src/sentry/db/postgres/exceptions.py#L29

This seems fine: there is an __iter__ method and there is even an assertion that tb_list is an iterable. However, if that tb_list was an empty iterable __iter__ would still raise StopIteration. Of course this is something that the developer probably knows about, but it is also a potential source of errors: thus cause for a linting warning.
I think the biggest issue with this message is that people will often struggle what to define as default, so that's why they don't do so. But just putting None there shows that you have at least thought about the potential cause of issues.

@jacobtylerwalls jacobtylerwalls changed the title Add unspecified-default-for-next Add unguarded-next-without-default Jul 3, 2022
@jacobtylerwalls
Copy link
Member

Let's block this on an astroid Constraint PR that adds a not-empty constraint. That would deal with most of the false positives, right? Then, if there are still code paths that make an empty generator reachable by an unguarded next, that's fine to warn about, even if they're unlikely paths in working libraries. People will just disable the whole check if it's too picky, but I think they'll see the value for others and won't complain :D

@github-actions

This comment has been minimized.

@DanielNoord DanielNoord added this to the 2.16.0 milestone Jan 9, 2023
@Pierre-Sassoulas Pierre-Sassoulas removed Blocked 🚧 Blocked by a particular issue Needs astroid update Needs an astroid update (probably a release too) before being mergable labels Jan 9, 2023
@Pierre-Sassoulas Pierre-Sassoulas removed this from the 2.16.0 milestone Jan 9, 2023
@github-actions

This comment has been minimized.

@codecov
Copy link

codecov bot commented Jan 9, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.43%. Comparing base (0ff2cb5) to head (da4074e).
Report is 1124 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #6987   +/-   ##
=======================================
  Coverage   95.43%   95.43%           
=======================================
  Files         176      176           
  Lines       18545    18557   +12     
=======================================
+ Hits        17698    17710   +12     
  Misses        847      847           
Files with missing lines Coverage Δ
pylint/checkers/stdlib.py 96.83% <100.00%> (+0.13%) ⬆️

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

🤖 Effect of this PR on checked open source code: 🤖

Effect on astroid:
The following messages are now emitted:

  1. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/PyCQA/astroid/blob/9eb8c47ddb6b48e14dbdb87bb1b02fcb580cb20d/astroid/objects.py#L291
  2. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/PyCQA/astroid/blob/9eb8c47ddb6b48e14dbdb87bb1b02fcb580cb20d/astroid/brain/brain_pathlib.py#L41

Effect on black:
The following messages are now emitted:

  1. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/black/lines.py#L439
  2. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/black/linegen.py#L298
  3. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L122
  4. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L124
  5. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L128
  6. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L138
  7. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L143
  8. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L146
  9. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L153
  10. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L161
  11. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L163
  12. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L172
  13. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L182
  14. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L193
  15. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L199
  16. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L204
  17. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L214
  18. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L219
  19. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L221
  20. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L226
  21. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L228
  22. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L233
  23. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/conv.py#L239
  24. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/driver.py#L85
  25. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/pgen.py#L363
  26. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/pgen.py#L365
  27. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/psf/black/blob/4bee9cca5553c55493203822b5a112ec5216bc74/src/blib2to3/pgen2/pgen.py#L397

Effect on django:
The following messages are now emitted:

  1. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/views/debug.py#L553
  2. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/templatetags/i18n.py#L549
  3. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/db/backends/oracle/base.py#L565
  4. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/db/models/options.py#L317
  5. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/db/models/base.py#L579
  6. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/db/models/sql/query.py#L265
  7. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/db/models/sql/query.py#L527
  8. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/db/models/sql/query.py#L709
  9. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/db/models/sql/query.py#L742
  10. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/db/models/sql/query.py#L2276
  11. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/contrib/gis/geos/point.py#L71
  12. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/contrib/gis/geos/point.py#L72
  13. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/contrib/gis/geos/point.py#L74
  14. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/template/defaultfilters.py#L697
  15. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/template/defaulttags.py#L103
  16. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/template/base.py#L334
  17. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/utils/regex_helper.py#L209
  18. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/utils/regex_helper.py#L259
  19. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/forms/utils.py#L167
  20. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/django/django/blob/648005dee62481acc1784e5c9625e90f0fd6aab4/django/forms/utils.py#L194

Effect on flask:
The following messages are now emitted:

  1. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pallets/flask/blob/9da947a279d5ed5958609e0b6ec690160c496480/src/flask/scaffold.py#L825
  2. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pallets/flask/blob/9da947a279d5ed5958609e0b6ec690160c496480/src/flask/helpers.py#L159
  3. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pallets/flask/blob/9da947a279d5ed5958609e0b6ec690160c496480/src/flask/json/tag.py#L104
  4. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pallets/flask/blob/9da947a279d5ed5958609e0b6ec690160c496480/src/flask/json/tag.py#L108
  5. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pallets/flask/blob/9da947a279d5ed5958609e0b6ec690160c496480/src/flask/json/tag.py#L112
  6. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pallets/flask/blob/9da947a279d5ed5958609e0b6ec690160c496480/src/flask/json/tag.py#L299

Effect on music21:
The following messages are now emitted:

  1. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/cuthbertLab/music21/blob/323523633d9e35cee96c825fa1b216333ef38351/music21/romanText/tsvConverter.py#L559
  2. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/cuthbertLab/music21/blob/323523633d9e35cee96c825fa1b216333ef38351/music21/romanText/tsvConverter.py#L780
  3. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/cuthbertLab/music21/blob/323523633d9e35cee96c825fa1b216333ef38351/music21/romanText/translate.py#L1677
  4. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/cuthbertLab/music21/blob/323523633d9e35cee96c825fa1b216333ef38351/music21/meter/tools.py#L266
  5. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/cuthbertLab/music21/blob/323523633d9e35cee96c825fa1b216333ef38351/music21/stream/iterator.py#L2132
  6. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/cuthbertLab/music21/blob/323523633d9e35cee96c825fa1b216333ef38351/music21/stream/iterator.py#L2137
  7. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/cuthbertLab/music21/blob/323523633d9e35cee96c825fa1b216333ef38351/music21/stream/iterator.py#L2142
  8. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/cuthbertLab/music21/blob/323523633d9e35cee96c825fa1b216333ef38351/music21/stream/iterator.py#L2182

Effect on pandas:
The following messages are now emitted:

  1. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/io/pytables.py#L1354
  2. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/io/xml.py#L318
  3. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/io/xml.py#L501
  4. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/io/xml.py#L607
  5. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/io/xml.py#L744
  6. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/io/formats/xml.py#L218
  7. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/io/formats/printing.py#L120
  8. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/io/parsers/readers.py#L1690
  9. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/io/parsers/python_parser.py#L731
  10. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/io/parsers/python_parser.py#L798
  11. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/groupby/transform/test_transform.py#L1168
  12. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/test_sql.py#L142
  13. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/test_sql.py#L157
  14. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/test_common.py#L176
  15. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/test_common.py#L431
  16. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/test_common.py#L435
  17. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/pytables/test_read.py#L228
  18. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/xml/test_xml.py#L416
  19. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/sas/test_sas7bdat.py#L398
  20. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/parser/common/test_file_buffer_url.py#L385
  21. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/parser/common/test_file_buffer_url.py#L401
  22. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/io/parser/common/test_chunksize.py#L216
  23. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/frame/test_iteration.py#L84
  24. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/frame/test_iteration.py#L113
  25. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/frame/test_iteration.py#L119
  26. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/frame/test_iteration.py#L125
  27. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/frame/test_iteration.py#L131
  28. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/frame/test_iteration.py#L136
  29. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/arrays/test_datetimes.py#L131
  30. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/window/test_base_indexer.py#L478
  31. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/window/test_base_indexer.py#L500
  32. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/plotting/test_common.py#L34
  33. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/plotting/test_common.py#L37
  34. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/indexing/test_scalar.py#L51
  35. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/tests/extension/test_arrow.py#L1005
  36. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/core/apply.py#L355
  37. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/core/base.py#L345
  38. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/core/reshape/merge.py#L2470
  39. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/core/groupby/generic.py#L306
  40. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/core/groupby/generic.py#L343
  41. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/core/groupby/groupby.py#L684
  42. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/core/computation/parsing.py#L127
  43. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/core/indexes/api.py#L373
  44. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/core/strings/accessor.py#L3278
  45. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/plotting/_matplotlib/core.py#L523
  46. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pandas-dev/pandas/blob/954cf55938c50e34ddeced408d04bd04651eb96a/pandas/plotting/_matplotlib/core.py#L920

Effect on pytest:
The following messages are now emitted:

  1. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pytest-dev/pytest/blob/7421f3bb94df80ff2d131e932223b190f9b6d7b6/src/_pytest/mark/expression.py#L74
  2. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pytest-dev/pytest/blob/7421f3bb94df80ff2d131e932223b190f9b6d7b6/src/_pytest/mark/expression.py#L111
  3. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pytest-dev/pytest/blob/7421f3bb94df80ff2d131e932223b190f9b6d7b6/src/_pytest/assertion/rewrite.py#L748
  4. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pytest-dev/pytest/blob/7421f3bb94df80ff2d131e932223b190f9b6d7b6/src/_pytest/assertion/rewrite.py#L781
  5. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pytest-dev/pytest/blob/7421f3bb94df80ff2d131e932223b190f9b6d7b6/src/_pytest/assertion/rewrite.py#L812
  6. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/pytest-dev/pytest/blob/7421f3bb94df80ff2d131e932223b190f9b6d7b6/src/_pytest/_code/source.py#L203

Effect on sentry:
The following messages are now emitted:

  1. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/db/postgres/exceptions.py#L29
  2. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/db/models/manager/base.py#L271
  3. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/tsdb/redis.py#L617
  4. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/tsdb/redis.py#L896
  5. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/tsdb/redis.py#L910
  6. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/digests/utils.py#L55
  7. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/utils/cursors.py#L160
  8. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/utils/cursors.py#L225
  9. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/utils/cursors.py#L229
  10. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/management/commands/send_fake_data.py#L30
  11. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/management/commands/send_fake_data.py#L32
  12. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/management/commands/send_fake_data.py#L35
  13. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/api/serializers/rest_framework/rule.py#L58
  14. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/api/endpoints/project_grouping_configs.py#L24
  15. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/api/endpoints/project_dynamic_sampling.py#L359
  16. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/api/endpoints/project_release_stats.py#L20
  17. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/lang/javascript/processor.py#L369
  18. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/ratelimits/sliding_windows.py#L415
  19. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/ratelimits/cardinality.py#L418
  20. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/web/frontend/debug/mail.py#L166
  21. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/web/frontend/debug/mail.py#L366
  22. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/web/frontend/debug/mail.py#L488
  23. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/grouping/strategies/utils.py#L30
  24. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/getsentry/sentry/blob/859f4dfc78c360075c6e2c2a557271b3f431a08b/src/sentry/interfaces/exception.py#L181

Effect on coverage:
The following messages are now emitted:

  1. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/nedbat/coveragepy/blob/b059a67fd1fe5d514f7c283f5ab99052e1cea15f/coverage/misc.py#L300
  2. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/nedbat/coveragepy/blob/b059a67fd1fe5d514f7c283f5ab99052e1cea15f/coverage/debug.py#L428
  3. unguarded-next-without-default:
    Using next without explicitly specifying a default value or catching the StopIteration
    https://github.com/nedbat/coveragepy/blob/b059a67fd1fe5d514f7c283f5ab99052e1cea15f/coverage/debug.py#L443

This comment was generated for commit da4074e

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.

Wow, this has been stale for a long time. 😄 Let's put this little wonder in 2.17 !

@@ -0,0 +1 @@
next(i for i in (1, 2) if isinstance(i, str)) # [unguarded-next-without-default]
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
next(i for i in (1, 2) if isinstance(i, str)) # [unguarded-next-without-default]
def display(animals):
iterator = iter(animals)
while True:
print(next(iterator)) # [unguarded-next-without-default]

@@ -0,0 +1 @@
next((i for i in (1, 2) if isinstance(i, str)), None)
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
next((i for i in (1, 2) if isinstance(i, str)), None)
def display(animals):
iterator = iter(animals)
while True:
next_animal = next(iterator, None)
if next_animal is None:
break
print(next_animal)

"unguarded-next-without-default",
"Without a default value calls to next() can raise a StopIteration "
"exception. This exception should be caught or a default value should "
"be provided.",
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
"be provided.",
"be provided, unless you're in a function that is expected to raise ``StopIteration``.",

},
),
"W1519": (
"Using next without explicitly specifying a default value or catching the StopIteration",
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
"Using next without explicitly specifying a default value or catching the StopIteration",
"Using next without specifying a default value or catching the StopIteration",

There's no implicit default, it's going to raise a StopIteration if you don't set the default right ?

"W1519": (
"Using next without explicitly specifying a default value or catching the StopIteration",
"unguarded-next-without-default",
"Without a default value calls to next() can raise a StopIteration "
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
"Without a default value calls to next() can raise a StopIteration "
"Without a default value calls to next() will raise a ``StopIteration`` "

"Using next without explicitly specifying a default value or catching the StopIteration",
"unguarded-next-without-default",
"Without a default value calls to next() can raise a StopIteration "
"exception. This exception should be caught or a default value should "
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
"exception. This exception should be caught or a default value should "
"exception when there's nothing to iterate anymore. It's generally not a trivial task to prove that it's possible to call next safely so this exception should be caught or a default value should "

Comment on lines +35 to +37
class MyClass:
def __next__(self):
return next(i for i in (1, 2))
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas Feb 5, 2023

Choose a reason for hiding this comment

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

Suggested change
class MyClass:
def __next__(self):
return next(i for i in (1, 2))
class MyClass:
def __next__(self):
return next(i for i in (1, 2))
def expected_to_raise_stop_iteration(self):
"""Custom iterator function that will raise StopIteration by design.
Raises:
StopIteration: If the end of the sequence has been reached.
"""
return next(i for i in (1, 2))

Just a thought. Maybe we don't need to check that it's a proper docstring in numpy style or whatever. If there's StopIteration in the docstring we do not raise.

@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Feb 5, 2023

I thought we identified a class of false positives we could address with a not-empty constraint in astroid?

@Pierre-Sassoulas
Copy link
Member

Astroid inferring capability with none check has been improved in 2.13. there's still a lot of warning in the primers, I did not check how many less compared to before. Do we want to check what is inside the iterator to raise less and more accurately ? (I.e. is this a errors message or a convention/warning message ?)

@jacobtylerwalls
Copy link
Member

jacobtylerwalls commented Feb 5, 2023

Sorry for not fleshing out further. I was suggesting blocking this PR on a yet to be written astroid PR that would build on the last constraint PR (is None) by adding a new constraint (has length) (or even truthy?), and that way this would no longer be flagged in this PR:

bunch = []
if bunch:
    next(bunch)  # just doing this once, and we just checked not-empty

Also, I think this PR should also special case the infinite iterator used here: https://github.com/nedbat/coveragepy/blob/b059a67fd1fe5d514f7c283f5ab99052e1cea15f/coverage/debug.py#L414

@jacobtylerwalls
Copy link
Member

Maybe it can be done inside pylint only, without another astroid PR, I don't know.

@DanielNoord
Copy link
Collaborator Author

This would require some work in astroid. I'll draft this again!

@DanielNoord DanielNoord marked this pull request as draft February 5, 2023 19:25
@Pierre-Sassoulas Pierre-Sassoulas added Blocked 🚧 Blocked by a particular issue Work in progress labels Apr 3, 2023
@DanielNoord
Copy link
Collaborator Author

Going to close this. I never had time to look into this further and the amount of false positives without changes to astroid is way too high.

@DanielNoord DanielNoord closed this Jun 3, 2024
@DanielNoord DanielNoord deleted the next branch June 3, 2024 20:13
@Pierre-Sassoulas
Copy link
Member

It's a pity, that would be a nice check :( But yeah, if you use next without default you probably thought about what you're doing thus a lot of false positives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocked 🚧 Blocked by a particular issue Enhancement ✨ Improvement to a component Work in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Checker for unguarded-next-called-without-default for next called without catching StopIteration and without default value in python 3.7+
4 participants