Skip to content

Reject bool for Schema(int) type checks - #554

Open
Pitchfork-and-Torch wants to merge 1 commit into
alecthomas:masterfrom
Pitchfork-and-Torch:cook/reject-bool-integer-schema
Open

Pitchfork-and-Torch wants to merge 1 commit into
alecthomas:masterfrom
Pitchfork-and-Torch:cook/reject-bool-integer-schema

Conversation

@Pitchfork-and-Torch

Copy link
Copy Markdown

Summary

bool is a subclass of int in Python, so Schema(int) accepted True/False and treated them as 0/1. This rejects booleans for integer type checks while leaving Schema(bool) unchanged.

Bug

>>> from voluptuous import Schema
>>> Schema(int)(True)   # previously returned True
>>> Schema({'age': int})({'age': False})  # previously OK

Fix

In _compile_scalar, when the schema type is int, raise TypeInvalid if the value is a bool.

Test plan

  • pytest voluptuous/tests/tests.py::test_boolean_is_not_an_integer
  • Confirmed Schema(bool)(True/False) still works
  • Nested dict / ExactSequence([int, int]) reject bools

bool is a subclass of int, so Schema(int) accepted True/False and
treated them as 0/1. Reject booleans explicitly so integer fields
cannot be satisfied by a bool. Schema(bool) is unchanged.

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.

1 participant