ruff是一个使用rust编写的非常快的python linter。他有如下特性:
- 比现有的linter快10-100倍
- 可通过pip安装
- 支持pyproject.toml
- 兼容python 3.11
- 内置缓存,防止重新分析未修改文件
- 支持自动修正错误
- 支持500条以上规则
- 与内置的flake8规则集对等
- 重新实现了数十个flake8插件逻辑
- vs code等编辑器支持
- 支持分层和级联配置
Ruff的目标是比现有工具快及各个数量级,同时集成更多通用规则。
Ruff可以用来替换Flake8(加上数十个插件),isort,pydocstyle,yesqa,eradicate,pyupgrade和autoflake。
使用gui配置工具
使用gui配置工具,可以让配置ruff规则更简单。
在凌鲨(linksaas)的微应用里面可以找到ruff配置工具,快速对ruff进行配置。
凌鲨(linksaas)是以开源方式运作的,您可以从jihulab.com/linksaas 访问源代码。也可以从 linksaas.pro 下载预编译的版本。
附录
规则列表
- F401 unused-import
- F402 import-shadowed-by-loop-var
- F403 undefined-local-with-import-star
- F404 late-future-import
- F405 undefined-local-with-import-star-usage
- F406 undefined-local-with-nested-import-star-usage
- F407 future-feature-not-defined
- F501 percent-format-invalid-format
- F502 percent-format-expected-mapping
- F503 percent-format-expected-sequence
- F504 percent-format-extra-named-arguments
- F505 percent-format-missing-argument
- F506 percent-format-mixed-positional-and-named
- F507 percent-format-positional-count-mismatch
- F508 percent-format-star-requires-sequence
- F509 percent-format-unsupported-format-character
- F521 string-dot-format-invalid-format
- F522 string-dot-format-extra-named-arguments
- F523 string-dot-format-extra-positional-arguments
- F524 string-dot-format-missing-arguments
- F525 string-dot-format-mixing-automatic
- F541 f-string-missing-placeholders
- F601 multi-value-repeated-key-literal
- F602 multi-value-repeated-key-variable
- F621 expressions-in-star-assignment
- F622 multiple-starred-expressions
- F631 assert-tuple
- F632 is-literal
- F633 invalid-print-syntax
- F634 if-tuple
- F701 break-outside-loop
- F702 continue-outside-loop
- F704 yield-outside-function
- F706 return-outside-function
- F707 default-except-not-last
- F722 forward-annotation-syntax-error
- F811 redefined-while-unused
- F821 undefined-name
- F822 undefined-export
- F823 undefined-local
- F841 unused-variable
- F842 unused-annotation
- F901 raise-not-implemented
- E101 mixed-spaces-and-tabs
- E401 multiple-imports-on-one-line
- E402 module-import-not-at-top-of-file
- E501 line-too-long
- E701 multiple-statements-on-one-line-colon
- E702 multiple-statements-on-one-line-semicolon
- E703 useless-semicolon
- E711 none-comparison
- E712 true-false-comparison
- E713 not-in-test
- E714 not-is-test
- E721 type-comparison
- E722 bare-except
- E731 lambda-assignment
- E741 ambiguous-variable-name
- E742 ambiguous-class-name
- E743 ambiguous-function-name
- E902 io-error
- E999 syntax-error
- W191 tab-indentation
- W291 trailing-whitespace
- W292 missing-newline-at-end-of-file
- W293 blank-line-with-whitespace
- W505 doc-line-too-long
- W605 invalid-escape-sequence
- C901 complex-structure
- I001 unsorted-imports
- I002 missing-required-import
- N801 invalid-class-name
- N802 invalid-function-name
- N803 invalid-argument-name
- N804 invalid-first-argument-name-for-class-method
- N805 invalid-first-argument-name-for-method
- N806 non-lowercase-variable-in-function
- N807 dunder-function-name
- N811 constant-imported-as-non-constant
- N812 lowercase-imported-as-non-lowercase
- N813 camelcase-imported-as-lowercase
- N814 camelcase-imported-as-constant
- N815 mixed-case-variable-in-class-scope
- N816 mixed-case-variable-in-global-scope
- N817 camelcase-imported-as-acronym
- N818 error-suffix-on-exception-name
- N999 invalid-module-name
- D100 undocumented-public-module
- D101 undocumented-public-class
- D102 undocumented-public-method
- D103 undocumented-public-function
- D104 undocumented-public-package
- D105 undocumented-magic-method
- D106 undocumented-public-nested-class
- D107 undocumented-public-init
- D200 fits-on-one-line
- D201 no-blank-line-before-function
- D202 no-blank-line-after-function
- D203 one-blank-line-before-class
- D204 one-blank-line-after-class
- D205 blank-line-after-summary
- D206 indent-with-spaces
- D207 under-indentation
- D208 over-indentation
- D209 new-line-after-last-paragraph
- D210 surrounding-whitespace
- D211 blank-line-before-class
- D212 multi-line-summary-first-line
- D213 multi-line-summary-second-line
- D214 section-not-over-indented
- D215 section-underline-not-over-indented
- D300 triple-single-quotes
- D301 escape-sequence-in-docstring
- D400 ends-in-period
- D401 non-imperative-mood
- D402 no-signature
- D403 first-line-capitalized
- D404 docstring-starts-with-this
- D405 capitalize-section-name
- D406 new-line-after-section-name
- D407 dashed-underline-after-section
- D408 section-underline-after-name
- D409 section-underline-matches-section-length
- D410 no-blank-line-after-section
- D411 no-blank-line-before-section
- D412 blank-lines-between-header-and-content
- D413 blank-line-after-last-section
- D414 empty-docstring-section
- D415 ends-in-punctuation
- D416 section-name-ends-in-colon
- D417 undocumented-param
- D418 overload-with-docstring
- D419 empty-docstring
- UP001 useless-metaclass-type
- UP003 type-of-primitive
- UP004 useless-object-inheritance
- UP005 deprecated-unittest-alias
- UP006 non-pep585-annotation
- UP007 non-pep604-annotation
- UP008 super-call-with-parameters
- UP009 utf8-encoding-declaration
- UP010 unnecessary-future-import
- UP011 lru-cache-without-parameters
- UP012 unnecessary-encode-utf8
- UP013 convert-typed-dict-functional-to-class
- UP014 convert-named-tuple-functional-to-class
- UP015 redundant-open-modes
- UP017 datetime-timezone-utc
- UP018 native-literals
- UP019 typing-text-str-alias
- UP020 open-alias
- UP021 replace-universal-newlines
- UP022 replace-stdout-stderr
- UP023 deprecated-c-element-tree
- UP024 os-error-alias
- UP025 unicode-kind-prefix
- UP026 deprecated-mock-import
- UP027 unpacked-list-comprehension
- UP028 yield-in-for-loop
- UP029 unnecessary-builtin-import
- UP030 format-literals
- UP031 printf-string-formatting
- UP032 f-string
- UP033 lru-cache-with-maxsize-none
- UP034 extraneous-parentheses
- UP035 deprecated-import
- UP036 outdated-version-block
- UP037 quoted-annotation
- UP038 non-pep604-isinstance
- YTT101 sys-version-slice3
- YTT102 sys-version2
- YTT103 sys-version-cmp-str3
- YTT201 sys-version-info0-eq3
- YTT202 six-py3
- YTT203 sys-version-info1-cmp-int
- YTT204 sys-version-info-minor-cmp-int
- YTT301 sys-version0
- YTT302 sys-version-cmp-str10
- YTT303 sys-version-slice1
- ANN001 missing-type-function-argument
- ANN002 missing-type-args
- ANN003 missing-type-kwargs
- ANN101 missing-type-self
- ANN102 missing-type-cls
- ANN201 missing-return-type-undocumented-public-function
- ANN202 missing-return-type-private-function
- ANN204 missing-return-type-special-method
- ANN205 missing-return-type-static-method
- ANN206 missing-return-type-class-method
- ANN401 any-type
- S101 assert
- S102 exec-builtin
- S103 bad-file-permissions
- S104 hardcoded-bind-all-interfaces
- S105 hardcoded-password-string
- S106 hardcoded-password-func-arg
- S107 hardcoded-password-default
- S108 hardcoded-temp-file
- S110 try-except-pass
- S112 try-except-continue
- S113 request-without-timeout
- S301 suspicious-pickle-usage
- S302 suspicious-marshal-usage
- S303 suspicious-insecure-hash-usage
- S304 suspicious-insecure-cipher-usage
- S305 suspicious-insecure-cipher-mode-usage
- S306 suspicious-mktemp-usage
- S307 suspicious-eval-usage
- S308 suspicious-mark-safe-usage
- S310 suspicious-url-open-usage
- S311 suspicious-non-cryptographic-random-usage
- S312 suspicious-telnet-usage
- S313 suspicious-xmlc-element-tree-usage
- S314 suspicious-xml-element-tree-usage
- S315 suspicious-xml-expat-reader-usage
- S316 suspicious-xml-expat-builder-usage
- S317 suspicious-xml-sax-usage
- S318 suspicious-xml-mini-dom-usage
- S319 suspicious-xml-pull-dom-usage
- S320 suspicious-xmle-tree-usage
- S321 suspicious-ftp-lib-usage
- S323 suspicious-unverified-context-usage
- S324 hashlib-insecure-hash-function
- S501 request-with-no-cert-validation
- S506 unsafe-yaml-load
- S508 snmp-insecure-version
- S509 snmp-weak-cryptography
- S602 subprocess-popen-with-shell-equals-true
- S603 subprocess-without-shell-equals-true
- S604 call-with-shell-equals-true
- S605 start-process-with-a-shell
- S606 start-process-with-no-shell
- S607 start-process-with-partial-path
- S608 hardcoded-sql-expression
- S612 logging-config-insecure-listen
- S701 jinja2-autoescape-false
- BLE001 blind-except
- FBT001 boolean-positional-arg-in-function-definition
- FBT002 boolean-default-value-in-function-definition
- FBT003 boolean-positional-value-in-function-call
- B002 unary-prefix-increment
- B003 assignment-to-os-environ
- B004 unreliable-callable-check
- B005 strip-with-multi-characters
- B006 mutable-argument-default
- B007 unused-loop-control-variable
- B008 function-call-in-default-argument
- B009 get-attr-with-constant
- B010 set-attr-with-constant
- B011 assert-false
- B012 jump-statement-in-finally
- B013 redundant-tuple-in-exception-handler
- B014 duplicate-handler-exception
- B015 useless-comparison
- B016 cannot-raise-literal
- B017 assert-raises-exception
- B018 useless-expression
- B019 cached-instance-method
- B020 loop-variable-overrides-iterator
- B021 f-string-docstring
- B022 useless-contextlib-suppress
- B023 function-uses-loop-variable
- B024 abstract-base-class-without-abstract-method
- B025 duplicate-try-block-exception
- B026 star-arg-unpacking-after-keyword-arg
- B027 empty-method-without-abstract-decorator
- B028 no-explicit-stacklevel
- B029 except-with-empty-tuple
- B030 except-with-non-exception-classes
- B031 reuse-of-groupby-generator
- B032 unintentional-type-annotation
- B904 raise-without-from-inside-except
- B905 zip-without-explicit-strict
- A001 builtin-variable-shadowing
- A002 builtin-argument-shadowing
- A003 builtin-attribute-shadowing
- COM812 missing-trailing-comma
- COM818 trailing-comma-on-bare-tuple
- COM819 prohibited-trailing-comma
- C400 unnecessary-generator-list
- C401 unnecessary-generator-set
- C402 unnecessary-generator-dict
- C403 unnecessary-list-comprehension-set
- C404 unnecessary-list-comprehension-dict
- C405 unnecessary-literal-set
- C406 unnecessary-literal-dict
- C408 unnecessary-collection-call
- C409 unnecessary-literal-within-tuple-call
- C410 unnecessary-literal-within-list-call
- C411 unnecessary-list-call
- C413 unnecessary-call-around-sorted
- C414 unnecessary-double-cast-or-process
- C415 unnecessary-subscript-reversal
- C416 unnecessary-comprehension
- C417 unnecessary-map
- C418 unnecessary-literal-within-dict-call
- C419 unnecessary-comprehension-any-all
- DTZ001 call-datetime-without-tzinfo
- DTZ002 call-datetime-today
- DTZ003 call-datetime-utcnow
- DTZ004 call-datetime-utcfromtimestamp
- DTZ005 call-datetime-now-without-tzinfo
- DTZ006 call-datetime-fromtimestamp
- DTZ007 call-datetime-strptime-without-zone
- DTZ011 call-date-today
- DTZ012 call-date-fromtimestamp
- T100 debugger
- DJ001 django-nullable-model-string-field
- DJ003 django-locals-in-render-function
- DJ006 django-exclude-with-model-form
- DJ007 django-all-with-model-form
- DJ008 django-model-without-dunder-str
- DJ012 django-unordered-body-content-in-model
- DJ013 django-non-leading-receiver-decorator
- EM101 raw-string-in-exception
- EM102 f-string-in-exception
- EM103 dot-format-in-exception
- EXE001 shebang-not-executable
- EXE002 shebang-missing-executable-file
- EXE003 shebang-missing-python
- EXE004 shebang-leading-whitespace
- EXE005 shebang-not-first-line
- ISC001 single-line-implicit-string-concatenation
- ISC002 multi-line-implicit-string-concatenation
- ISC003 explicit-string-concatenation
- ICN001 unconventional-import-alias
- ICN002 banned-import-alias
- ICN003 banned-import-from
- G001 logging-string-format
- G002 logging-percent-format
- G003 logging-string-concat
- G004 logging-f-string
- G010 logging-warn
- G101 logging-extra-attr-clash
- G201 logging-exc-info
- G202 logging-redundant-exc-info
- INP001 implicit-namespace-package
- PIE790 unnecessary-pass
- PIE794 duplicate-class-field-definition
- PIE796 non-unique-enums
- PIE800 unnecessary-spread
- PIE804 unnecessary-dict-kwargs
- PIE807 reimplemented-list-builtin
- PIE810 multiple-starts-ends-with
- T201 print
- T203 p-print
- PYI001 unprefixed-type-param
- PYI006 bad-version-info-comparison
- PYI007 unrecognized-platform-check
- PYI008 unrecognized-platform-name
- PYI009 pass-statement-stub-body
- PYI010 non-empty-stub-body
- PYI011 typed-argument-default-in-stub
- PYI012 pass-in-class-body
- PYI014 argument-default-in-stub
- PYI015 assignment-default-in-stub
- PYI016 duplicate-union-member
- PYI021 docstring-in-stub
- PYI033 type-comment-in-stub
- PT001 pytest-fixture-incorrect-parentheses-style
- PT002 pytest-fixture-positional-args
- PT003 pytest-extraneous-scope-function
- PT004 pytest-missing-fixture-name-underscore
- PT005 pytest-incorrect-fixture-name-underscore
- PT006 pytest-parametrize-names-wrong-type
- PT007 pytest-parametrize-values-wrong-type
- PT008 pytest-patch-with-lambda
- PT009 pytest-unittest-assertion
- PT010 pytest-raises-without-exception
- PT011 pytest-raises-too-broad
- PT012 pytest-raises-with-multiple-statements
- PT013 pytest-incorrect-pytest-import
- PT015 pytest-assert-always-false
- PT016 pytest-fail-without-message
- PT017 pytest-assert-in-except
- PT018 pytest-composite-assertion
- PT019 pytest-fixture-param-without-value
- PT020 pytest-deprecated-yield-fixture
- PT021 pytest-fixture-finalizer-callback
- PT022 pytest-useless-yield-fixture
- PT023 pytest-incorrect-mark-parentheses-style
- PT024 pytest-unnecessary-asyncio-mark-on-fixture
- PT025 pytest-erroneous-use-fixtures-on-fixture
- PT026 pytest-use-fixtures-without-parameters
- Q000 bad-quotes-inline-string
- Q001 bad-quotes-multiline-string
- Q002 bad-quotes-docstring
- Q003 avoidable-escaped-quote
- RSE102 unnecessary-paren-on-raise-exception
- RET501 unnecessary-return-none
- RET502 implicit-return-value
- RET503 implicit-return
- RET504 unnecessary-assign
- RET505 superfluous-else-return
- RET506 superfluous-else-raise
- RET507 superfluous-else-continue
- RET508 superfluous-else-break
- SLF001 private-member-access
- SIM101 duplicate-isinstance-call
- SIM102 collapsible-if
- SIM103 needless-bool
- SIM105 suppressible-exception
- SIM107 return-in-try-except-finally
- SIM108 if-else-block-instead-of-if-exp
- SIM109 compare-with-tuple
- SIM110 reimplemented-builtin
- SIM112 uncapitalized-environment-variables
- SIM114 if-with-same-arms
- SIM115 open-file-with-context-handler
- SIM116 if-else-block-instead-of-dict-lookup
- SIM117 multiple-with-statements
- SIM118 in-dict-keys
- SIM201 negate-equal-op
- SIM202 negate-not-equal-op
- SIM208 double-negation
- SIM210 if-expr-with-true-false
- SIM211 if-expr-with-false-true
- SIM212 if-expr-with-twisted-arms
- SIM220 expr-and-not-expr
- SIM221 expr-or-not-expr
- SIM222 expr-or-true
- SIM223 expr-and-false
- SIM300 yoda-conditions
- SIM401 if-else-block-instead-of-dict-get
- SIM910 dict-get-with-none-default
- TID251 banned-api
- TID252 relative-imports
- TCH001 typing-only-first-party-import
- TCH002 typing-only-third-party-import
- TCH003 typing-only-standard-library-import
- TCH004 runtime-import-in-type-checking-block
- TCH005 empty-type-checking-block
- INT001 f-string-in-get-text-func-call
- INT002 format-in-get-text-func-call
- INT003 printf-in-get-text-func-call
- ARG001 unused-function-argument
- ARG002 unused-method-argument
- ARG003 unused-class-method-argument
- ARG004 unused-static-method-argument
- ARG005 unused-lambda-argument
- PTH100 os-path-abspath
- PTH101 os-chmod
- PTH102 os-mkdir
- PTH103 os-makedirs
- PTH104 os-rename
- PTH105 pathlib-replace
- PTH106 os-rmdir
- PTH107 os-remove
- PTH108 os-unlink
- PTH109 os-getcwd
- PTH110 os-path-exists
- PTH111 os-path-expanduser
- PTH112 os-path-isdir
- PTH113 os-path-isfile
- PTH114 os-path-islink
- PTH115 os-readlink
- PTH116 os-stat
- PTH117 os-path-isabs
- PTH118 os-path-join
- PTH119 os-path-basename
- PTH120 os-path-dirname
- PTH121 os-path-samefile
- PTH122 os-path-splitext
- PTH123 builtin-open
- PTH124 py-path
- ERA001 commented-out-code
- PD002 pandas-use-of-inplace-argument
- PD003 pandas-use-of-dot-is-null
- PD004 pandas-use-of-dot-not-null
- PD007 pandas-use-of-dot-ix
- PD008 pandas-use-of-dot-at
- PD009 pandas-use-of-dot-iat
- PD010 pandas-use-of-dot-pivot-or-unstack
- PD011 pandas-use-of-dot-values
- PD012 pandas-use-of-dot-read-table
- PD013 pandas-use-of-dot-stack
- PD015 pandas-use-of-pd-merge
- PD901 pandas-df-variable-name
- PGH001 eval
- PGH002 deprecated-log-warn
- PGH003 blanket-type-ignore
- PGH004 blanket-noqa
- PLC0414 useless-import-alias
- PLC1901 compare-to-empty-string
- PLC3002 unnecessary-direct-lambda-call
- PLE0100 yield-in-init
- PLE0101 return-in-init
- PLE0116 continue-in-finally
- PLE0117 nonlocal-without-binding
- PLE0118 load-before-global-declaration
- PLE0302 unexpected-special-method-signature
- PLE0604 invalid-all-object
- PLE0605 invalid-all-format
- PLE1142 await-outside-async
- PLE1205 logging-too-many-args
- PLE1206 logging-too-few-args
- PLE1307 bad-string-format-type
- PLE1310 bad-str-strip-call
- PLE1507 invalid-envvar-value
- PLE2502 bidirectional-unicode
- PLE2510 invalid-character-backspace
- PLE2512 invalid-character-sub
- PLE2513 invalid-character-esc
- PLE2514 invalid-character-nul
- PLE2515 invalid-character-zero-width-space
- PLR0133 comparison-of-constant
- PLR0206 property-with-parameters
- PLR0402 manual-from-import
- PLR0911 too-many-return-statements
- PLR0912 too-many-branches
- PLR0913 too-many-arguments
- PLR0915 too-many-statements
- PLR1701 repeated-isinstance-calls
- PLR1711 useless-return
- PLR1722 sys-exit-alias
- PLR2004 magic-value-comparison
- PLR5501 collapsible-else-if
- PLW0120 useless-else-on-loop
- PLW0129 assert-on-string-literal
- PLW0602 global-variable-not-assigned
- PLW0603 global-statement
- PLW0711 binary-op-exception
- PLW1508 invalid-envvar-default
- PLW2901 redefined-loop-name
- TRY002 raise-vanilla-class
- TRY003 raise-vanilla-args
- TRY004 type-check-without-type-error
- TRY200 reraise-no-cause
- TRY201 verbose-raise
- TRY300 try-consider-else
- TRY301 raise-within-try
- TRY400 error-instead-of-exception
- TRY401 verbose-log-message
- NPY001 numpy-deprecated-type-alias
- NPY002 numpy-legacy-random
- RUF001 ambiguous-unicode-character-string
- RUF002 ambiguous-unicode-character-docstring
- RUF003 ambiguous-unicode-character-comment
- RUF005 collection-literal-concatenation
- RUF006 asyncio-dangling-task
- RUF007 pairwise-over-zipped
- RUF008 mutable-dataclass-default
- RUF009 function-call-in-dataclass-default-argument
- RUF100 unused-noqa