Skip to content

Commit 548465c

Browse files
committed
Raise typeerror on comparisons
1 parent 082f14f commit 548465c

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

explorecourses/classes.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- LearningObjective
1313
- Attribute
1414
- Tag
15+
1516
"""
1617

1718
from typing import Tuple
@@ -88,7 +89,7 @@ def get_department(self, idf: str) -> Department:
8889
Returns:
8990
Department: The department matched by the given identifier if a
9091
match was found, None otherwise.
91-
92+
9293
"""
9394

9495
idf = idf.lower()
@@ -475,7 +476,7 @@ def __str__(self):
475476
def __eq__(self, other):
476477
"""
477478
Overloads the equality (==) operator for the Course class.
478-
479+
479480
A Course can only be compared to another Course. Course equality is
480481
determined by course ID.
481482
@@ -508,7 +509,10 @@ def __lt__(self, other):
508509
509510
"""
510511

511-
if type(other) != Course: return False
512+
if type(other) != Course:
513+
raise TypeError(f"'<' not supported between instances of "
514+
f"'{type(self)}' and '{type(other)}'")
515+
512516
if self.subject != other.subject:
513517
return self.subject < other.subject
514518
if self.code != other.code:
@@ -534,14 +538,11 @@ def __gt__(self, other):
534538
535539
"""
536540

537-
if type(other) != Course: return False
538-
if self.subject != other.subject:
539-
return self.subject > other.subject
540-
if self.code != other.code:
541-
return self.code > other.code
542-
if self.year != other.year:
543-
return self.year > other.year
544-
return False
541+
if type(other) != Course:
542+
raise TypeError(f"'>' not supported between instances of "
543+
f"'{type(self)}' and '{type(other)}'")
544+
545+
return not self.__lt__(other) and not self.__eq__(other)
545546

546547

547548
def __le__(self, other):
@@ -560,6 +561,10 @@ def __le__(self, other):
560561
561562
"""
562563

564+
if type(other) != Course:
565+
raise TypeError(f"'<=' not supported between instances of "
566+
f"'{type(self)}' and '{type(other)}'")
567+
563568
return self.__lt__(other) or self.__eq__(other)
564569

565570

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

582590
return self.__gt__(other) or self.__eq__(other)

explorecourses/course_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CourseConnection():
1919
_URL = "http://explorecourses.stanford.edu/"
2020

2121
def __init__(self):
22-
"""
22+
"""
2323
Constructs a new CourseConnection by beginning a requests session.
2424
2525
"""
@@ -54,7 +54,7 @@ def get_school(self, name: str) -> School:
5454
Gets a school within the university by name.
5555
5656
Args:
57-
name (str): The name of the school.the
57+
name (str): The name of the school.
5858
5959
Returns:
6060
School: The school if it exists, None otherwise.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setuptools.setup(
44
name="explorecourses",
5-
version="1.0.3",
5+
version="1.0.4",
66
url="https://github.com/illiteratecoder/Explore-Courses-API",
77
author="Jeremy Ephron",
88
author_email="[email protected]",

tests/test_course.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from xml.etree import ElementTree as ET
22

3+
import pytest
4+
35
from explorecourses import *
46

57
class TestCourse(object):
@@ -1235,3 +1237,16 @@ def test_course_comparisons(self):
12351237
assert math19 < math20
12361238
assert math20 >= math20
12371239
assert math19 <= math19
1240+
1241+
with pytest.raises(TypeError):
1242+
math19 < 9
1243+
1244+
with pytest.raises(TypeError):
1245+
math20 > "test"
1246+
1247+
with pytest.raises(TypeError):
1248+
math19 <= [1, 2, 3]
1249+
1250+
with pytest.raises(TypeError):
1251+
math20 >= 10
1252+

0 commit comments

Comments
 (0)