diff --git a/schema/__init__.py b/schema/__init__.py index 10d6e8a..6533090 100644 --- a/schema/__init__.py +++ b/schema/__init__.py @@ -246,7 +246,7 @@ def __init__( ) -> None: self._pattern_str: str = pattern_str flags_list = [ - Regex.NAMES[i] for i, f in enumerate(f"{flags:09b}") if f != "0" + Regex.NAMES[i] for i, f in enumerate(f"{int(flags):09b}") if f != "0" ] # Name for each bit self._flags_names: str = ", flags=" + "|".join(flags_list) if flags_list else "" diff --git a/test_schema.py b/test_schema.py index 4d78456..adaf216 100644 --- a/test_schema.py +++ b/test_schema.py @@ -215,6 +215,19 @@ def test_regex(): Regex(None).validate("bar") +def test_regex_flags(): + # Passing an actual re flag (a re.RegexFlag enum member, not a plain int) + # must not crash while building the human-readable repr, and the flag must + # still be applied. On CPython <= 3.10 and PyPy, formatting a RegexFlag with + # the "b" format code raised ValueError before the flag was compiled. + single = Regex(r"foo", flags=re.IGNORECASE) + assert single.validate("FOObar") == "FOObar" + assert "re.IGNORECASE" in repr(single) + + multi = Regex(r"^foo", flags=re.IGNORECASE | re.MULTILINE) + assert multi.validate("bar\nFOO") == "bar\nFOO" + + def test_validate_list(): assert Schema([1, 0]).validate([1, 0, 1, 1]) == [1, 0, 1, 1] assert Schema([1, 0]).validate([]) == []