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

Overriding built-in default ignores through an extension #284

Closed
asottile opened this issue Apr 3, 2021 · 12 comments · Fixed by #1576
Closed

Overriding built-in default ignores through an extension #284

asottile opened this issue Apr 3, 2021 · 12 comments · Fixed by #1576
Milestone

Comments

@asottile
Copy link
Member

asottile commented Apr 3, 2021

In GitLab by @snoack on Jan 10, 2019, 14:17

We'd like to centralize our flake8 configuration through an extension, rather than duplicating the error codes to be selected/ignored in each of our project's tox.ini. It's already possible to add default ignores and selects through an extension:

    @classmethod
    def add_options(cls, parser):
        parser.extend_default_ignore(['D1'])
        parser.extend_default_select(['E226'])

However, if I select an error code that flake8 ignores by default, the built-in default ignore takes precedence. There is also an remove_default_ignore() method but it only removes ignores that have been preiosly added by this plugin, it doesn't have any effect on the built-in default ignores either.

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @snoack on Jan 10, 2019, 15:09

changed title from Overriding built-in default ignores though an extension to Overriding built-in default ignores th{+r+}ough an extension

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @snoack on Jan 10, 2019, 15:12

changed the description

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @sigmavirus24 on Jan 11, 2019, 04:06

To be brief (because I don't have oodles of time right now to respond, frankly) this isn't what these were designed for. We'd probably want an entirely separate set of APIs for a "plugin" that defines a style guide which we don't have today.

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @warsaw on Mar 11, 2019, 15:52

I have a similar use case. I want to add a new code in a new version of my plugin, but I would like to suppress it by default. Only if a user calls flake8 with --select=U999 (let's say 999 is my new code), would you get this error reported. I believe this issue covers the fact that this is not currently possible.

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @warsaw on Mar 11, 2019, 15:59

mentioned in merge request warsaw/flufl.flake8!1

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Mar 11, 2019, 22:30

One way to do that with the current facilities is to separate the "optional" things into a separate "plugin" (can be in the same distribution)

For instance:

[options.entry_points]
flake8.extension =
    U1 = foo.MainPlugin
    U9 = foo.ExperimentalPlugin

And then in your plugin set

class ExperimentalPlugin:
    # ...
    off_by_default = True

Then users can opt into this set of optional codes with

--enable-extension=U9

It ~works (we're using this strategy at work), I think we'd have a better solution with the --extend-select proposed in #365 (similar to already-implemented --extend-ignore)

@sco1
Copy link

sco1 commented Jun 24, 2022

Question on this via PyCQA/flake8-bugbear#266, am I correct that the following should now yield an ignored-by-default error code (B950 in this case) on the main branch? flake8-bugbear uses extend-default-ignore to add the opinionated codes.

$ pip install flake8-bugbear
$ pip install --force git+https://github.com/pycqa/flake8@main
$ flake8 --version
4.0.1 (flake8-bugbear: 22.6.22, mccabe: 0.7.0, pycodestyle: 2.8.0, pyflakes: 2.4.0) CPython 3.10.2
on Windows
$ echo "foo = 'foo'" > bug.py
$ flake8 --extend-ignore=E501 --extend-select=B950 --max-line-length=9 bug.py

Verbose output:

$ flake8 -vv --extend-ignore=E501 --extend-select=B950 --max-line-length=9 bug.py
flake8                    MainProcess     69 DEBUG    Added a None logging handler to logger root at flake8
flake8.options.manager    MainProcess    117 DEBUG    Registered option "Option(-q, --quiet, action='count', default=0, help='Report only file names, or nothing. This option is repeatable.')".
flake8.options.manager    MainProcess    117 DEBUG    Registered option "Option(--color, default='auto', choices=('auto', 'always', 'never'), help='Whether to use color in output.  Defaults to `%(default)s`.')".
flake8.options.manager    MainProcess    118 DEBUG    Registered option "Option(--count, action='store_true', help='Print total number of errors to standard output and set the exit code to 1 if total is not empty.')".
flake8.options.manager    MainProcess    118 DEBUG    Registered option "Option(--diff, action='store_true', help='(DEPRECATED) Report changes only within line number ranges in the unified diff provided on standard in by the user.')".
flake8.options.manager    MainProcess    118 DEBUG    Registered option "Option(--exclude, default='.svn,CVS,.bzr,.hg,.git,__pycache__,.tox,.nox,.eggs,*.egg', type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=True), help='Comma-separated list of files or directories to exclude. (Default: %(default)s)', metavar='patterns')".
flake8.options.manager    MainProcess    118 DEBUG    Registered option "Option(--extend-exclude, default='', type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=True), help='Comma-separated list of files or directories to add to the list of excluded ones.', metavar='patterns')".
flake8.options.manager    MainProcess    118 DEBUG    Registered option "Option(--filename, default='*.py', type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=False), help='Only check for filenames matching the patterns in this comma-separated list. (Default: %(default)s)', metavar='patterns')".
flake8.options.manager    MainProcess    118 DEBUG    Registered option "Option(--stdin-display-name, default='stdin', help='The name used when reporting errors from code passed via stdin. This is useful for editors piping the file contents to flake8. (Default: %(default)s)')".
flake8.options.manager    MainProcess    118 DEBUG    Registered option "Option(--format, default='default', help='Format errors according to the chosen formatter.', metavar='format')".
flake8.options.manager    MainProcess    119 DEBUG    Registered option "Option(--hang-closing, action='store_true', help="Hang closing bracket instead of matching indentation of opening bracket's line.")".
flake8.options.manager    MainProcess    119 DEBUG    Registered option "Option(--ignore, type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=False), help='Comma-separated list of error codes to ignore (or skip). For example, ``--ignore=E4,E51,W234``. (Default: E121,E123,E126,E226,E24,E704,W503,W504)', metavar='errors')".
flake8.options.manager    MainProcess    119 DEBUG    Registered option "Option(--extend-ignore, type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=False), help='Comma-separated list of error codes to add to the list of ignored ones. For example, ``--extend-ignore=E4,E51,W234``.', metavar='errors')".
flake8.options.manager    MainProcess    119 DEBUG    Registered option "Option(--per-file-ignores, default='', help='A pairing of filenames and violation codes that defines which violations to ignore in a particular file. The filenames can be specified in a manner similar to the ``--exclude`` option and the violations work similarly to the ``--ignore`` and ``--select`` options.')".
flake8.options.manager    MainProcess    119 DEBUG    Registered option "Option(--max-line-length, default=79, type=<class 'int'>, help='Maximum allowed line length for the entirety of this run. (Default: %(default)s)', metavar='n')".
flake8.options.manager    MainProcess    120 DEBUG    Registered option "Option(--max-doc-length, default=None, type=<class 'int'>, help='Maximum allowed doc line length for the entirety of this run. (Default: %(default)s)', metavar='n')".
flake8.options.manager    MainProcess    120 DEBUG    Registered option "Option(--indent-size, default=4, type=<class 'int'>, help='Number of spaces used for indentation (Default: %(default)s)', metavar='n')".
flake8.options.manager    MainProcess    120 DEBUG    Registered option "Option(--select, type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=False), help='Comma-separated list of error codes to enable. For example, ``--select=E4,E51,W234``. (Default: E,F,W,C90)', metavar='errors')".
flake8.options.manager    MainProcess    120 DEBUG    Registered option "Option(--extend-select, type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=False), help='Comma-separated list of error codes to add to the list of selected ones. For example, ``--extend-select=E4,E51,W234``.', metavar='errors')".
flake8.options.manager    MainProcess    120 DEBUG    Registered option "Option(--disable-noqa, action='store_true', default=False, help='Disable the effect of "# noqa". This will report errors on lines with "# noqa" at the end.')".
flake8.options.manager    MainProcess    120 DEBUG    Registered option "Option(--show-source, action='store_true', help='Show the source generate each error or warning.')".
flake8.options.manager    MainProcess    120 DEBUG    Registered option "Option(--no-show-source, action='store_false', dest='show_source', help='Negate --show-source')".
flake8.options.manager    MainProcess    120 DEBUG    Registered option "Option(--statistics, action='store_true', help='Count errors.')".
flake8.options.manager    MainProcess    121 DEBUG    Registered option "Option(--exit-zero, action='store_true', help='Exit with status code "0" even if there are errors.')".
flake8.options.manager    MainProcess    121 DEBUG    Registered option "Option(-j, --jobs, default='auto', type=<class 'flake8.main.options.JobsArgument'>, help='Number of subprocesses to use to run checks in parallel. This is ignored on Windows. The default, "auto", will auto-detect the number of processors available to use. (Default: %(default)s)')".
flake8.options.manager    MainProcess    121 DEBUG    Registered option "Option(--tee, action='store_true', default=False, help='Write to stdout and output-file.')".
flake8.options.manager    MainProcess    121 DEBUG    Registered option "Option(--benchmark, action='store_true', default=False, help='Print benchmark information about this run of Flake8')".
flake8.options.manager    MainProcess    121 DEBUG    Registered option "Option(--bug-report, action='store_true', help='Print information necessary when preparing a bug report')".
flake8.options.manager    MainProcess    121 DEBUG    Extending default ignore list with ['B901', 'B902', 'B903', 'B904', 'B950']
flake8.options.manager    MainProcess    121 DEBUG    Registered option "Option(--extend-immutable-calls, default=[], type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=False), help='Skip B008 test for additional immutable calls.')".
flake8.options.manager    MainProcess    121 DEBUG    Extending default select list with ['B']
flake8.options.manager    MainProcess    121 DEBUG    Registered option "Option(--max-complexity, action='store', default=-1, type=<class 'int'>, help='McCabe complexity threshold')".
flake8.options.manager    MainProcess    121 DEBUG    Extending default select list with ['C90']
flake8.options.manager    MainProcess    121 DEBUG    Registered option "Option(--builtins, type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=False), help='define more built-ins, comma separated')".
flake8.options.manager    MainProcess    122 DEBUG    Registered option "Option(--doctests, action='store_true', default=False, help='also check syntax of the doctests')".
flake8.options.manager    MainProcess    122 DEBUG    Registered option "Option(--include-in-doctest, default='', type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=True), dest='include_in_doctest', help='Run doctests only on these files')".
flake8.options.manager    MainProcess    122 DEBUG    Registered option "Option(--exclude-from-doctest, default='', type=functools.partial(<function _flake8_normalize at 0x000001D2BA61F520>, comma_separated_list=True, normalize_paths=True), dest='exclude_from_doctest', help='Skip these files when running doctests')".
flake8.options.manager    MainProcess    122 DEBUG    Extending default select list with ['F']
flake8.options.manager    MainProcess    122 DEBUG    Extending default select list with ['E']
flake8.options.manager    MainProcess    122 DEBUG    Extending default select list with ['W']
flake8.checker            MainProcess    122 WARNING  The multiprocessing module is not available. Ignoring --jobs arguments.
flake8.checker            MainProcess    123 INFO     Making checkers
flake8.discover_files     MainProcess    123 DEBUG    "C:\Users\sco1\Documents\Github\testcode\bug.py" has not been excluded
flake8.checker            MainProcess    123 INFO     Checking 1 files
flake8.checker            MainProcess    126 DEBUG    Logical line: "foo = 'xxx'"
flake8.main.application   MainProcess    126 INFO     Finished running
flake8.main.application   MainProcess    127 INFO     Reporting errors
flake8.style_guide        MainProcess    127 DEBUG    The user configured 'E501' to be <Selected.Explicitly: 'explicitly selected'>, <Ignored.Explicitly: 'explicitly ignored'>
flake8.style_guide        MainProcess    127 DEBUG    "E501" will be "Decision.Ignored"
flake8.violation          MainProcess    127 DEBUG    Violation(code='E501', filename='bug.py', line_number=1, column_number=10, text='line too long (11 > 9 characters)', physical_line="foo = 'foo'\n") is not inline ignored
flake8.style_guide        MainProcess    127 DEBUG    The user configured 'B950' to be <Selected.Explicitly: 'explicitly selected'>, <Ignored.Explicitly: 'explicitly ignored'>
flake8.style_guide        MainProcess    127 DEBUG    "B950" will be "Decision.Ignored"
flake8.violation          MainProcess    127 DEBUG    Violation(code='B950', filename='bug.py', line_number=1, column_number=12, text='line too long (11 > 9 characters)', physical_line="foo = 'foo'\n") is not inline ignored
flake8.main.application   MainProcess    127 INFO     Found a total of 2 violations and reported 0

@asottile
Copy link
Member Author

it's a little weird, but by using --extend-ignore that ~slightly opts out of the "am I using the default ignore list" -- I wonder if there should be a middle ground to accommodate for that?

@sco1
Copy link

sco1 commented Jun 24, 2022

Ah I see, if --extend-ignore is omitted then we go from:

flake8.style_guide        MainProcess    127 DEBUG    The user configured 'B950' to be <Selected.Explicitly: 'explicitly selected'>, <Ignored.Explicitly: 'explicitly ignored'>

to:

flake8.style_guide        MainProcess    127 DEBUG    The user configured 'B950' to be <Selected.Explicitly: 'explicitly selected'>, <Ignored.Implicitly: 'implicitly ignored'>

For both paths I think I would be expecting that B950 is implicitly ignored, is such a middle ground feasible?

@asottile
Copy link
Member Author

yeah there's probably a better middle ground than assuming any time --extend-ignore happens that the default values are intended to be ignored. let me see if I can make that work ~slightly better

@asottile
Copy link
Member Author

@sco1 should work now after #1609 -- try it out and let me know!

@sco1
Copy link

sco1 commented Jun 25, 2022

lgtm, thanks for the help :)

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 a pull request may close this issue.

2 participants