Skip to content
Draft
21 changes: 11 additions & 10 deletions py/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# This is a copy of https://github.com/HyperionGray/python-chrome-devtools-protocol/blob/master/generator/generate.py
# This is a modified copy of:
# - https://github.com/HyperionGray/python-chrome-devtools-protocol/blob/master/generator/generate.py
# The license above is theirs and MUST be preserved.

# flake8: noqa
# flake8: noqa: E501

import builtins
from dataclasses import dataclass
Expand All @@ -36,7 +37,7 @@
from pathlib import Path
import re
from textwrap import dedent, indent as tw_indent
from typing import Optional , cast, List, Union, Iterator
from typing import Optional, List, Union, Iterator, cast

import inflection # type: ignore

Expand Down Expand Up @@ -389,12 +390,12 @@ def generate_enum_code(self):
CDP metadata.
'''
def_to_json = dedent('''\
def to_json(self):
def to_json(self) -> str:
return self.value''')

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

code = f'class {self.id}(enum.Enum):\n'
Expand Down Expand Up @@ -435,8 +436,8 @@ class {self.id}:\n''')
# Emit to_json() method. The properties are sorted in the same order as
# above for readability.
def_to_json = dedent('''\
def to_json(self):
json = dict()
def to_json(self) -> T_JSON_DICT:
json: T_JSON_DICT = dict()
''')
assigns = (p.generate_to_json(dict_='json') for p in props)
def_to_json += indent('\n'.join(assigns), 4)
Expand All @@ -446,9 +447,9 @@ def to_json(self):

# Emit from_json() method. The properties are sorted in the same order
# as above for readability.
def_from_json = dedent('''\
def_from_json = dedent(f'''\
@classmethod
def from_json(cls, json):
def from_json(cls, json: T_JSON_DICT) -> {self.id}:
return cls(
''')
from_jsons = []
Expand Down
Loading
Loading