Skip to content

Commit

Permalink
Fix mypy errors and rename NamedTuple fields in gym.py
Browse files Browse the repository at this point in the history
Add None to some type annotations in gym.py in order to fix some mypy
errors.

Rename some fields in the Gym NamedTuple class in order to clarify their
meanings and to fix a mypy error. The "count" field specifically causes
a mypy error because mypy interprets the field's name as the "count"
method that NamedTuple inherits from the base tuple class. According to
the mypy devs, this is expected behavior and is not a bug:
- python/mypy#4507
- python/mypy#17047
  • Loading branch information
tianyizheng02 committed Aug 18, 2024
1 parent f9f3def commit 1e18ead
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
15 changes: 7 additions & 8 deletions pittapi/gym.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@

class Gym(NamedTuple):
name: str
date: str
count: int
percentage: int
last_updated: str | None
current_count: int | None
percent_full: int | None

@classmethod
def from_text(cls, text: str) -> Gym:
info = text.split("|")
name = info[0]
if len(info) < 4:
return cls(name=name, date=None, count=None, percentage=None)
return cls(name=name, last_updated=None, current_count=None, percent_full=None)
count = int(info[2][12:])
date = info[3][9:]
date_time = info[3][9:]
try:
percentage = int(info[4].rstrip("%"))
except ValueError:
percentage = 0

return cls(name=name, date=date, count=count, percentage=percentage)
return cls(name=name, last_updated=date_time, current_count=count, percent_full=percentage)


def get_all_gyms_info() -> list[Gym]:
Expand All @@ -71,7 +71,6 @@ def get_all_gyms_info() -> list[Gym]:
soup = BeautifulSoup(page.text, "html.parser")
gym_info_list = soup.find_all("div", class_="barChart")

# Iterate through list and add to dictionary
gyms = [Gym.from_text(gym.get_text("|", strip=True)) for gym in gym_info_list]
return gyms

Expand All @@ -81,6 +80,6 @@ def get_gym_info(gym_name: str) -> Gym | None:
info = get_all_gyms_info()
if gym_name in GYM_NAMES:
for gym in info:
if gym.name == gym_name and gym.date and gym.count and gym.percentage:
if gym.name == gym_name and gym.last_updated and gym.current_count and gym.percent_full:
return gym
return None
29 changes: 18 additions & 11 deletions tests/gym_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@ def test_fetch_gym_info(self):

gym_info = gym.get_all_gyms_info()
expected_info = [
gym.Gym(name="Baierl Rec Center", date="07/09/2024 09:05 AM", count=100, percentage=50),
gym.Gym(name="Bellefield Hall: Fitness Center & Weight Room", date="07/09/2024 09:05 AM", count=50, percentage=0),
gym.Gym(name="Bellefield Hall: Court & Dance Studio", date=None, count=None, percentage=None),
gym.Gym(name="Trees Hall: Fitness Center", date="07/09/2024 09:05 AM", count=70, percentage=58),
gym.Gym(name="Trees Hall: Courts", date="07/09/2024 09:05 AM", count=20, percentage=33),
gym.Gym(name="Baierl Rec Center", last_updated="07/09/2024 09:05 AM", current_count=100, percent_full=50),
gym.Gym(
name="Bellefield Hall: Fitness Center & Weight Room",
last_updated="07/09/2024 09:05 AM",
current_count=50,
percent_full=0,
),
gym.Gym(name="Bellefield Hall: Court & Dance Studio", last_updated=None, current_count=None, percent_full=None),
gym.Gym(name="Trees Hall: Fitness Center", last_updated="07/09/2024 09:05 AM", current_count=70, percent_full=58),
gym.Gym(name="Trees Hall: Courts", last_updated="07/09/2024 09:05 AM", current_count=20, percent_full=33),
gym.Gym(
name="Trees Hall: Racquetball Courts & Multipurpose Room",
date="07/09/2024 09:05 AM",
count=10,
percentage=25,
last_updated="07/09/2024 09:05 AM",
current_count=10,
percent_full=25,
),
gym.Gym(name="William Pitt Union", date="07/09/2024 09:05 AM", count=25, percentage=25),
gym.Gym(name="Pitt Sports Dome", date="07/09/2024 09:05 AM", count=15, percentage=20),
gym.Gym(name="William Pitt Union", last_updated="07/09/2024 09:05 AM", current_count=25, percent_full=25),
gym.Gym(name="Pitt Sports Dome", last_updated="07/09/2024 09:05 AM", current_count=15, percent_full=20),
]

self.assertEqual(gym_info, expected_info)
Expand All @@ -37,7 +42,9 @@ def test_get_gym_info(self):
responses.add(responses.GET, gym.GYM_URL, body=mock_gym_html, status=200)

gym_info = gym.get_gym_info("Baierl Rec Center")
expected_info = gym.Gym(name="Baierl Rec Center", date="07/09/2024 09:05 AM", count=100, percentage=50)
expected_info = gym.Gym(
name="Baierl Rec Center", last_updated="07/09/2024 09:05 AM", current_count=100, percent_full=50
)
self.assertEqual(gym_info, expected_info)

@responses.activate
Expand Down

0 comments on commit 1e18ead

Please sign in to comment.