From 985e61d6596129c43ed6e01b52e058d4ccc2b29a Mon Sep 17 00:00:00 2001 From: Robert Clark Date: Sun, 12 Apr 2020 16:33:19 -0500 Subject: [PATCH] Handle missing player weights throwing errors If a player doesn't have a weight listed on his/her page, an AttributeError will be thrown while trying to call 'replace' on an object of None. Simply checking if the weight is valid prior to attempting to replace the unit will prevent this error. Signed-Off-By: Robert Clark --- sportsreference/mlb/roster.py | 2 ++ sportsreference/nba/roster.py | 2 ++ sportsreference/nfl/roster.py | 2 ++ sportsreference/nhl/roster.py | 2 ++ tests/unit/test_mlb_roster.py | 8 ++++++++ tests/unit/test_nba_roster.py | 8 ++++++++ tests/unit/test_nfl_roster.py | 8 ++++++++ tests/unit/test_nhl_roster.py | 8 ++++++++ 8 files changed, 40 insertions(+) diff --git a/sportsreference/mlb/roster.py b/sportsreference/mlb/roster.py index a130fad4..6421f422 100644 --- a/sportsreference/mlb/roster.py +++ b/sportsreference/mlb/roster.py @@ -746,6 +746,8 @@ def weight(self): """ Returns an ``int`` of the player's weight in pounds. """ + if not self._weight: + return None return int(self._weight.replace('lb', '')) @property diff --git a/sportsreference/nba/roster.py b/sportsreference/nba/roster.py index f23ec2ee..24e59e7e 100644 --- a/sportsreference/nba/roster.py +++ b/sportsreference/nba/roster.py @@ -720,6 +720,8 @@ def weight(self): """ Returns an ``int`` of the player's weight in pounds. """ + if not self._weight: + return None return int(self._weight.replace('lb', '')) @property diff --git a/sportsreference/nfl/roster.py b/sportsreference/nfl/roster.py index d29f5eea..90e3b0cf 100644 --- a/sportsreference/nfl/roster.py +++ b/sportsreference/nfl/roster.py @@ -663,6 +663,8 @@ def weight(self): """ Returns an ``int`` of the player's weight in pounds. """ + if not self._weight: + return None return int(self._weight.replace('lb', '')) @property diff --git a/sportsreference/nhl/roster.py b/sportsreference/nhl/roster.py index ce23d9ca..9c7a4986 100644 --- a/sportsreference/nhl/roster.py +++ b/sportsreference/nhl/roster.py @@ -568,6 +568,8 @@ def weight(self): """ Returns an ``int`` of the player's weight in pounds. """ + if not self._weight: + return None return int(self._weight.replace('lb', '')) @_int_property_decorator diff --git a/tests/unit/test_mlb_roster.py b/tests/unit/test_mlb_roster.py index 0138220d..e2ed5dee 100644 --- a/tests/unit/test_mlb_roster.py +++ b/tests/unit/test_mlb_roster.py @@ -84,3 +84,11 @@ def test_cleanup_of_none_returns_default_for_player(self): result = _cleanup_player(None) assert result == '' + + @patch('requests.get', side_effect=mock_pyquery) + def test_missing_weight_returns_none(self, *args, **kwargs): + mock_weight = PropertyMock(return_value=None) + player = Player(None) + type(player)._weight = mock_weight + + assert not player.weight diff --git a/tests/unit/test_nba_roster.py b/tests/unit/test_nba_roster.py index 97326c04..40dd763e 100644 --- a/tests/unit/test_nba_roster.py +++ b/tests/unit/test_nba_roster.py @@ -103,6 +103,14 @@ def test_empty_contract_is_none(self): assert player._contract is None + @patch('requests.get', side_effect=mock_pyquery) + def test_missing_weight_returns_none(self, *args, **kwargs): + mock_weight = PropertyMock(return_value=None) + player = Player(None) + type(player)._weight = mock_weight + + assert not player.weight + class TestInvalidNBAPlayer: def test_no_player_data_returns_no_stats(self): diff --git a/tests/unit/test_nfl_roster.py b/tests/unit/test_nfl_roster.py index 36dff3b3..89ce2dd7 100644 --- a/tests/unit/test_nfl_roster.py +++ b/tests/unit/test_nfl_roster.py @@ -38,3 +38,11 @@ def test_invalid_url_returns_none(self, *args, **kwargs): result = player._retrieve_html_page() assert result is None + + @patch('requests.get', side_effect=mock_pyquery) + def test_missing_weight_returns_none(self, *args, **kwargs): + mock_weight = PropertyMock(return_value=None) + player = Player(None) + type(player)._weight = mock_weight + + assert not player.weight diff --git a/tests/unit/test_nhl_roster.py b/tests/unit/test_nhl_roster.py index d1e5602b..8969e8f7 100644 --- a/tests/unit/test_nhl_roster.py +++ b/tests/unit/test_nhl_roster.py @@ -38,3 +38,11 @@ def test_invalid_url_returns_none(self, *args, **kwargs): result = player._retrieve_html_page() assert result is None + + @patch('requests.get', side_effect=mock_pyquery) + def test_missing_weight_returns_none(self, *args, **kwargs): + mock_weight = PropertyMock(return_value=None) + player = Player(None) + type(player)._weight = mock_weight + + assert not player.weight