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

aspectModule: Add ``get()`` method #4412

Merged
merged 1 commit into from Jun 26, 2017

Conversation

@adhikasp
Member

adhikasp commented Jun 26, 2017

Create get() function as wrapper of __getitem__ that doesn't
raise error when trying to access non-existant aspect.

Closes #4411

Relates to https://gitlab.com/coala/GSoC-2017/issues/314

coalib/bearlib/aspects/__init__.py Outdated
@@ -54,6 +54,25 @@ def __init__(self, module):
subaspect = getattr(submod, submodname)
setattr(self, submodname, subaspect)
def get(self, aspectname, multi_find=False):
"""
Wrapper for ``__getitem__`` that doesn't raise exception.

This comment has been minimized.

@jayvdb

jayvdb Jun 26, 2017

Member

but, by default multi_find is disabled, so MultipleAspectFoundError exception will be raised.

This comment has been minimized.

@adhikasp

adhikasp Jun 26, 2017

Member

I am not sure whether to make multi_find enabled by default because it will make the result type not consistent (either aspectclass or list of aspectclass)

This comment has been minimized.

@adhikasp

adhikasp Jun 26, 2017

Member

And i think it's better to get an explicit error rather than silently get a list and failing when trying to manipulate it as aspectclass

This comment has been minimized.

@jayvdb

jayvdb Jun 26, 2017

Member

Then add :raises ... and fix this docstring short description to be accurate.

coalib/bearlib/aspects/__init__.py Outdated
"""
Wrapper for ``__getitem__`` that doesn't raise exception.
:aspectname: Name of the aspect that should be searched.

This comment has been minimized.

@jayvdb

jayvdb Jun 26, 2017

Member

:param ...

coalib/bearlib/aspects/__init__.py Outdated
:param aspectname:
Name of the aspect that should be searched.
:multi_find:

This comment has been minimized.

@jayvdb

jayvdb Jun 26, 2017

Member

:param ... sigh!

coalib/bearlib/aspects/__init__.py Outdated
return those matches as list.
:raises MultipleAspectFoundError:
When finding multiple aspect with same name AND `multi_find` is
True.

This comment has been minimized.

@jayvdb

jayvdb Jun 26, 2017

Member

really??

@jayvdb

jayvdb approved these changes Jun 26, 2017

@userzimmermann

@adhikasp Good job! Also the multi_find idea... But remove the latter! I explained below why. Better put that in a separate method later...

coalib/bearlib/aspects/__init__.py Outdated
@@ -54,6 +54,31 @@ def __init__(self, module):
subaspect = getattr(submod, submodname)
setattr(self, submodname, subaspect)
def get(self, aspectname, multi_find=False):

This comment has been minimized.

@userzimmermann

userzimmermann Jun 26, 2017

Member

Remove that multi_find feature. The method shouldn't return either a single aspect or a sequence of aspects based on the same kind of input. That's confusing and needs a check every time what you actually got. Let's maybe implement some .get_all() for that later...

This comment has been minimized.

@adhikasp

adhikasp Jun 26, 2017

Member

What if we did find multiple? Return None or return first find?

This comment has been minimized.

@adhikasp

adhikasp Jun 26, 2017

Member

I return None

This comment has been minimized.

@jayvdb

jayvdb Jun 26, 2017

Member

You still have the parameter here...?

@userzimmermann

And please add Relates to https://gitlab.com/coala/GSoC-2017/issues/314 to your commit message

@userzimmermann

userzimmermann requested changes Jun 26, 2017 edited

Regarding https://github.com/coala/coala/pull/4412/files#r123971923 :

Just let the MultipleAspectFoundError go through. The purpose of .get() is to either get the only one that matches or None

And instead of that proposed .get_all() in https://github.com/coala/coala/pull/4412/files#r123965883 we should then later better implement some .filter() method to iterate over aspects matching certain criteria like:

coala_aspects.filter(name='Length', ...)

or:

coala_aspects.filter(tastes=['max_line_length', ...], ...)

Or use aspectsModule.__call__ for that... Let's see :)

cc @coala/aspects-developers @jayvdb

coalib/bearlib/aspects/__init__.py Outdated
"""
try:
return self[aspectname]
except (AspectNotFoundError, MultipleAspectFoundError):

This comment has been minimized.

@userzimmermann

userzimmermann Jun 26, 2017

Member

Just don't except the MultipleAspectFoundError

@gitmate-bot gitmate-bot added size/M and removed size/S labels Jun 26, 2017

@userzimmermann

And the last one! Promise ;)

coalib/bearlib/aspects/__init__.py Outdated
@@ -54,6 +54,21 @@ def __init__(self, module):
subaspect = getattr(submod, submodname)
setattr(self, submodname, subaspect)
def get(self, aspectname, multi_find=False):
"""
Wrapper for ``__getitem__`` that doesn't raise exception for searching

This comment has been minimized.

@userzimmermann

userzimmermann Jun 26, 2017

Member

Don't name it a wrapper for __getitem__. Not interesting for the user. Just explain what it does, or at least change it to something like Similar to __getitem__, but ...

@userzimmermann

Sorry for my promise before ;) But this is really the last one!

coalib/bearlib/aspects/__init__.py Outdated
:raise MultipleAspectFoundError:
When multiple aspects with same name was found.
:return:
An aspectclass.

This comment has been minimized.

@userzimmermann

userzimmermann Jun 26, 2017

Member

An aspectclass or None

coalib/bearlib/aspects/__init__.py Outdated
@@ -54,6 +54,31 @@ def __init__(self, module):
subaspect = getattr(submod, submodname)
setattr(self, submodname, subaspect)
def get(self, aspectname, multi_find=False):

This comment has been minimized.

@jayvdb

jayvdb Jun 26, 2017

Member

You still have the parameter here...?

@userzimmermann

This is not a review! Since I already did my "last" one before ;P

tests/bearlib/aspects/ModuleTest.py Outdated
self.assertIs(coalib.bearlib.aspects.get(aspectname),
coalib.bearlib.aspects.Root)
def test_getitem_no_match(self):

This comment has been minimized.

@userzimmermann

userzimmermann Jun 26, 2017

Member

test_get_no_match

tests/bearlib/aspects/ModuleTest.py Outdated
for aspectname in ['noaspect', 'NOASPECT', 'Root.aspectsYEAH']:
self.assertIsNone(coalib.bearlib.aspects.get(aspectname))
with self.assertRaisesRegex(

This comment has been minimized.

@userzimmermann

userzimmermann Jun 26, 2017

Member

Create separate def test_get_multi_match(self):

@jayvdb

jayvdb approved these changes Jun 26, 2017

@jayvdb

This comment has been minimized.

Member

jayvdb commented Jun 26, 2017

ack ee529fd

@userzimmermann

Final not-a-review ;P

tests/bearlib/aspects/ModuleTest.py Outdated
for aspectname in ['noaspect', 'NOASPECT', 'Root.aspectsYEAH']:
self.assertIsNone(coalib.bearlib.aspects.get(aspectname))
def test_get_multi_math(self):

This comment has been minimized.

@userzimmermann

userzimmermann Jun 26, 2017

Member

math --> match ;)

This comment has been minimized.

@adhikasp

adhikasp Jun 26, 2017

Member

Aw sorry :(

aspectModule: Add ``get()`` method
Create ``get()`` function as wrapper of ``__getitem__`` that doesn't 
raise error when trying to access non-existant aspect.

Closes #4411
@userzimmermann

This comment has been minimized.

Member

userzimmermann commented Jun 26, 2017

ack 66d0276

@jayvdb

This comment has been minimized.

Member

jayvdb commented Jun 26, 2017

@rultor merge

1 similar comment
@userzimmermann

This comment has been minimized.

Member

userzimmermann commented Jun 26, 2017

@rultor merge

@rultor

This comment has been minimized.

Contributor

rultor commented Jun 26, 2017

@rultor merge

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

@rultor rultor merged commit 66d0276 into coala:master Jun 26, 2017

6 of 9 checks passed

ci/circleci CircleCI is running your tests
Details
continuous-integration/appveyor/branch Waiting for AppVeyor build to complete
Details
continuous-integration/travis-ci/push The Travis CI build is in progress
Details
codecov/project 100% (target 100%)
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
review/gitmate/commit No issues with this one - go ahead! :)
Details
review/gitmate/manual This commit was acknowledged.
Details
review/gitmate/pr All is well! :) (0 problems solved)
Details
@rultor

This comment has been minimized.

Contributor

rultor commented Jun 26, 2017

@rultor merge

@jayvdb Done! FYI, the full log is here (took me 2min)

@rultor

This comment has been minimized.

Contributor

rultor commented Jun 26, 2017

@rultor merge

@adhikasp @userzimmermann The pull request is closed already, so I can't merge it

@rultor

This comment has been minimized.

Contributor

rultor commented Jun 26, 2017

@rultor merge

@userzimmermann I'm sorry, I don't understand you :( Check this page and try again please

@adhikasp adhikasp deleted the adhikasp:adhikasp/get-module branch Jul 7, 2017

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