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

cEP-0005: Improve bear and aspect design #28

Merged
merged 1 commit into from
Nov 18, 2016
Merged

cEP-0005: Improve bear and aspect design #28

merged 1 commit into from
Nov 18, 2016

Conversation

sils
Copy link
Member

@sils sils commented Nov 10, 2016

CC @userzimmermann for review

@gitmate-bot
Copy link

Thanks for your contribution!

Reviewing pull requests take really a lot of time and we're all volunteers. Please make sure you go through the following check list and complete them all before pinging someone for a review.

As you learn things over your Pull Request please help others on the chat and on PRs to get their stuff right as well!

"""

# This is a leaf of the tree! It has to be well documented and bears will only
# implement leafs. However users may use just Metadata.Shortlog to check for

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leafs -> leaves :p

class CommitMessage:
"""
Your commit message is important documentation associated with your
source code. It can help you to identify bugs (e.g. through

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without to i think

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to identify or identifying, I prefer the first in this case

"""

# This is a leaf of the tree! It has to be well documented and bears will only
# implement leafs. However users may use just Metadata.Shortlog to check for

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma after however :D

# implement leafs. However users may use just Metadata.Shortlog to check for
# groups of aspects. This simplifies browsing aspect documentation:
#
# Wether the user wants to see/configure aspects precisely or roughly, he can

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether*

@Shortlog.subaspect
class ColonExistence:
"""
Some projects force to use colons in the commit message shortlog (first

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

force you* makes more sense

@@ -102,18 +102,25 @@ class RedundancyBear(LocalBear):
# This bear is supposed to be able to fix unused imports...
FIX_ASPECTS = {Root.Redundancy.UnusedImport}
# ... and detect code clones.
DETECT_ASPECTS = {Root.Redundancy.CloneRedundancy}
DETECT_ASPECTS = {Root.Redundancy.Clone}

# Aspects are passed as parameter
def run(self, filename, file, aspects):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be easier to use a generalized run() from Bear base class which gets **aspect_settings and then just iterates over FIX_ASPECTS and DETECT_ASPECTS (and WHATEVER_ASPECTS), instantiates each selected one with the aspect_settings, and runs registered bear-specific checker functions for every aspect, as commented below the run() method. This would also avoid any .active checks and running code and generating results for unselected aspects.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see below, I like the idea, that would be an own cEP IMO and can be implemented orthogonally to this, so we can do it later.


# The aspect is tied to the result. coala now knows everything from
# the aspect documentation!
yield Result.from_values(aspect=Root.Redundancy.Clone, ...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bear base class could provide some .aspect_checker decorator:

    @classmethod
    def aspect_checker(cls, aspectcls):
        def checker_deco(func):
            cls.ASPECT_CHECKERS[aspectcls] = func
            return func
        return checker_deco

Which is then used like:

@RedundancyBear.aspect_checker(Root.Redundancy.Clone)
def clone_checker(bear, filename, file, aspect):
    min_clone_tokens = aspect.min_clone_tokens
    return ResultValues(name='value', ...)  # Result.from_values args except aspect=

And a generalized run() method could then work like:

    def run(self, *args, **aspect_settings):
        for aspectcls in self.FIX_ASPECTS.union(self.DETECT_ASPECTS):
            if not selected(aspectcls):
                continue
            checker_func = self.ASPECT_CHECKERS.get(aspectcls)
            if checker_func is None:
                continue
            aspect = aspectcls(**aspect_settings)
            result_values = checker_func(self, *args, aspect=aspect)
            yield Result.from_values(aspect=aspectcls, **result_values)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@...aspect_checker functions could of course also yield several ResultValues()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still some problems exist with that approach, like when several aspects depend on a common logic, which should only be processed once. Or a missing proper run() argspec in derived bears.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if checker_func is None: in run() above should of course raise NotImplementedError instead of just continue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in the usual case this won't be possible easily because of shared logic, we might however include that possibility in the bear base class at a later point. This is however nothing that changes the main design so I wouldn't consider it yet especially as it probably comes with a performance penalty at least in most possible usecases (due to shared code)

@userzimmermann
Copy link
Member

Forgot to submit my review from Friday :D

if unused_imports(file):
# A bear may yield results for any aspect even if it's not selected.
# coala will filter out only selected aspects.
yield Result.from_values(aspect=Root.Redundancy.UnusedImprt, ...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UnusedImp**_o**_rt?

# Check the code ...
if unused_imports(file):
# A bear may yield results for any aspect even if it's not selected.
# coala will filter out only selected aspects.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should trigger a warning in this case^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm not sure any more...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, the bear may or may not use the aspects to perf optimize, it may just yield everything. Convenience feature esp. useful for linter bears


# Check the code ...
if unused_imports(file):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not valid code, missing pass or some other meaningful code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's valid, there's a yield coming

@Makman2
Copy link
Member

Makman2 commented Nov 18, 2016

ack 07696f7

@Makman2
Copy link
Member

Makman2 commented Nov 18, 2016

reack ed58ca3

@userzimmermann
Copy link
Member

ack ed58ca3

@userzimmermann
Copy link
Member

@rultor merge

@rultor
Copy link

rultor commented Nov 18, 2016

@rultor merge

@userzimmermann OK, I'll try to merge now. You can check the progress of the merge here

@rultor rultor merged commit ed58ca3 into master Nov 18, 2016
@rultor
Copy link

rultor commented Nov 18, 2016

@rultor merge

@userzimmermann Done! FYI, the full log is here (took me 1min)

@sils sils deleted the sils/cep5 branch November 18, 2016 23:45
@jayvdb
Copy link
Member

jayvdb commented Nov 18, 2016

Not an objection, (and I havent even started looking at the details of aspects) ... just a comment in case it is relevant ...

Bears are expected to use all of the settings they get via the aspects.

I believe there are going to be many cases where a linter bear needs to listen to an aspect, in order to reject configuration problems. e.g. coala/coala-bears#897

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

7 participants