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

Complexity calculation is unnecessarily high for chained method calls #22

Open
mwgamble opened this issue Apr 19, 2022 · 0 comments
Open
Labels
bug Something isn't working

Comments

@mwgamble
Copy link

Describe the bug

We have a builder class that we'll instantiate and get a result from all in a single statement. This works well because most of the time we only need to set a few things on the builder, and we don't want the builder to live on once the result has been obtained.

These expressions do not look complex. There is nothing nested. However, these statements fail on ECE001.

To Reproduce

Put this code in a file named test.py:

class MyClass:
    def __init__(self):
        self.prop1 = None
        self.prop2 = None
        self.prop3 = None

    def method1(self, prop1):
        self.prop1 = prop1
        return self

    def method2(self, prop2):
        self.prop2 = prop2
        return self

    def method3(self, prop3):
        self.prop3 = prop3
        return self

    def build(self):
        return {
            "prop1": self.prop1,
            "prop2": self.prop2,
            "prop3": self.prop3,
        }

x = MyClass().method1("a").method2("b").method3("c").build()
print(x)

Install flake8-expression-complexity.

Put this config in a file named setup.cfg:

[flake8]
select =
    # Regular flake8 rules
    C, E, F, W
    # flake8-expression-complexity
    ECE

Run flake8 test.py in the directory containing these files. Observe the following output:

test.py:27:1: ECE001 Expression is too complex (7.5 > 7)

Also observe that flake8 exits with a non-zero exit status code.

Expected behavior

Running flake8 test.py results in no errors and an exit status of 0.

Additional context

Basically it isn't clear why these chained method calls are considered "complex".

@mwgamble mwgamble added the bug Something isn't working label Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant