Skip to content

Commit 4881346

Browse files
committed
Update annotations (fixes #11)
Annotations are no longer quoted. Instead we use from __future__ import annotations. Tests are updated to match and all tests pass.
1 parent 3095345 commit 4881346

3 files changed

Lines changed: 35 additions & 33 deletions

File tree

generator/generate.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
CDP {{}} Domain{{}}
3232
\'\'\'
3333
34+
from __future__ import annotations
3435
from cdp.util import event_class, T_JSON_DICT
3536
from dataclasses import dataclass
3637
import enum
@@ -153,14 +154,14 @@ def py_annotation(self) -> str:
153154
if self.items:
154155
if self.items.ref:
155156
py_ref = ref_to_python(self.items.ref)
156-
ann = "typing.List['{}']".format(py_ref)
157+
ann = "typing.List[{}]".format(py_ref)
157158
else:
158159
ann = 'typing.List[{}]'.format(
159160
CdpPrimitiveType.get_annotation(self.items.type))
160161
else:
161162
if self.ref:
162163
py_ref = ref_to_python(self.ref)
163-
ann = f"'{py_ref}'"
164+
ann = py_ref
164165
else:
165166
ann = CdpPrimitiveType.get_annotation(
166167
typing.cast(str, self.type))
@@ -216,7 +217,7 @@ def generate_to_json(self, dict_: str, use_self: bool=True) -> str:
216217

217218
def generate_from_json(self, dict_) -> str:
218219
''' Generate the code that creates an instance from a JSON dict named
219-
``json``. '''
220+
``dict_``. '''
220221
if self.items:
221222
if self.items.ref:
222223
py_ref = ref_to_python(self.items.ref)
@@ -273,10 +274,9 @@ def generate_primitive_code(self) -> str:
273274
if self.items:
274275
if self.items.ref:
275276
nested_type = ref_to_python(self.items.ref)
276-
py_type = f"typing.List['{nested_type}']"
277277
else:
278278
nested_type = CdpPrimitiveType.get_annotation(self.items.type)
279-
py_type = f'typing.List[{nested_type}]'
279+
py_type = f'typing.List[{nested_type}]'
280280
superclass = 'list'
281281
else:
282282
# A primitive type cannot have a ref, so there is no branch here.
@@ -295,7 +295,7 @@ def to_json(self) -> {py_type}:
295295

296296
def_from_json = dedent(f'''\
297297
@classmethod
298-
def from_json(cls, json: {py_type}) -> '{self.id}':
298+
def from_json(cls, json: {py_type}) -> {self.id}:
299299
return cls(json)''')
300300
code += '\n\n' + indent(def_from_json, 4)
301301

@@ -321,7 +321,7 @@ def to_json(self) -> str:
321321

322322
def_from_json = dedent(f'''\
323323
@classmethod
324-
def from_json(cls, json: str) -> '{self.id}':
324+
def from_json(cls, json: str) -> {self.id}:
325325
return cls(json)''')
326326

327327
code = f'class {self.id}(enum.Enum):\n'
@@ -376,7 +376,7 @@ def to_json(self) -> T_JSON_DICT:
376376
# as above for readability.
377377
def_from_json = dedent(f'''\
378378
@classmethod
379-
def from_json(cls, json: T_JSON_DICT) -> '{self.id}':
379+
def from_json(cls, json: T_JSON_DICT) -> {self.id}:
380380
return cls(
381381
''')
382382
from_jsons = list()
@@ -418,13 +418,13 @@ def generate_code(self) -> str:
418418
if self.items:
419419
if self.items.ref:
420420
nested_type = ref_to_python(self.items.ref)
421-
py_type = f"typing.List['{nested_type}']"
421+
py_type = f"typing.List[{nested_type}]"
422422
else:
423423
nested_type = CdpPrimitiveType.get_annotation(self.items.type)
424424
py_type = f'typing.List[{nested_type}]'
425425
else:
426426
if self.ref:
427-
py_type = "'{}'".format(ref_to_python(self.ref))
427+
py_type = "{}".format(ref_to_python(self.ref))
428428
else:
429429
py_type = CdpPrimitiveType.get_annotation(
430430
typing.cast(str, self.type))
@@ -469,14 +469,14 @@ def py_annotation(self):
469469
if self.items:
470470
if self.items.ref:
471471
py_ref = ref_to_python(self.items.ref)
472-
ann = f"typing.List['{py_ref}']"
472+
ann = f"typing.List[{py_ref}]"
473473
else:
474474
py_type = CdpPrimitiveType.get_annotation(self.items.type)
475475
ann = f'typing.List[{py_type}]'
476476
else:
477477
if self.ref:
478478
py_ref = ref_to_python(self.ref)
479-
ann = f"'{py_ref}'"
479+
ann = f"{py_ref}"
480480
else:
481481
ann = CdpPrimitiveType.get_annotation(self.type)
482482
if self.optional:
@@ -660,7 +660,7 @@ class {self.py_name}:''')
660660

661661
if self.deprecated:
662662
code = f'@deprecated(version="{current_version}")\n' + code
663-
663+
664664
code += '\n'
665665
if self.description:
666666
code += indent(docstring(self.description), 4)
@@ -670,7 +670,7 @@ class {self.py_name}:''')
670670
code += '\n\n'
671671
def_from_json = dedent(f'''\
672672
@classmethod
673-
def from_json(cls, json: T_JSON_DICT) -> '{self.py_name}':
673+
def from_json(cls, json: T_JSON_DICT) -> {self.py_name}:
674674
return cls(
675675
''')
676676
code += indent(def_from_json, 4)

generator/test_generate.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def to_json(self) -> str:
5454
return self
5555
5656
@classmethod
57-
def from_json(cls, json: str) -> 'AXNodeId':
57+
def from_json(cls, json: str) -> AXNodeId:
5858
return cls(json)
5959
6060
def __repr__(self):
@@ -81,11 +81,11 @@ class ArrayOfStrings(list):
8181
'''
8282
Index of the string in the strings table.
8383
'''
84-
def to_json(self) -> typing.List['StringIndex']:
84+
def to_json(self) -> typing.List[StringIndex]:
8585
return self
8686
8787
@classmethod
88-
def from_json(cls, json: typing.List['StringIndex']) -> 'ArrayOfStrings':
88+
def from_json(cls, json: typing.List[StringIndex]) -> ArrayOfStrings:
8989
return cls(json)
9090
9191
def __repr__(self):
@@ -128,7 +128,7 @@ def to_json(self) -> str:
128128
return self.value
129129
130130
@classmethod
131-
def from_json(cls, json: str) -> 'AXValueSourceType':
131+
def from_json(cls, json: str) -> AXValueSourceType:
132132
return cls(json)""")
133133

134134
type = CdpType.from_json(json_type)
@@ -182,16 +182,16 @@ class AXValue:
182182
A single computed AX property.
183183
'''
184184
#: The type of this value.
185-
type: 'AXValueType'
185+
type: AXValueType
186186
187187
#: The computed value of this property.
188188
value: typing.Optional[typing.Any] = None
189189
190190
#: One or more related nodes, if applicable.
191-
related_nodes: typing.Optional[typing.List['AXRelatedNode']] = None
191+
related_nodes: typing.Optional[typing.List[AXRelatedNode]] = None
192192
193193
#: The sources which contributed to the computation of this property.
194-
sources: typing.Optional[typing.List['AXValueSource']] = None
194+
sources: typing.Optional[typing.List[AXValueSource]] = None
195195
196196
def to_json(self) -> T_JSON_DICT:
197197
json: T_JSON_DICT = dict()
@@ -205,7 +205,7 @@ def to_json(self) -> T_JSON_DICT:
205205
return json
206206
207207
@classmethod
208-
def from_json(cls, json: T_JSON_DICT) -> 'AXValue':
208+
def from_json(cls, json: T_JSON_DICT) -> AXValue:
209209
return cls(
210210
type=AXValueType.from_json(json['type']),
211211
value=json['value'] if 'value' in json else None,
@@ -264,11 +264,11 @@ def test_cdp_command():
264264
}
265265
expected = dedent("""\
266266
def get_partial_ax_tree(
267-
node_id: typing.Optional['dom.NodeId'] = None,
268-
backend_node_id: typing.Optional['dom.BackendNodeId'] = None,
269-
object_id: typing.Optional['runtime.RemoteObjectId'] = None,
267+
node_id: typing.Optional[dom.NodeId] = None,
268+
backend_node_id: typing.Optional[dom.BackendNodeId] = None,
269+
object_id: typing.Optional[runtime.RemoteObjectId] = None,
270270
fetch_relatives: typing.Optional[bool] = None
271-
) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,typing.List['AXNode']]:
271+
) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,typing.List[AXNode]]:
272272
'''
273273
Fetches the accessibility node and partial accessibility tree for this DOM node, if it exists.
274274
@@ -467,7 +467,7 @@ def test_cdp_command_ref_parameter():
467467
expected = dedent("""\
468468
def resolve_animation(
469469
animation_id: str
470-
) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,'runtime.RemoteObject']:
470+
) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,runtime.RemoteObject]:
471471
'''
472472
Gets the remote object of the Animation.
473473
@@ -544,7 +544,7 @@ def test_cdp_command_multiple_return():
544544
}
545545
expected = dedent("""\
546546
def get_encoded_response(
547-
request_id: 'network.RequestId',
547+
request_id: network.RequestId,
548548
encoding: str,
549549
quality: typing.Optional[float] = None,
550550
size_only: typing.Optional[bool] = None
@@ -615,8 +615,8 @@ def test_cdp_command_array_of_ref_parameter():
615615
expected = dedent("""\
616616
def grant_permissions(
617617
origin: str,
618-
permissions: typing.List['PermissionType'],
619-
browser_context_id: typing.Optional['target.BrowserContextID'] = None
618+
permissions: typing.List[PermissionType],
619+
browser_context_id: typing.Optional[target.BrowserContextID] = None
620620
) -> typing.Generator[T_JSON_DICT,T_JSON_DICT,None]:
621621
'''
622622
Grant specific permissions to the given origin and reject all others.
@@ -666,10 +666,10 @@ class RecordingStateChanged:
666666
Called when the recording state for the service has been updated.
667667
'''
668668
is_recording: bool
669-
service: 'ServiceName'
669+
service: ServiceName
670670
671671
@classmethod
672-
def from_json(cls, json: T_JSON_DICT) -> 'RecordingStateChanged':
672+
def from_json(cls, json: T_JSON_DICT) -> RecordingStateChanged:
673673
return cls(
674674
is_recording=bool(json['isRecording']),
675675
service=ServiceName.from_json(json['service'])
@@ -730,7 +730,7 @@ class WindowOpen:
730730
user_gesture: bool
731731
732732
@classmethod
733-
def from_json(cls, json: T_JSON_DICT) -> 'WindowOpen':
733+
def from_json(cls, json: T_JSON_DICT) -> WindowOpen:
734734
return cls(
735735
url=str(json['url']),
736736
window_name=str(json['windowName']),

mypy.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[mypy]
2+
disallow_any_decorated=False

0 commit comments

Comments
 (0)