Skip to content

Reject bool for int settings (CACHE_SIZE_LIMIT, DEFAULT_DAYS_IN_MONTH) - #1386

Open
Pitchfork-and-Torch wants to merge 1 commit into
scrapinghub:masterfrom
Pitchfork-and-Torch:cook/reject-bool-int-settings
Open

Pitchfork-and-Torch wants to merge 1 commit into
scrapinghub:masterfrom
Pitchfork-and-Torch:cook/reject-bool-int-settings

Conversation

@Pitchfork-and-Torch

Copy link
Copy Markdown

Summary

bool subclasses int, so isinstance(True, int) is true. That let CACHE_SIZE_LIMIT=True / DEFAULT_DAYS_IN_MONTH=True pass settings validation and silently behave as 1 (and False as 0).

Reject bool explicitly when the setting type is int.

Test plan

  • New test test_check_settings_rejects_bool_for_int_settings passes
  • Valid int values for CACHE_SIZE_LIMIT / DEFAULT_DAYS_IN_MONTH still accepted

@serhii73 serhii73 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Checked the diff, the surrounding code and the docs. I'm not approving yet, because one issue should be settled first.

URGENT: breaking change for callers that pass a bool (dateparser/conf.py:263-267, the new isinstance(setting_value, bool) branch).
CACHE_SIZE_LIMIT treats any falsy value as "no limit" (dateparser/languages/dictionary.py:314-315: self._settings.CACHE_SIZE_LIMIT and len(cache) > ...), and docs/settings.rst says to set 0 to turn the limit off. CACHE_SIZE_LIMIT=False is not documented, but it works today and means exactly that: on current master DateDataParser(settings={"CACHE_SIZE_LIMIT": False}) is accepted and parse("12 June 2020", settings={"CACHE_SIZE_LIMIT": False}) returns a date. With this PR the same call raises SettingValidationError, so code that runs fine on 1.5.0 breaks on upgrade. The same applies to True/False for DEFAULT_DAYS_IN_MONTH. The project usually deprecates before it rejects (for example #1556).
Suggested fix: keep accepting bool for now, convert it with int() and emit a DeprecationWarning that says to pass an int (0 to disable the cache limit), and reject it in a later release. At the very least, document the new validation and keep False working as 0 for CACHE_SIZE_LIMIT.

Non-urgent notes:

  • No usable CI: the three workflow runs for this head ended in failure with no jobs (a startup failure), and the branch conflicts with current master. Please rebase so CI can run.
  • The test asserts only that valid values give a truthy object. Assert the stored value (settings.CACHE_SIZE_LIMIT == 100, and 0). assertRaisesRegex is given an unescaped pattern (. and quotes); re.escape would make it robust.
  • Only bool is rejected; DEFAULT_DAYS_IN_MONTH=0/negative and a negative CACHE_SIZE_LIMIT are still accepted. Optional follow-up.
  • setting_props["type"] is int would not cover a future setting declared as a tuple of types or numbers.Integral; a check of the form "bool value and bool not among the expected types" would be more general.

CACHE_SIZE_LIMIT=False is still stored as 0, which disables the limit, and True is stored as 1. DEFAULT_DAYS_IN_MONTH is the same. Both emit DeprecationWarning. A later release can reject them.
@Pitchfork-and-Torch
Pitchfork-and-Torch force-pushed the cook/reject-bool-int-settings branch from c881cc8 to f3b397a Compare November 5, 2026 23:35
@Pitchfork-and-Torch

Copy link
Copy Markdown
Author

Rebased onto current master.

Bool is no longer rejected. CACHE_SIZE_LIMIT=False is stored as 0, which still disables the limit, and True is stored as 1. DEFAULT_DAYS_IN_MONTH is the same. Both emit DeprecationWarning, and the docs and HISTORY say a later release can reject them. The tests assert the stored int, including 0 and 100.

This branch has not been deployed

No deployments
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