Permalink
Browse files

Raise TypeError when composing a Morphism with a non-Morphism.

Invoking Morphism.compose on something that is not a Morphism or a
derived class now raises TypeError.  __mul__ will in this case raise a
NotImplementedError instead.
  • Loading branch information...
1 parent 8dedae3 commit 9fffc9ba19619919557d563c0bab8db964ecbae6 @scolobb committed Jun 12, 2012
Showing with 15 additions and 1 deletion.
  1. +10 −1 sympy/categories/baseclasses.py
  2. +5 −0 sympy/categories/tests/test_baseclasses.py
@@ -256,6 +256,9 @@ def compose(self, g, new_name=""):
'h'
"""
+ if not isinstance(g, Morphism):
+ raise TypeError("Morphisms can only be composed with morphisms.")
+
if g.codomain != self.domain:
raise ValueError("Uncomponsable morphisms.")
@@ -281,7 +284,10 @@ def __mul__(self, g):
=======
compose
"""
- return self.compose(g)
+ try:
+ return self.compose(g)
+ except TypeError:
+ return NotImplemented
def flatten(self, new_name=""):
"""
@@ -478,6 +484,9 @@ def compose(self, g, new_name=""):
True
"""
+ if not isinstance(g, Morphism):
+ raise TypeError("Morphisms can only be composed with morphisms.")
+
if g.codomain != self.domain:
raise ValueError("Uncomponsable morphisms.")
@@ -107,6 +107,11 @@ def test_morphism():
assert f != Morphism(A, B)
assert f == f
+ raises(TypeError, lambda: f.compose(None))
+ raises(TypeError, lambda: id_A.compose(None))
+ raises(TypeError, lambda: f * None)
+ raises(TypeError, lambda: id_A * None)
+
def test_diagram():
A = Object("A")
B = Object("B")

0 comments on commit 9fffc9b

Please sign in to comment.