zero-sum-seattle / python-mlb-statsapi

Python Wrapper for the MLB's Official Stats API
MIT License
41 stars 10 forks source link

AssertionError: season, seasonAdvanced stats MLB API Issue #179

Closed KCNilssen closed 1 year ago

KCNilssen commented 1 year ago

Output:

====================================== short test summary info ====================================== 

FAILED tests/external_tests/stats/test_catching.py::TestCatchingStats::test_catching_stat_attributes_player - AssertionError: 1 != 2
FAILED tests/external_tests/stats/test_catching.py::TestCatchingStats::test_catching_stat_attributes_team - AssertionError: 1 != 2
FAILED tests/external_tests/stats/test_fielding.py::TestPitchingStats::test_fielding_stat_attributes_player - AssertionError: 1 != 2
FAILED tests/external_tests/stats/test_fielding.py::TestPitchingStats::test_fielding_stat_attributes_team - AssertionError: 2 != 4
FAILED tests/external_tests/stats/test_hitting.py::TestHittingStats::test_hitting_bydate_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_hitting.py::TestHittingStats::test_hitting_bydayofweek_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_hitting.py::TestHittingStats::test_hitting_bymonth_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_hitting.py::TestHittingStats::test_hitting_excepected_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_hitting.py::TestHittingStats::test_hitting_hotcoldzones_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_hitting.py::TestHittingStats::test_hitting_pitchArsenal_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_hitting.py::TestHittingStats::test_hitting_pitchlog_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_hitting.py::TestHittingStats::test_hitting_stat_attributes_player - AssertionError: 2 != 4
FAILED tests/external_tests/stats/test_hitting.py::TestHittingStats::test_hitting_stat_attributes_team - AssertionError: 2 != 4
FAILED tests/external_tests/stats/test_hitting.py::TestHittingStats::test_hitting_vsteam_stats_team - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_pitching.py::TestPitchingStats::test_pitching_bydate_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_pitching.py::TestPitchingStats::test_pitching_bydayofweek_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_pitching.py::TestPitchingStats::test_pitching_bymonth_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_pitching.py::TestPitchingStats::test_pitching_excepected_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_pitching.py::TestPitchingStats::test_pitching_hotcoldzones_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_pitching.py::TestPitchingStats::test_pitching_pitchArsenal_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_pitching.py::TestPitchingStats::test_pitching_pitchlog_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_pitching.py::TestPitchingStats::test_pitching_playlog_stats_player - AssertionError: {} == {}
FAILED tests/external_tests/stats/test_pitching.py::TestPitchingStats::test_pitching_stat_attributes_player - AssertionError: 2 != 4
FAILED tests/external_tests/stats/test_pitching.py::TestPitchingStats::test_pitching_stat_attributes_team - AssertionError: 2 != 4

Issues:

Team endpoint that should return hitting stats for season: https://statsapi.mlb.com/api/v1/teams/133/stats?stats=season,career&group=hitting

MLB api now returns Object not found when asked for season stats, which is a new output we have not seen before from the MLB api. When an addition stats type is included such as stats=season,career instead of just stats=season, we are returned a normal return but with only stats for career, it does not even include an empty season stats.

{
   "messageNumber":10,
   "message":"Object not found",
   "timestamp":"2023-02-28T20:31:24.076747546Z",
   "traceId":null
}

People endpoint that should return stats for season and seasonAdvanced: https://statsapi.mlb.com/api/v1/people/660271/stats?stats=season,seasonAdvanced&group=pitching

MLB api returns an empty stats object, which unlike above, an empty stats object that I assume is meant to contain the pitching season and seasonAdvanced stats objects. When an additional group is added like above, stats=season,seasonAdvanced,career, only the career pitching stats are returned, missing both the season and seasonAdvanced.

{
   "copyright":"Copyright 2023 MLB Advanced Media, L.P.  Use of any content on this page acknowledges agreement to the terms posted here http://gdx.mlb.com/components/copyright.txt",
   "stats":[]
}

Summery: The failed tests are all tied with the season and seasonAdvanced stat types. Because of these issues when we ask for 4 stat types and two of which are the issued season types, only 2 stat types are returned. Meaning that with a test expecting 4 but only receiving two, it fails, creating an assertionError. Example below where we ask for season and career catching stats. Since only one is returned when we check, 1 != 2 and thus the AssertionError:

def test_catching_stat_attributes_team(self):
        """mlb get stats should return pitching stats"""
        self.stats = ['season', 'career']
        self.group = ['catching']
        # let's get some stats
        stats = self.mlb.get_team_stats(self.al_team, stats=self.stats, groups=self.group)

        # check for empty dict
        self.assertNotEqual(stats, {})

        # the end point should give us 2 hitting
        self.assertTrue('catching' in stats)
        self.assertFalse('hitting' in stats)
>       self.assertEqual(len(stats['catching']), 2)
E       AssertionError: 1 != 2

Thoughts

I think this may have something to do with it being the offseason. Could this be how the MLB api handles its returns for between seasons or is this the result of a change or update with the MLB api and how they handle these endpoints? Due the the lack of api information that is publicly available at this time we have no real idea.

KCNilssen commented 1 year ago

@Mattsface What do you think, should we temporarily change the tests to "wait out" till the season starts to see if this is how the MLB api handles offseason stats requests for season or should we handle it with some code checking the date against the mlb season start and end dates when asking for season stats?

Not sure what we can really do besides just wait it out and see given the lack of public facing documentation on the api

Mattsface commented 1 year ago

@KCNilssen I will explore updating the pytests to add a specific season parameter. I believe you might be on to something with the new season starting.

KCNilssen commented 1 year ago

@Mattsface I think the only thing to do at the moment is wait it out and see. I tried to find information on It and came up empty.

https://statsapi.mlb.com/api/v1/people/663728/stats?stats=season&season=2018&group=catching

Adding the season param for 2018 to see if that would change anything still results in an empty return. I think this issue must be with season stats? Issues could be:

Mattsface commented 1 year ago

@Mattsface I think the only thing to do at the moment is wait it out and see. I tried to find information on It and came up empty.

https://statsapi.mlb.com/api/v1/people/663728/stats?stats=season&season=2018&group=catching

Adding the season param for 2018 to see if that would change anything still results in an empty return. I think this issue must be with season stats? Issues could be:

  • They are updating season stats and have chosen this for the mean time?
  • Season stats are broken on api side
  • They have completely changed the endpoint and the way we get stats

Let's give it a few days to see how things work out. I wish we had some information from the MLB on this.

Mattsface commented 1 year ago

PR Made. I added the season param to all stat tests.

KCNilssen commented 1 year ago

Resolved with PR that includes a specified season for season stats to use to avoid the offseason mlb stats returning empty values.