Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Verify home key exists when querying for matchup data #409

Merged
merged 1 commit into from Nov 22, 2022

Conversation

jelias2
Copy link
Contributor

@jelias2 jelias2 commented Nov 18, 2022

  • I discovered a bug which caused the intialization of the league object to crash.
  • I believe this occurred because in my league we have an odd number of teams which results in one matchup each week having only one team and that team always appears to be the away team,

When creating the outcomes object the code looks for the home and away keys even though they may not exist in the matchup object in a league with an odd number of teams.

This PR adds a check to ensure the home key exists prior to lookup to avoid a crash.

Here was the error I received, prior to implementing the fix.

python3 main.py                                                                                                                                                                              ─╯
Traceback (most recent call last):
  File "/Users/jacobelias/Desktop/workspace/fantasy-football/main.py", line 605, in <module>
    main(swid, espn_s2, league_id, week)
  File "/Users/jacobelias/Desktop/workspace/fantasy-football/main.py", line 514, in main
    league = League(league_id=league_id, year=2022, espn_s2=espn_s2, swid=swid)
  File "/opt/homebrew/lib/python3.10/site-packages/espn_api/football/league.py", line 23, in __init__
    self.fetch_league()
  File "/opt/homebrew/lib/python3.10/site-packages/espn_api/football/league.py", line 26, in fetch_league
    self._fetch_league()
  File "/opt/homebrew/lib/python3.10/site-packages/espn_api/football/league.py", line 33, in _fetch_league
    self._fetch_teams(data)
  File "/opt/homebrew/lib/python3.10/site-packages/espn_api/football/league.py", line 38, in _fetch_teams
    super()._fetch_teams(data, TeamClass=Team)
  File "/opt/homebrew/lib/python3.10/site-packages/espn_api/base_league.py", line 63, in _fetch_teams
    self.teams.append(TeamClass(team, roster=roster, member=member, schedule=schedule, year=seasonId))
  File "/opt/homebrew/lib/python3.10/site-packages/espn_api/football/team.py", line 39, in __init__
    self._fetch_schedule(schedule)
  File "/opt/homebrew/lib/python3.10/site-packages/espn_api/football/team.py", line 66, in _fetch_schedule
    elif matchup['home']['teamId'] == self.team_id:
KeyError: 'home'

S/o to @wowcanubelieveit for providing the league with odd teams and the crash scenario, and for rapping dreams and nightmares to motivate me while debugging

@cwendt94 cwendt94 merged commit 5441d53 into cwendt94:master Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants