Skip to content

Commit

Permalink
Raise TypeError when composing a Morphism with a non-Morphism.
Browse files Browse the repository at this point in the history
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
scolobb committed Jun 15, 2012
1 parent 8dedae3 commit 9fffc9b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion sympy/categories/baseclasses.py
Expand Up @@ -256,6 +256,9 @@ def compose(self, g, new_name=""):
'h' 'h'
""" """
if not isinstance(g, Morphism):
raise TypeError("Morphisms can only be composed with morphisms.")

if g.codomain != self.domain: if g.codomain != self.domain:
raise ValueError("Uncomponsable morphisms.") raise ValueError("Uncomponsable morphisms.")


Expand All @@ -281,7 +284,10 @@ def __mul__(self, g):
======= =======
compose compose
""" """
return self.compose(g) try:
return self.compose(g)
except TypeError:
return NotImplemented


def flatten(self, new_name=""): def flatten(self, new_name=""):
""" """
Expand Down Expand Up @@ -478,6 +484,9 @@ def compose(self, g, new_name=""):
True True
""" """
if not isinstance(g, Morphism):
raise TypeError("Morphisms can only be composed with morphisms.")

if g.codomain != self.domain: if g.codomain != self.domain:
raise ValueError("Uncomponsable morphisms.") raise ValueError("Uncomponsable morphisms.")


Expand Down
5 changes: 5 additions & 0 deletions sympy/categories/tests/test_baseclasses.py
Expand Up @@ -107,6 +107,11 @@ def test_morphism():
assert f != Morphism(A, B) assert f != Morphism(A, B)
assert f == f 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(): def test_diagram():
A = Object("A") A = Object("A")
B = Object("B") B = Object("B")
Expand Down

0 comments on commit 9fffc9b

Please sign in to comment.