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

KeyError: 'stats' in player.py #603

Closed
MihaMuskinja opened this issue Oct 31, 2024 · 13 comments · Fixed by #604
Closed

KeyError: 'stats' in player.py #603

MihaMuskinja opened this issue Oct 31, 2024 · 13 comments · Fixed by #604

Comments

@MihaMuskinja
Copy link

Sport

Basketball

Summary

I get a KeyError: 'stats' during the api execution. Has something changed in the api?

Logs/Data

  File "/Users/mihamuskinja/git/fantasy/api.py", line 56, in get_league
    league = League(league_id=109755, year=2025, swid=swid, espn_s2=espn_s2, debug=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/fantasy/lib/python3.11/site-packages/espn_api/basketball/league.py", line 19, in __init__
    self.fetch_league()
  File "/opt/homebrew/Caskroom/miniconda/base/envs/fantasy/lib/python3.11/site-packages/espn_api/basketball/league.py", line 23, in fetch_league
    self._fetch_teams(data)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/fantasy/lib/python3.11/site-packages/espn_api/basketball/league.py", line 50, in _fetch_teams
    super()._fetch_teams(data, TeamClass=Team, pro_schedule=pro_schedule)
  File "/opt/homebrew/Caskroom/miniconda/base/envs/fantasy/lib/python3.11/site-packages/espn_api/base_league.py", line 86, in _fetch_teams
    self.teams.append(TeamClass(team, roster=roster, schedule=schedule, year=seasonId, owners=owners, pro_schedule=pro_schedule))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/fantasy/lib/python3.11/site-packages/espn_api/basketball/team.py", line 30, in __init__
    self._fetch_roster(roster, year, kwargs.get('pro_schedule'))
  File "/opt/homebrew/Caskroom/miniconda/base/envs/fantasy/lib/python3.11/site-packages/espn_api/basketball/team.py", line 44, in _fetch_roster
    self.roster.append(Player(player, year, pro_schedule))
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Caskroom/miniconda/base/envs/fantasy/lib/python3.11/site-packages/espn_api/basketball/player.py", line 45, in __init__
    if split['stats']:
       ~~~~~^^^^^^^^^
KeyError: 'stats'
@MihaMuskinja
Copy link
Author

Maybe change this?

                if split['stats']:

into

                if 'stats' in split and split['stats']:

@monsdar
Copy link
Contributor

monsdar commented Oct 31, 2024

Updating espn_api to the newest version solved this for me. Came from 0.39.0, updated to 0.40.1

UPDATE: Nevermind... My unittests were just called in a different order. The issue still persists, it just comes up later in the test suite and I was too fast writing this message.

@monsdar
Copy link
Contributor

monsdar commented Oct 31, 2024

Seems to be similar to this issue: https://github.com/cwendt94/espn-api/pull/591/files

So changing split['stats'] to split.get('stats') should be good enough.

@monsdar
Copy link
Contributor

monsdar commented Oct 31, 2024

Yeah, that works fine for me locally, PR incoming

@martincham
Copy link
Contributor

Was curious about why this is happening. Heres some splits for Bogdan Bogdanovich. Some werid stuff going on with the 'externalId' as well.
Screenshot 2024-10-31 at 10 13 14 AM
Screenshot 2024-10-31 at 10 13 21 AM
Screenshot 2024-10-31 at 10 13 28 AM

@atagunduzalp
Copy link

hi guys, when is this commit merged and usable? thanks!

@shaggycamel
Copy link

I'm experiencing this issue too

@nathanlochunho
Copy link

Same, hope this issue will be fixed soon!

@monsdar
Copy link
Contributor

monsdar commented Nov 1, 2024

Even when my PR fixes the error it's still unclear what happens datawise. It's not really a fix when there's no exception anymore but half of the data is missing.

@martincham
Copy link
Contributor

martincham commented Nov 1, 2024

It's not really a fix when half of the data is missing.

There doesn't seem to be data missing. This case is only happening for Bogdan Bogdaovich, Dejounte Murray, and Immanuel Quickly. They all played the 1st game of the season and have missed since, so their "Last 7" stats are empty. For some reason ESPN is differently populating players who've missed part of the season, rather than the whole season. Perhaps some edge-case error they introduced in their API?

@martincham
Copy link
Contributor

martincham commented Nov 1, 2024

Same, hope this issue will be fixed soon!

hi guys, when is this commit merged and usable? thanks!

You can also fix it locally. Go to player.py, or debug and trace it to where your player.py is located, and change line 45 to :

if split.get('stats'):

@monsdar
Copy link
Contributor

monsdar commented Nov 1, 2024

There doesn't seem to be data missing.

Awesome, thanks for checking that!

You can also fix it locally.

I just ported our Telegram bot to run as an Azure Function. There's no easy way to fix it within the function, so I guess I'll need to wait for the merge. Hooray, Cloud!

@cwendt94
Copy link
Owner

cwendt94 commented Nov 1, 2024

Thanks for all the investigation! The fix has been added in v0.41.0!

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 a pull request may close this issue.

7 participants