88
99class RegexVariantName (enum .Enum ):
1010 default = "default"
11+ nonunicode = "nonunicode"
1112 python = "python"
1213
1314
@@ -31,23 +32,47 @@ def __init__(self, variant: RegexVariantName) -> None:
3132 self .variant = variant
3233
3334 if self .variant == RegexVariantName .default :
34- self ._real_implementation = _RegressImplementation ()
35+ self ._real_implementation = _UnicodeRegressImplementation ()
36+ elif self .variant == RegexVariantName .nonunicode :
37+ self ._real_implementation = _NonunicodeRegressImplementation ()
3538 else :
3639 self ._real_implementation = _PythonImplementation ()
3740
3841 self .check_format = self ._real_implementation .check_format
3942 self .pattern_keyword = self ._real_implementation .pattern_keyword
4043
4144
42- class _RegressImplementation :
45+ class _UnicodeRegressImplementation :
46+ def check_format (self , instance : t .Any ) -> bool :
47+ if not isinstance (instance , str ):
48+ return True
49+ try :
50+ regress .Regex (instance , flags = "u" )
51+ except regress .RegressError :
52+ return False
53+ return True
54+
55+ def pattern_keyword (
56+ self , validator : t .Any , pattern : str , instance : str , schema : t .Any
57+ ) -> t .Iterator [jsonschema .ValidationError ]:
58+ if not validator .is_type (instance , "string" ):
59+ return
60+
61+ try :
62+ regress_pattern = regress .Regex (pattern , flags = "u" )
63+ except regress .RegressError :
64+ yield jsonschema .ValidationError (f"pattern { pattern !r} failed to compile" )
65+ if not regress_pattern .find (instance ):
66+ yield jsonschema .ValidationError (f"{ instance !r} does not match { pattern !r} " )
67+
68+
69+ class _NonunicodeRegressImplementation :
4370 def check_format (self , instance : t .Any ) -> bool :
4471 if not isinstance (instance , str ):
4572 return True
4673 try :
4774 regress .Regex (instance )
48- # something is wrong with RegressError getting into the published types
49- # needs investigation... for now, ignore the error
50- except regress .RegressError : # type: ignore[attr-defined]
75+ except regress .RegressError :
5176 return False
5277 return True
5378
@@ -59,7 +84,7 @@ def pattern_keyword(
5984
6085 try :
6186 regress_pattern = regress .Regex (pattern )
62- except regress .RegressError : # type: ignore[attr-defined]
87+ except regress .RegressError :
6388 yield jsonschema .ValidationError (f"pattern { pattern !r} failed to compile" )
6489 if not regress_pattern .find (instance ):
6590 yield jsonschema .ValidationError (f"{ instance !r} does not match { pattern !r} " )
0 commit comments