Skip to content

Commit 656d3d2

Browse files
despadamedoardob90
andauthored
Fix test in Object-Oriented Example 4 (empa-scientific-it#262)
* Add __eq__ validation in Example 4 * Add a comment to explain the rationale behind __closure__ testing --------- Co-authored-by: edoardob90 <edoardob90@gmail.com>
1 parent 9240a6d commit 656d3d2

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

object_oriented_programming.ipynb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
" - [Example 1](#Example-1)\n",
2020
" - [Properties and methods](#Properties-and-methods)\n",
2121
" - [Example 2](#Example-2)\n",
22-
" - [Python's *special methods*](#Python's-*special-methods*)\n",
22+
" - [Python's special methods](#Python's-special-methods)\n",
2323
" - [`__str__` and `__repr__`](#__str__-and-__repr__)\n",
2424
" - [Example 3](#Example-3)\n",
2525
" - [Comparison methods](#Comparison-methods)\n",
@@ -220,7 +220,7 @@
220220
" \"\"\"\n",
221221
"\n",
222222
" # Write your solution here\n",
223-
" pass"
223+
" return"
224224
]
225225
},
226226
{
@@ -357,7 +357,7 @@
357357
" \"\"\"\n",
358358
"\n",
359359
" # Write your solution here\n",
360-
" pass"
360+
" return"
361361
]
362362
},
363363
{
@@ -367,7 +367,7 @@
367367
"tags": []
368368
},
369369
"source": [
370-
"# Python's *special methods*"
370+
"# Python's special methods"
371371
]
372372
},
373373
{
@@ -684,7 +684,7 @@
684684
" \"\"\"\n",
685685
"\n",
686686
" # Write your solution here\n",
687-
" pass"
687+
" return"
688688
]
689689
},
690690
{
@@ -974,7 +974,7 @@
974974
" \"\"\"\n",
975975
"\n",
976976
" # Write your solution here\n",
977-
" pass"
977+
" return"
978978
]
979979
},
980980
{
@@ -1429,7 +1429,7 @@
14291429
" \"\"\"\n",
14301430
"\n",
14311431
" # Write your solution here\n",
1432-
" pass"
1432+
" return"
14331433
]
14341434
},
14351435
{
@@ -1476,11 +1476,11 @@
14761476
" Args:\n",
14771477
" flavors: all available ice cream flavors\n",
14781478
" Returns:\n",
1479-
" - a bowl\n",
1479+
" - a Bowl instance\n",
14801480
" \"\"\"\n",
14811481
"\n",
14821482
" # Write your solution here\n",
1483-
" pass"
1483+
" return"
14841484
]
14851485
},
14861486
{
@@ -1538,7 +1538,7 @@
15381538
" \"\"\"\n",
15391539
"\n",
15401540
" # Write your solution here\n",
1541-
" pass"
1541+
" return"
15421542
]
15431543
},
15441544
{
@@ -1680,7 +1680,7 @@
16801680
" \"\"\"\n",
16811681
"\n",
16821682
" # Write your solution here\n",
1683-
" pass"
1683+
" return"
16841684
]
16851685
}
16861686
],
@@ -1700,7 +1700,7 @@
17001700
"name": "python",
17011701
"nbconvert_exporter": "python",
17021702
"pygments_lexer": "ipython3",
1703-
"version": "3.10.15"
1703+
"version": "3.10.13"
17041704
}
17051705
},
17061706
"nbformat": 4,

tutorial/tests/test_object_oriented_programming.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ def __init__(self):
1313
# Example 1: Person
1414
#
1515

16+
# NOTE:
17+
# When testing the classes returned by the solutions function in the Examples 1-4
18+
# we often need to check if a class method is implemented and it's using the right attributes.
19+
# One way to check both is to check if the class's method has the __closure__ attribute.
20+
# Why?
21+
# 1. If __method__ is not implemented, Python will fetch object.__method__ instead, which obviously is not a closure
22+
# `hasattr(solution_result.__method__, "__closure__")` will return False.
23+
# 2. If __method__ is implemented, but it's not using the class attributes, it will be a closure
24+
# `solution_result.__method__.__closure__ is None` will return False.
25+
# Reference: https://github.com/empa-scientific-it/python-tutorial/pull/249#discussion_r1836867387
26+
1627

1728
def reference_oop_person(first_name: str, last_name: str):
1829
class Person:
@@ -211,6 +222,9 @@ def validate_oop_compare_persons(solution_result):
211222
assert (
212223
type(solution_result).__name__ == "Person"
213224
), "The class should be named 'Person'."
225+
assert hasattr(
226+
solution_result.__eq__, "__closure__"
227+
), "Make sure that the class is properly implementing the __eq__() method."
214228
# Check the class attributes
215229
try:
216230
attrs = list(vars(solution_result))

0 commit comments

Comments
 (0)