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

Tdl 24072 fix typecasting bug max api limit #84

Merged

Conversation

somethingmorerelevant
Copy link
Member

Description of change

  • Fixed int() argument must be a string, a bytes-like object or a number, not 'NoneType' issue with apI_max_limit typecasting

Manual QA steps

Risks

Rollback steps

  • revert this branch

Comment on lines 95 to 96
# Raises TypeError for None / Null Value
# Raises ValueError for "" / bool value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this a logger error like:

LOGGER.warning("Invalid max_daily_calls was passed: \'{}\', using default value of 40,000.".format("max_daily_calls"))

That way it's a little clearer what might have happened to folks reading the logs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int(max_daily_calls or MAX_DAILY_CALLS) expression will fallback to default value for None, True/False and "".

IMO any other exception should be raised instead of falling back to default value.

>>> int("" or 100)
100
>>> int(True or 100)
1
>>> int(False or 100)
100
>>> int('1000' or 100)
1000
>>> int('True' or 100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'True'

Copy link
Member Author

@somethingmorerelevant somethingmorerelevant Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the idea of failing the code for a non-mandatory value but suppressing it will not handle all scenarios, this will only result in the user observing unexpected behaviour.

  • Number with "," eg: 1,000,000 (internationalisation issues)
  • Negative Number
  • Decimal input
  • Zero as a string i.e "0" (which will result in the limit being set to Zero).

Hence i find it best to raise an error incase of any such values instead of using a fallback mechanism.

Copy link
Member Author

@somethingmorerelevant somethingmorerelevant Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the helper text in UI for this tap also displays a value with a comma for the maximum daily api calls limit i.e 50,000 which will also be a invalid input

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have added a few more test scenarios and handled the negative / zero limit condition

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have precedence in other taps of logging if we're going to fall back to a default value and allowing the tap to try to proceed with the default, but not really for an API quota scenario, so I'm good either way

tests/test_client_max_api_limit.py Outdated Show resolved Hide resolved
Verify that max_daily_calls is default if no value is passed
"""
# Initialize Client object
client = Client(**{'endpoint': "123-ABC-789",'client_id':'ABC-123','client_secret':'123-QRT'})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be consistent with the ' and "

Comment on lines 25 to 31
def test_maxdailycalls_default_bool_val(self):
"""
Verify that max_daily_calls is default if bool value is passed
"""
# Initialize Client object
client = Client(**{'endpoint': "123-ABC-789",'client_id':'ABC-123','client_secret':'123-QRT','max_daily_calls':False})
self.assertEqual(client.max_daily_calls ,MAX_DAILY_CALLS)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True bool value should also be tested.

@somethingmorerelevant
Copy link
Member Author

somethingmorerelevant commented Sep 15, 2023

try:
     self.max_daily_calls = int(max_daily_calls or MAX_DAILY_CALLS)
     if self.max_daily_calls <= 1:
        raise ValueError("Limit Cannot be Negative or Zero")
except (ValueError, TypeError) as err:
    singer.log_critical(f"Invalid Value passed for max_daily_calls: {max_daily_calls}")
     raise err

This is the only issue i see with the current implementation👇🏼

If the value is passed as a string "0" it throws an exception since the value is negative/zero, but if the same value is passed as 0 it sets it to default.

eg:

>>> int("0" or 40000)
0
>>> int(0 or 40000)
40000

@somethingmorerelevant somethingmorerelevant merged commit bf4c113 into master Sep 21, 2023
2 checks passed
@somethingmorerelevant somethingmorerelevant deleted the TDL-24072-fix-typecasting-bug-max-api-limit branch September 22, 2023 10:08
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.

3 participants