ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Implement `flake8-functions`

Open ngnpope opened this issue 2 years ago • 7 comments

GitHub, PyPI.

  • [ ] CFQ001: Function ... has length {function_length} that exceeds max allowed length {max_function_length}
    • This is an alternative to complexity checkers and is just purely the number of lines in the function
    • Unlike too-many-statements (PLR0915) from pylint, this counts the literal number of lines that the function takes up on the page, rather than the number of statements in the function.
  • [x] CFQ002: Function ... has {arguments_amount} arguments that exceeds max allowed {max_parameters_amount}
    • Already implemented as too-many-arguments (PLR0913) from pylint
  • [ ] CFQ003: Function ... is not pure
    • Uses mr-proper under the hood to check that a function is "pure"
    • That library says it's very experimental and it looks a bit too magic, so can probably skip this rule.
  • [x] CFQ004: Function ... has {returns_amount} returns that exceeds max allowed {max_returns_amount}
    • Already implemented as too-many-return-statements (PLR0911) from pylint

Configuration options:

  • [ ] max-function-length implemented as pylint:max-statements
  • [x] max-parameters-amount implemented as pylint:max-args
  • [x] max-returns-amount to be implemented as pylint:max-returns

Other than CFQ003 which is dubious, these will all be covered by pylint rules... So this can be closed if desired, but this will be useful to refer to when aliases are implemented.

ngnpope avatar Jan 31 '23 20:01 ngnpope

CFQ003 would be truly awesome to have

sbrugman avatar Jan 31 '23 22:01 sbrugman

Sounds like "challenge accepted"? 😂

ngnpope avatar Jan 31 '23 23:01 ngnpope

The features of Mr. Proper should be useable attributes to discover violations:

  • that function has no blacklisted calls (like print) and blacklisted attributes access (like smth.count);
  • that function not uses global objects (only local vars and function arguments);
  • that function has al least one return;
  • that function not mutates it's arguments;
  • that function has no local imports;
  • that function has no arguments of forbidden types (like ORM objects);
  • that function not uses self, class or super;
  • that function has calls of only pure functions.

Mostly this requires no type checking!

sbrugman avatar Jan 31 '23 23:01 sbrugman

You mind if I implement too-many-return-statements?

chanman3388 avatar Feb 03 '23 19:02 chanman3388

Go ahead! 🙂

ngnpope avatar Feb 03 '23 19:02 ngnpope

Why CFQ001 and CFQ004 is done? PLR0915/PLR0911 is not equal rule for CFQ001/CFQ004!

mirecl avatar May 15 '23 07:05 mirecl

Why CFQ001 and CFQ004 is done? PLR0915/PLR0911 is not equal rule for CFQ001/CFQ004!

I've unticked CFQ001 -- you're right, that does seem distinct from PLR0915 to me. CFQ004 does look basically the same as PLR0911 to me, though -- would you mind explaining to me how they are different, @mirecl?

(N.B. Just because I've "unticked" the rule doesn't necessarily mean that we'll accept a PR. CFQ001 is a very opinionated rule, so we probably wouldn't accept this rule until https://github.com/astral-sh/ruff/issues/1774 is completed and we have a better way of marking rules as disabled by default.)

AlexWaygood avatar Apr 09 '24 10:04 AlexWaygood