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

Ensure SP algebra matches vocab #240

Merged
merged 5 commits into from Mar 26, 2020
Merged

Ensure SP algebra matches vocab #240

merged 5 commits into from Mar 26, 2020

Conversation

jgosmann
Copy link
Collaborator

@jgosmann jgosmann commented Mar 22, 2020

Motivation and context:

Mixing algebras is not supported without explicit casts. Thus raise an exception when this is tried.

SemanticPointe.reinterpret can be used to add the pointer anyways.

Fixes #239.

How has this been tested?

unit test

How long should this take to review?

  • Quick (less than 40 lines changed or changes are straightforward)

Types of changes:

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the CONTRIBUTING.rst document.
  • I have updated the documentation accordingly.
  • I have included a changelog entry.
  • I have added tests to cover my changes.
  • I have run the test suite locally and all tests passed.

@jgosmann jgosmann marked this pull request as ready for review March 22, 2020 14:54
@jgosmann jgosmann requested a review from arvoelke March 22, 2020 14:54
Copy link
Contributor

@arvoelke arvoelke left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this @jgosmann. I added a couple minor commits that should be self-explanatory. LGTM. Someone from the Nengo team will likely want to take a look and merge.

@@ -63,6 +64,10 @@ def _get_algebra(cls, vocab, algebra):
algebra = vocab.algebra
elif vocab is not None and vocab.algebra is not algebra:
raise ValueError("vocab and algebra argument are mutually exclusive")
if not isinstance(algebra, AbstractAlgebra):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This seems unpythonic to me. Usually duck typing should be fine and enforcing that an algebra derives from AbstractAlgebra technically seems like a breaking change to me.

I would prefer to leave this check out (or just test for the protocol if that can be done easily). Of course, this enables a certain class of bugs, but this is a general problem of dynamic typing and there are probably many more instances in Nengo SPA where it would apply. I'd rather consider adding type annotations across the library (though this will probably require dropping support for Python 3.4 which has reached EOL a year ago).

Copy link
Contributor

@arvoelke arvoelke Mar 23, 2020

Choose a reason for hiding this comment

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

This was added because the unit test you created passed VtbAlgebra instead of VtbAlgebra() and things still worked (because the test didn't do anything with the pointers, like try to negate or add them).. moreover, when I did try to do something with them the error message was very cryptic.. so I figured if the mistake was that easy to make then something should be done to avoid having that happen again in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess we should change the AbstractAlgebra class to a proper ABC which should make the isinstance check succeed even if the algebra does not derive from AbstractAlgebra as long as all required methods are implemented.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I made an additional commit to continue to support duck-typing for algebras, but deprecate it (so that in the future we can fully rely on the abstract base class).

jgosmann and others added 5 commits March 26, 2020 20:01
when adding to a vocab.

SemanticPointe.reinterpret can be used to add the pointer anyways.
Mixing algebras is not supported without explicit casts.

Fixes #239.

Clarify that the created SPs keep their algebras with a test and some
added comments. Also removed redundant check, and renamed variable
names in check.
However, deprecate this feature to be able to remove the additional
code for this later and make AbstractAlgebra a pure ABCMeta class.
This allows to do type checking in the vocabulary without a breaking
change.

Due to the deprecation, I consider this worth a minor release and
changed the changelog accordingly.

squash! Continue to allow duck-typing for algebras

Satisfy flake8
@jgosmann jgosmann merged commit 9d00cfc into master Mar 26, 2020
@jgosmann jgosmann deleted the check-algebra branch June 22, 2020 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Vocabulary doesn't consider algebra of its vectors
2 participants