8000 Support hyphens in error codes · Issue #1568 · PyCQA/flake8 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support hyphens in error codes #1568

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

Closed
jarshwah opened this issue Mar 15, 2022 · 4 comments
Closed

Support hyphens in error codes #1568

jarshwah opened this issue Mar 15, 2022 · 4 comments

Comments

@jarshwah
Copy link

describe the request

I'd like to be able to use hyphens in error codes so that I can have readable error codes that are name-spaced. For example, here's a list of some error codes we're currently using:

COMPANY-UNIT-TESTS-FILEPATH
COMPANY-FUNCTIONAL-TESTS-FILEPATH
COMPANY-PRINT-STATEMENTS
COMPANY-MOCK-ASSERT-METHODS

These codes work fine with noqa directives. The only place they don't seem to work is in flake8 configuration files (such as setup.cfg). That is, the following config file crashes flake8:

[flake8]
per-file-ignores =
    tests/test_flake8_plugin.py:COMPANY-UNIT-TESTS-FILEPATH

With the error:

There was a critical error during execution of Flake8:
Expected `per-file-ignores` to be a mapping from file exclude patterns to ignore codes.

Configured `per-file-ignores` setting:

    tests/test_flake8_plugin.py:COMPANY-UNIT-TESTS-FILEPATH

I believe it comes down to this line:

(re.compile(r"[A-Z]+[0-9]*(?=$|\s|,)"), _CODE),

The fix could be a simple update of the regex. I will put a patch together shortly to demonstrate a potential fix.

@jarshwah
Copy link
Author

It sounds like this feature request is rejected, so rather than continue the discussion on the PR I thought I'd try to plead my case here. I'd like to understand the reasons for rejecting this, and wanted to dig in a little deeper to the reasons I think it's a good idea.

I don't think HYPHENATED-CODES should replace SRT123 codes but there's room for both to exist.

(I also understand that there's no obligation for the maintainers to continue this conversation, and if that's the case, so be it, no worries).

Firstly, the rest of flake8 and the ecosystem of plugins already works without incident with HYPHENATED-CODES. There is code out there in the wild producing error codes that don't match the form SRT123. I could only find the config parser with the original constraints (and a pycodestyle regex that enforces A123 type codes).

Second, ignoring rules using short-codes impairs readability, as you usually have to go search for what a code means or add comments into the config file to understand what they're used for. Eg
image

Similarly, adding NOQA lines with HYPHENATED-CODES can read a bit nicer:

print(summary) # noqa: NS-PRINT-STATEMENTS

Coming up with unique SHR123 codes can also be a bit of a hassle. You need to make sure you're not clashing with existing prefixes that other plugins have defined. Then you need some kind of numbering scheme so that similar rules are within the same range.

Finally, supporting arbitrary length codes with hyphens doesn't seem to cause any issues either.

I hope you can reconsider, thanks

@asottile
Copy link
Member

this isn't the first time this has come up. I encourage you to search the tracker and find the duplicates where this has been decided

@jarshwah
Copy link
Author

Thanks for the pointer (I did a cursory search prior but searching for code and regex wasn't the easiest search I've ever had to perform).

#325 seems to be one of the most relevant issues. It seems to boil down to being able to differentiate between codes and files, and playing nice with other tooling. Fair enough, cheers!

@asottile
Copy link
Member

fwiw, you generally want extend-ignore and not ignore -- it'd cut half of your ignore list

stephenfin added a commit to stephenfin/flake8-logging-format that referenced this issue Jun 15, 2022
flake8 has always expected codes to use formats like 'E123' or 'W451'.
More complex codes like 'COMPANY-UNIT-TESTS-FILEPATH' have traditionally
worked everywhere *except* in flake8 configuration files, and attempts
to change this have been rejected [1]. The upcoming version of flake8
now enforced this practice everywhere, which is resulting in the
following error message when attempting to use 'flake8-logging-format'
with master of 'flake8' [2]:

  There was a critical error during execution of Flake8:
  plugin code for `flake8-logging-format[logging-format]` does not match
  ^[A-Z]{1,3}[0-9]{0,3}$

The resolution here is change our code to something that matches this
pattern. We use 'LF' since it should be fairly unique and _somewhat_
descriptive.

[1] PyCQA/flake8#1568
[2] PyCQA/flake8#325

Signed-off-by: Stephen Finucane <stephen@that.guru>
stephenfin added a commit to stephenfin/flake8-logging-format that referenced this issue Jun 15, 2022
flake8 has always expected codes to use formats like 'E123' or 'W451'.
More complex codes like 'COMPANY-UNIT-TESTS-FILEPATH' have traditionally
worked everywhere *except* in flake8 configuration files, and attempts
to change this have been rejected [1]. The upcoming version of flake8
now enforced this practice everywhere, which is resulting in the
following error message when attempting to use 'flake8-logging-format'
with master of 'flake8' [2]:

  There was a critical error during execution of Flake8:
  plugin code for `flake8-logging-format[logging-format]` does not match
  ^[A-Z]{1,3}[0-9]{0,3}$

The resolution here is change our code to something that matches this
pattern. We use 'G' as this matches the codes provided by this plugin.

[1] PyCQA/flake8#1568
[2] PyCQA/flake8#325

Signed-off-by: Stephen Finucane <stephen@that.guru>
sethisernhagen pushed a commit to globality-corp/flake8-logging-format that referenced this issue Aug 5, 2022
flake8 has always expected codes to use formats like 'E123' or 'W451'.
More complex codes like 'COMPANY-UNIT-TESTS-FILEPATH' have traditionally
worked everywhere *except* in flake8 configuration files, and attempts
to change this have been rejected [1]. The upcoming version of flake8
now enforced this practice everywhere, which is resulting in the
following error message when attempting to use 'flake8-logging-format'
with master of 'flake8' [2]:

  There was a critical error during execution of Flake8:
  plugin code for `flake8-logging-format[logging-format]` does not match
  ^[A-Z]{1,3}[0-9]{0,3}$

The resolution here is change our code to something that matches this
pattern. We use 'G' as this matches the codes provided by this plugin.

[1] PyCQA/flake8#1568
[2] PyCQA/flake8#325

Signed-off-by: Stephen Finucane <stephen@that.guru>
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

No branches or pull requests

2 participants
0