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

Add an option to require ignore comments have error codes #11633

Merged

Conversation

PeterJCLaw
Copy link
Contributor

@PeterJCLaw PeterJCLaw commented Nov 28, 2021

Description

Fixes #11509

Pitch # type:ignore comments risk suppressing an unintended error, which is why it's preferred to specify the exact error code you're suppressing

This introduces a new error code ignore-without-code which achieves that. I've gone with naming this warn- rather than disallow- since the ignores are still obeyed, just alerted about (and also as the most closely related options are also warn- style). This is disabled by default but can be controlled via the --enable-error-code machinery.

This new option is aware of --warn-unused-ignores (since they're likely to be used together) such that we don't emit warnings about a lack of error codes if the ignore is unused and that option is turned on.

One thing I'm not sure about is what should happen for ignore comments that apply to a whole file. I've raised this on that issue, so discussion should probably happen there. Currently the new option obeys that instruction to ignore the file.

Test Plan

Some manual testing, plus automated tests added.

Code Review

I'm fairly happy with the code approach here, though I'm not at all sure that I've put the command line option in the right places. I also suspect I've missed some places that would need updating in the docs. Guidance around the proper places to put the docs/CLI arg/etc. is most welcome. (I believe I've now caught everywhere that this needs to go)

@PeterJCLaw PeterJCLaw force-pushed the fix-11509-disallow-ignore-without-code branch from 9ce13fe to a99fd9b Compare November 28, 2021 17:04
@github-actions

This comment has been minimized.

@JelleZijlstra
Copy link
Member

The mypy-primer output here is unexpected, right? The new error is off by default so it shouldn't trigger on existing code.

@PeterJCLaw
Copy link
Contributor Author

PeterJCLaw commented Nov 29, 2021

The mypy-primer output here is unexpected, right? The new error is off by default so it shouldn't trigger on existing code.

Indeed, I'm not sure what's causing that. When I run mypy from this branch by default I'm not seeing the option take effect.

Separately, I have noticed that if I run mypy from this branch initially without the new option and then run it with the new option the errors aren't found when they should be:

$ cat foo.py
foo  # type: ignore
$ mypy foo.py
Success: no issues found in 1 source file
$ mypy foo.py --disallow-ignore-without-code
Success: no issues found in 1 source file

Changing the test file seems to get the error to show up, so I'm wondering if there's some caching that I've missed?

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Nov 29, 2021

re: primer, I think you might have automatically included it in --strict (see the strict_group stuff)

@PeterJCLaw
Copy link
Contributor Author

PeterJCLaw commented Nov 29, 2021

I think you might have automatically included it in --strict (see the strict_group stuff)

Ahhh, yeah. That was actually intentional and I'd forgotten I'd done it. I'm not sure whether this qualifies for --strict (or how that gets decided), but it's something I'd be interested in including in the strict mode.

Perhaps it would be better to introduce this flag first and then later move it into --strict?
(If --warn-unused-ignores isn't in --strict then adding it at the same time might also make sense)

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Nov 29, 2021

Yeah, I think we should introduce this first and only later consider moving it into --strict, since this has a pretty large impact on users of --strict. I'd also want to make sure that the error codes mypy provides for common errors are useful before doing so to reduce future breakage; I think currently we have a lot of error code "misc".

My approximate mental model for --strict is "you shouldn't ever have type errors without an explicit circumvention somewhere". Or "this is what the default behaviour of mypy should be if all Python were typed".
If today was the first day of mypy I'd be more willing to include it; in our current world I lean towards avoiding breaking unsuspecting users since it's just an adjustment of something that's already explicitly opting in to type unsafety.

--warn-unused-ignores is already part of --strict, but --show-error-codes is not (but should be, if this is incorporated into --strict). Speaking of which, if possible, it would be really nice to show which error code(s) to add in your error message.

@PeterJCLaw
Copy link
Contributor Author

Yeah, I think we should introduce this first and only later consider moving it into --strict, since this has a pretty large impact on users of --strict. I'd also want to make sure that the error codes mypy provides for common errors are useful before doing so to reduce future breakage; I think currently we have a lot of error code "misc".

Ah, fair. I've personally found that while a lot of things were misc, most things now aren't (:+1: to whoever was pushing that a while back). I can definitely see the desire to get misc even lower before requiring codes though 👍

... if possible, it would be really nice to show which error code(s) to add in your error message.

Good point, I think it should be easy to add that.

Also make it non-strict for now. It may become part of --strict
in the future, though work may be needed to reduce the number of
'misc' errors before then.
Stylistically matching --warn-unused-ignores' docs.
@github-actions

This comment has been minimized.

This also includes it in the OPTIONS_AFFECTING_CACHE, which fixes
the caching issue I'd been seeing.
"Disallow" didn't feel right here since mypy was still obeying the
ignore comment. "Warn" is more precise since this is configuring
a new warning to be emitted.

This also adjusts the pluralisation in a way I think is better.
@PeterJCLaw
Copy link
Contributor Author

Separately, I have noticed that if I run mypy from this branch initially without the new option and then run it with the new option the errors aren't found when they should be

I believe I've fixed this in c811aa7.

@PeterJCLaw PeterJCLaw marked this pull request as ready for review December 2, 2021 21:39
@JukkaL
Copy link
Collaborator

JukkaL commented Dec 3, 2021

I think this would be better enabled via --enable-error-code <something>, instead of adding a new command-line option. The motivation is to avoid continuously increasing the size and complexity of the command-line interface.

(Some of the existing flags would work better using --enable-error-code, but they were added before --enable-error-code was available and maintaining backward compatibility is useful.)

@PeterJCLaw PeterJCLaw force-pushed the fix-11509-disallow-ignore-without-code branch from 82cb7ab to f113c4a Compare December 3, 2021 19:05
@PeterJCLaw PeterJCLaw force-pushed the fix-11509-disallow-ignore-without-code branch from e082301 to 732c92a Compare December 18, 2021 13:15
In preference to adding another specific command line flag.
Users who turn ignore-without-code on are likely at first to
encounter cases where their existing ignores are too broad.
The previous hint spelling was useful, however could encourage
users to just ignore everything which was already ignored rather
than actually reviewing the code. This change removes the implication
that the correct course is to blindly update the ignore comment
while still containing the useful information about what is ignored.
@PeterJCLaw PeterJCLaw force-pushed the fix-11509-disallow-ignore-without-code branch from 732c92a to 6f39906 Compare December 18, 2021 13:18
It's been moved to a pure error code style option.
@PeterJCLaw
Copy link
Contributor Author

@JukkaL thanks, I've changed over to this being an error code (still disabled by default).

@JelleZijlstra
Copy link
Member

I can handle getting this through, since Jukka and Shantanu already commented here without objections to the overall idea. If you fix the conflict, I'll do another round of review, then merge if everything looks good.

@PeterJCLaw
Copy link
Contributor Author

Thanks! I'll fix that conflict now 👍

@JelleZijlstra JelleZijlstra self-requested a review February 8, 2022 22:12
@github-actions

This comment has been minimized.

@JelleZijlstra
Copy link
Member

Looks like a test needs to be updated.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Some minor feedback on the docs

docs/source/error_code_list2.rst Outdated Show resolved Hide resolved
docs/source/error_code_list2.rst Outdated Show resolved Hide resolved
@NeilGirdhar
Copy link
Contributor

Just curious, what happens if you have a # type: ignore[pyright] or some other code that doesn't exist?

This appears to have originated in python#12067
which was picked up by 61e9589.

TODO: Work out if we should minimise the number of errors that users
end up with in this scenario.
@PeterJCLaw
Copy link
Contributor Author

Just curious, what happens if you have a # type: ignore[pyright] or some other code that doesn't exist?

Currently nothing specific from this new error code. --warn-unused-ignores does pick it up though.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thank you!

@PeterJCLaw
Copy link
Contributor Author

One thing I have spotted here is that it's now possible to have several errors from the same problem, as shown by the testErrorCodeMissingDoesntTrampleUnusedIgnoresWarning test:

# flags: --enable-error-code ignore-without-code --warn-unused-ignores
z # type: ignore[ignore-without-code]

generates three messages:

  • Error: Unused "type: ignore" comment
  • Error: Name "z" is not defined [name-defined]
  • Note: Error code "name-defined" not covered by "type: ignore" comment

While I'm not sure if this is something created by this PR, is this something we want to address here?

@github-actions

This comment has been minimized.

@JelleZijlstra
Copy link
Member

While I'm not sure if this is something created by this PR, is this something we want to address here?

I'd rather cover that in a separate PR. Maybe we should suppress the Unused "type: ignore" comment error?

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2022

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

@JelleZijlstra JelleZijlstra merged commit 84696ce into python:master Feb 9, 2022
@DanielNoord
Copy link

Just want to express my gratitude to @PeterJCLaw for writing and @JelleZijlstra for reviewing this PR. We have been waiting patiently for this function, nice to see it got merged 🎉 Thanks guys!

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.

option to enforce error codes in type: ignore comments
6 participants