Skip to content

Commit

Permalink
Raise typeerror on comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyephron committed Jul 15, 2018
1 parent 082f14f commit 548465c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
30 changes: 19 additions & 11 deletions explorecourses/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- LearningObjective
- Attribute
- Tag
"""

from typing import Tuple
Expand Down Expand Up @@ -88,7 +89,7 @@ def get_department(self, idf: str) -> Department:
Returns:
Department: The department matched by the given identifier if a
match was found, None otherwise.
"""

idf = idf.lower()
Expand Down Expand Up @@ -475,7 +476,7 @@ def __str__(self):
def __eq__(self, other):
"""
Overloads the equality (==) operator for the Course class.
A Course can only be compared to another Course. Course equality is
determined by course ID.
Expand Down Expand Up @@ -508,7 +509,10 @@ def __lt__(self, other):
"""

if type(other) != Course: return False
if type(other) != Course:
raise TypeError(f"'<' not supported between instances of "
f"'{type(self)}' and '{type(other)}'")

if self.subject != other.subject:
return self.subject < other.subject
if self.code != other.code:
Expand All @@ -534,14 +538,11 @@ def __gt__(self, other):
"""

if type(other) != Course: return False
if self.subject != other.subject:
return self.subject > other.subject
if self.code != other.code:
return self.code > other.code
if self.year != other.year:
return self.year > other.year
return False
if type(other) != Course:
raise TypeError(f"'>' not supported between instances of "
f"'{type(self)}' and '{type(other)}'")

return not self.__lt__(other) and not self.__eq__(other)


def __le__(self, other):
Expand All @@ -560,6 +561,10 @@ def __le__(self, other):
"""

if type(other) != Course:
raise TypeError(f"'<=' not supported between instances of "
f"'{type(self)}' and '{type(other)}'")

return self.__lt__(other) or self.__eq__(other)


Expand All @@ -578,5 +583,8 @@ def __ge__(self, other):
the Course, False otherwise.
"""
if type(other) != Course:
raise TypeError(f"'>=' not supported between instances of "
f"'{type(self)}' and '{type(other)}'")

return self.__gt__(other) or self.__eq__(other)
4 changes: 2 additions & 2 deletions explorecourses/course_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CourseConnection():
_URL = "http://explorecourses.stanford.edu/"

def __init__(self):
"""
"""
Constructs a new CourseConnection by beginning a requests session.
"""
Expand Down Expand Up @@ -54,7 +54,7 @@ def get_school(self, name: str) -> School:
Gets a school within the university by name.
Args:
name (str): The name of the school.the
name (str): The name of the school.
Returns:
School: The school if it exists, None otherwise.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setuptools.setup(
name="explorecourses",
version="1.0.3",
version="1.0.4",
url="https://github.com/illiteratecoder/Explore-Courses-API",
author="Jeremy Ephron",
author_email="[email protected]",
Expand Down
15 changes: 15 additions & 0 deletions tests/test_course.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from xml.etree import ElementTree as ET

import pytest

from explorecourses import *

class TestCourse(object):
Expand Down Expand Up @@ -1235,3 +1237,16 @@ def test_course_comparisons(self):
assert math19 < math20
assert math20 >= math20
assert math19 <= math19

with pytest.raises(TypeError):
math19 < 9

with pytest.raises(TypeError):
math20 > "test"

with pytest.raises(TypeError):
math19 <= [1, 2, 3]

with pytest.raises(TypeError):
math20 >= 10

0 comments on commit 548465c

Please sign in to comment.