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

Section Tags Filter #5690

Merged
merged 3 commits into from Aug 10, 2018
Merged

Section Tags Filter #5690

merged 3 commits into from Aug 10, 2018

Conversation

ksdme
Copy link
Member

@ksdme ksdme commented Aug 5, 2018

PTAL cEP-0028

@@ -177,6 +177,10 @@ def default_arg_parser(formatter_class=None):
'-d', '--bear-dirs', type=PathArg, nargs='+', metavar='DIR',
help='additional directories which may contain bears')

inputs_group.add_argument(
Copy link
Member

Choose a reason for hiding this comment

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

This belongs before --bear-dirs , probably after --files .

Lets wait for more feedback on position

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently,

Inputs:
  -b, --bears           names of bears to use
  -f, --files           files that should be checked
  -i, --ignore          files that should be ignored
  --limit-files         filter the `--files` argument's matches further
  -d, --bear-dirs       additional directories which may contain bears
  -t, --tags            tags of sections to execute

I think tags should definitely be above bear-dirs.

:param filter_args:
Arguments of the filter to be passed in.
For example:
``['tags', ('java',)]``
Copy link
Member

Choose a reason for hiding this comment

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

please use exampes from your cEP, and your own real needs.

this looks like language , not a tag which should be an arbitrary grouping which couldnt be achieved via any other means.

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 used them as examples, but I will update it.

:param filters:
List of args based on ``sections`` has to be filtered.
For example:
``[['tags', ('save', 'change')], ['exclude', ('css', '!python')]]``
Copy link
Member

Choose a reason for hiding this comment

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

again, dont use tags that are a language . it is confusing.

@@ -198,12 +198,16 @@ def test_merge_defaults(self):
gather_configuration(lambda *args: True,
self.log_printer,
arg_list=['-S', 'value=1', 'test.value=2',
'-c', escape(temporary, '\\')] +
'--tags', 'save', '-c',
Copy link
Member

Choose a reason for hiding this comment

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

move the '-c', back to the next line, so you dont modify that line.

it is also confusing to have the '-c', on this line when its arg is on the next line.

@@ -198,12 +198,16 @@ def test_merge_defaults(self):
gather_configuration(lambda *args: True,
self.log_printer,
arg_list=['-S', 'value=1', 'test.value=2',
'-c', escape(temporary, '\\')] +
'-c', escape(temporary, '\\'),
'--tags', 'save',] +
Copy link
Collaborator

Choose a reason for hiding this comment

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

The code does not comply to PEP8.

Origin: PEP8Bear, Section: autopep8.

The issue can be fixed by applying the following patch:

--- a/tmp/tmpbnwihg5y/tests/settings/ConfigurationGatheringTest.py
+++ b/tmp/tmpbnwihg5y/tests/settings/ConfigurationGatheringTest.py
@@ -199,7 +199,7 @@
                                      self.log_printer,
                                      arg_list=['-S', 'value=1', 'test.value=2',
                                                '-c', escape(temporary, '\\'),
-                                               '--tags', 'save',] +
+                                               '--tags', 'save', ] +
                                      self.min_args))
 
         self.assertEqual(sections['cli'],

@@ -198,12 +198,16 @@ def test_merge_defaults(self):
gather_configuration(lambda *args: True,
self.log_printer,
arg_list=['-S', 'value=1', 'test.value=2',
'-c', escape(temporary, '\\')] +
'-c', escape(temporary, '\\'),
'--tags', 'save',] +
Copy link
Collaborator

Choose a reason for hiding this comment

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

E231 missing whitespace after ','

Origin: PycodestyleBear (E231), Section: autopep8.



def is_valid_filter(filter):
return filter in available_filters


def is_valid_section_filter(filter):
Copy link
Member

Choose a reason for hiding this comment

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

nope.

lets improve the filters system to support sections.

def foo_filter(thing, args):

every filter than checks whether is knows what is in thing, e.g. using isinstance , and raises NotImplementedError if it doesnt get it. Existing filters only know about Bear.

You make a small chance to the existing filters to make them compliant with the new API by adding a raise NotImplementedError to them.

(Actually most of them understand could easily be improved to understand Section, but that is beyond the scope of your change, and each will need to be carefully done wrt the CLI args to ensure we build a good user experience)

if not filter_args or len(filter_args) == 0:
return all_sections

filter_function = available_section_filters[filter_name]
Copy link
Member

Choose a reason for hiding this comment

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

not needed. below you would catch NotImplementedError and ignore it.

:return: ``True`` if this section matches the criteria inside args,
``False`` otherwise.
"""
if section.name == 'cli':
Copy link
Member

Choose a reason for hiding this comment

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

I agree with @shreyans800755 that this doesnt belong in the filter.
ksdme@f8fc69c#r29964403

The cli section should be kept hidden inside coalib/settings, as a special case implementation detail. It annoys me that we have any UI messages that mention this section -- the user cant see it, so they shouldnt hear about it.

And likewise developers in other parts of coalib shouldnt need to deal with that section.

@@ -159,6 +161,15 @@ def run_coala(console_printer=None,
(section_name, sections[section_name])
for section_name in targets)

# Collect all the section filters which are based on args,
Copy link
Member

Choose a reason for hiding this comment

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

I think we could remove the last ,

@ksdme
Copy link
Member Author

ksdme commented Aug 6, 2018

Updated to use --filter-by (can be used by doing coala --filter-by section_tags save) for both section filters and bear filters. Known issues:

  • Requires update to --filter-by documentation.

"""
things = bears
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's call it items rather than things

List of filters in standard filter format, i.e
``[['filter_name', 'arg1', 'arg2']]``.
"""
if args is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have any practical case for this condition ?
If so, is there way we can check this before calling this function ?

Copy link
Member Author

Choose a reason for hiding this comment

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

run_coala

:param arg_parser:        Instance of ArgParser that is used to non-setting arguments.
:param arg_list:              The CLI argument list.
:param args:                   Alternative pre-parsed CLI arguments.

This is not just about the tests, since args to run_coala() can be optionally passed instead of arg_list, I think we need to merge opts from both, so as to enable the internal usage of this filter. Tests also use can use this by directly calling run_coala() with an arg list.

List of filters in standard filter format, i.e
``[['filter_name', 'arg1', 'arg2']]``.
"""
if args is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Because this is not the job of this function. This function is just a getter

Copy link
Member

Choose a reason for hiding this comment

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

It looks like a helper for the tests , so should be moved into the tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

run_coala

:param arg_parser:        Instance of ArgParser that is used to non-setting arguments.
:param arg_list:              The CLI argument list.
:param args:                   Alternative pre-parsed CLI arguments.

This is not just about the tests, since args to run_coala() can be optionally passed instead of arg_list, I think we need to merge opts from both, so as to enable the internal usage of this filter. Tests also use can use this by directly calling run_coala() with an arg list.

jayvdb
jayvdb previously requested changes Aug 6, 2018
@@ -49,22 +50,83 @@ def apply_filter(filter_name, filter_args, all_bears=None):
return local_bears, global_bears


def apply_filters(filters, bears=None):
def _apply_section_filter(filter_name, filter_args, all_sections):
Copy link
Member

Choose a reason for hiding this comment

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

move this after the apply_filters function so that the diff better aligns, and probably at the end as it is a private function

List of filters in standard filter format, i.e
``[['filter_name', 'arg1', 'arg2']]``.
"""
if args is None:
Copy link
Member

Choose a reason for hiding this comment

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

It looks like a helper for the tests , so should be moved into the tests.

from functools import wraps


def typed_filter(type_classes, msg=None):
Copy link
Member

Choose a reason for hiding this comment

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

(almost) very generic ... belongs in coala_utils::decorators.py

and this is enforce_duck_type or enforce_duct_tape ? ;-)

and to make calling generic, use

@typed_filter(bear='bearclass')
def language_filter(bear, args):
```

Copy link
Member

Choose a reason for hiding this comment

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

belongs in coala_utils::decorators.py

Unfortunately not ...

Because we are not allowed to do imports here! argh.

It was built: https://gitlab.com/coala/coala-utils/merge_requests/82

def decorator(filter):

@wraps(filter)
def decorated_filter(thing, *args, **kargs):
Copy link
Member

Choose a reason for hiding this comment

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

thing -> obj or object_

break
else:
raise NotImplementedError(
msg or '\'{filter}\' filter can only handle {type_name}. '
Copy link
Member

Choose a reason for hiding this comment

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

it was going so well until you mentioned filter here.

the thing you are wrapping has a name, which you are using here.

This decorator is a developer helper ... to catch when they have done the wrong thing. it shouldnt give pretty names ... the caller needs to detect this and make it user-friendly.

Why should a NotImplementedError say "language_filter filter" .. ? language_filter is better, and the backtrace is even better, so no need to tidy it up.

To help, create a subclass of NotImplementedError and stuff into it extra attributes to record what went wrong, such as the callee, expected classes, and the incorrect object which was encountered. Then whoever is catching it can understand what went wrong and create a user-friendly message.



@typed_filter(('bearclass', 'Bear', 'Section'))
def tag_section_filter(section_or_bear, args):
Copy link
Member

Choose a reason for hiding this comment

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

section_tag_filter

@ksdme ksdme changed the title Tag Section Filters Section Tags Filter Aug 7, 2018
@@ -5,9 +5,11 @@
from .LanguageFilter import language_filter
from .CanDetectFilter import can_detect_filter
from .CanFixFilter import can_fix_filter
from .SectionTagFilter import section_tag_filter
Copy link
Collaborator

Choose a reason for hiding this comment

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

This file contains unused source code.

Origin: PyUnusedCodeBear, Section: flakes.

The issue can be fixed by applying the following patch:

--- a/tmp/tmp9_9zjivn/coalib/parsing/filters/__init__.py
+++ b/tmp/tmp9_9zjivn/coalib/parsing/filters/__init__.py
@@ -5,7 +5,6 @@
 from .LanguageFilter import language_filter
 from .CanDetectFilter import can_detect_filter
 from .CanFixFilter import can_fix_filter
-from .SectionTagFilter import section_tag_filter
 
 
 available_filters = {'language': language_filter,

@ksdme ksdme force-pushed the tag-filters-gsoc branch 2 times, most recently from 726414c to 93efd22 Compare August 7, 2018 12:06
@jayvdb jayvdb dismissed their stale review August 7, 2018 13:00

mostly done; no objections

Section filtering based on configurations is now
possible. This commit also adds
- Typed filters, filters can be decorated to
  specifically support certain types of things.
- Supports section filtering.
The new API requires filters to be type annotated
to support for proper usage resolution, this commit
updates the filters with that requirement.
SectionTagsFilter can be used with --filter-by to filter
sections that are to be executed based on their tags
property.
@jayvdb
Copy link
Member

jayvdb commented Aug 10, 2018

ack 3818164 ce6513e ba2c2f7

@gitmate-bot gitmate-bot added process/approved The PR is approved and will be merged soon and removed process/pending review labels Aug 10, 2018
@jayvdb
Copy link
Member

jayvdb commented Aug 10, 2018

@gitmate-bot ff

@gitmate-bot
Copy link
Collaborator

Hey! I'm GitMate.io! This pull request is being fastforwarded automatically. Please DO NOT push while fastforward is in progress or your changes would be lost permanently ⚠️

@gitmate-bot gitmate-bot removed the process/approved The PR is approved and will be merged soon label Aug 10, 2018
@gitmate-bot
Copy link
Collaborator

Automated fastforward with GitMate.io was successful! 🎉

@gitmate-bot gitmate-bot merged commit ba2c2f7 into coala:master Aug 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

None yet

5 participants