@@ -12,98 +12,54 @@ class RegexVariantName(enum.Enum):
12
12
python = "python"
13
13
14
14
15
- class _ConcreteImplementation (t .Protocol ):
16
- def check_format (self , instance : t .Any ) -> bool : ...
17
-
18
- def pattern_keyword (
19
- self , validator : t .Any , pattern : str , instance : str , schema : t .Any
20
- ) -> t .Iterator [jsonschema .ValidationError ]: ...
21
-
22
- def patternProperties_keyword (
23
- self ,
24
- validator : t .Any ,
25
- patternProperties : dict [str , t .Any ],
26
- instance : dict [str , t .Any ],
27
- schema : t .Any ,
28
- ) -> t .Iterator [jsonschema .ValidationError ]: ...
29
-
30
-
31
15
class RegexImplementation :
32
16
"""
33
17
A high-level interface for getting at the different possible
34
18
implementations of regex behaviors.
35
19
"""
36
20
37
- _real_implementation : _ConcreteImplementation
21
+ _concrete : " _ConcreteImplementation"
38
22
39
23
def __init__ (self , variant : RegexVariantName ) -> None :
40
24
self .variant = variant
41
25
42
26
if self .variant == RegexVariantName .default :
43
- self ._real_implementation = _UnicodeRegressImplementation ()
27
+ self ._concrete = _RegressImplementation ()
44
28
elif self .variant == RegexVariantName .nonunicode :
45
- self ._real_implementation = _NonunicodeRegressImplementation ()
29
+ self ._concrete = _NonunicodeRegressImplementation ()
46
30
else :
47
- self ._real_implementation = _PythonImplementation ()
31
+ self ._concrete = _PythonImplementation ()
48
32
49
- self .check_format = self ._real_implementation .check_format
50
- self .pattern_keyword = self ._real_implementation .pattern_keyword
51
- self .patternProperties_keyword = (
52
- self ._real_implementation .patternProperties_keyword
53
- )
33
+ self .check_format = self ._concrete .check_format
34
+ self .pattern_keyword = self ._concrete .pattern_keyword
35
+ self .patternProperties_keyword = self ._concrete .patternProperties_keyword
54
36
55
37
56
- class _UnicodeRegressImplementation :
57
- def check_format (self , instance : t .Any ) -> bool :
58
- if not isinstance (instance , str ):
59
- return True
60
- try :
61
- regress .Regex (instance , flags = "u" )
62
- except regress .RegressError :
63
- return False
64
- return True
38
+ class _ConcreteImplementation (t .Protocol ):
39
+ def check_format (self , instance : t .Any ) -> bool : ...
65
40
66
41
def pattern_keyword (
67
42
self , validator : t .Any , pattern : str , instance : str , schema : t .Any
68
- ) -> t .Iterator [jsonschema .ValidationError ]:
69
- if not validator .is_type (instance , "string" ):
70
- return
71
-
72
- try :
73
- regress_pattern = regress .Regex (pattern , flags = "u" )
74
- except regress .RegressError :
75
- yield jsonschema .ValidationError (f"pattern { pattern !r} failed to compile" )
76
- if not regress_pattern .find (instance ):
77
- yield jsonschema .ValidationError (f"{ instance !r} does not match { pattern !r} " )
43
+ ) -> t .Iterator [jsonschema .ValidationError ]: ...
78
44
79
45
def patternProperties_keyword (
80
46
self ,
81
47
validator : t .Any ,
82
48
patternProperties : dict [str , t .Any ],
83
49
instance : dict [str , t .Any ],
84
50
schema : t .Any ,
85
- ) -> t .Iterator [jsonschema .ValidationError ]:
86
- if not validator .is_type (instance , "object" ):
87
- return
51
+ ) -> t .Iterator [jsonschema .ValidationError ]: ...
88
52
89
- for pattern , subschema in patternProperties .items ():
90
- regress_pattern = regress .Regex (pattern , flags = "u" )
91
- for k , v in instance .items ():
92
- if regress_pattern .find (k ):
93
- yield from validator .descend (
94
- v ,
95
- subschema ,
96
- path = k ,
97
- schema_path = pattern ,
98
- )
99
53
54
+ class _RegressImplementation :
55
+ def _compile_pattern (self , pattern : str ) -> regress .Regex :
56
+ return regress .Regex (pattern , flags = "u" )
100
57
101
- class _NonunicodeRegressImplementation :
102
58
def check_format (self , instance : t .Any ) -> bool :
103
59
if not isinstance (instance , str ):
104
60
return True
105
61
try :
106
- regress . Regex (instance )
62
+ self . _compile_pattern (instance )
107
63
except regress .RegressError :
108
64
return False
109
65
return True
@@ -115,7 +71,7 @@ def pattern_keyword(
115
71
return
116
72
117
73
try :
118
- regress_pattern = regress . Regex (pattern )
74
+ regress_pattern = self . _compile_pattern (pattern )
119
75
except regress .RegressError :
120
76
yield jsonschema .ValidationError (f"pattern { pattern !r} failed to compile" )
121
77
if not regress_pattern .find (instance ):
@@ -132,7 +88,7 @@ def patternProperties_keyword(
132
88
return
133
89
134
90
for pattern , subschema in patternProperties .items ():
135
- regress_pattern = regress . Regex (pattern )
91
+ regress_pattern = self . _compile_pattern (pattern )
136
92
for k , v in instance .items ():
137
93
if regress_pattern .find (k ):
138
94
yield from validator .descend (
@@ -143,6 +99,11 @@ def patternProperties_keyword(
143
99
)
144
100
145
101
102
+ class _NonunicodeRegressImplementation (_RegressImplementation ):
103
+ def _compile_pattern (self , pattern : str ) -> regress .Regex :
104
+ return regress .Regex (pattern )
105
+
106
+
146
107
class _PythonImplementation :
147
108
def check_format (self , instance : t .Any ) -> bool :
148
109
if not isinstance (instance , str ):
0 commit comments