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

Simplify suppression of spurious 'unused-ignore's #15419

Closed
wants to merge 1 commit into from

Conversation

ikonst
Copy link
Contributor

@ikonst ikonst commented Jun 12, 2023

In #15164 we've made it so that # type: ignores in "unreachable" code are not flagged as unused.

Here I'm removing the unreachable_lines attribute (and related setters). It appears that the same effect can be achieved by pruning the lines from the existing ignored_lines dict.

This change also has the advantage (IMO) of directly spelling out what we're doing here. In contrast, when assigning to unreachable_lines one has to follow the code to determine what it's being used for. And yes, it's more open-ended to keep "unreachable_lines" but then (a) it's not so simple because not all "unreachabilities" are the same, (b) unused open-end is unwarranted complexity.

Motivation

While looking at #12409, it became clear to me that the term "unreachable" became somewhat overloaded:

  • code that cannot be soundly reached
    if False:
      a = 42  # E: Statement is unreachable
    if True:
      raise Exception
      a = 42  # E: Statement is unreachable
    assert False
    a = 42  # E: Statement is unreachable
  • code unreachable on current version/platform
    if sys.version_info < (3,):
       a = 42
    if sys.platform == 'foobar':
       a = 42
    assert sys.platform == 'foobar'
    a = 42

IMO it would be helpful to disambiguate and I suggest we stop referring to the latter as "reachability".

@ikonst
Copy link
Contributor Author

ikonst commented Jun 12, 2023

@ilevkivskyi since you authored #15164

@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

FWIW I am not a fan of this idea: in general I try to avoid modifying parsed part of AST unless really necessary, and ignored_lines comes almost directly from parser.

Another thing, I suspect this may break daemon mode, and --warn-unused-ignores is already quite fragile/tricky in daemon mode.

@ikonst
Copy link
Contributor Author

ikonst commented Jun 12, 2023

Wouldn't del file.defs[i + 1 :] similarly modify parsed AST?

(Also, WDYT about at the very least renaming unreachable_lines to skipped_lines? Trying to disambiguate the concept of "reachability".)

@ikonst
Copy link
Contributor Author

ikonst commented Jun 14, 2023

^ @ilevkivskyi

@ilevkivskyi
Copy link
Member

Wouldn't del file.defs[i + 1 :] similarly modify parsed AST?

Yes, and I think it is a bad idea, but maybe it was needed for some reason (like an alternative would be even worse).

WDYT about at the very least renaming unreachable_lines to skipped_lines

Yes, this is a good idea.

@ikonst
Copy link
Contributor Author

ikonst commented Jun 21, 2023

Closed in favor of #15483.

@ikonst ikonst closed this Jun 21, 2023
@ikonst ikonst deleted the rename-reachability branch June 21, 2023 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants