Skip to content

Commit

Permalink
[*.py] Use PEP589 syntax to describe IR
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelMarks committed Dec 2, 2023
1 parent 5bda68b commit daef4ad
Show file tree
Hide file tree
Showing 31 changed files with 511 additions and 431 deletions.
16 changes: 9 additions & 7 deletions cdd/argparse_function/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ def argparse_function(
"""
Convert to an argparse FunctionDef
:param intermediate_repr: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:param intermediate_repr: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:type intermediate_repr: ```dict```
:param emit_default_doc: Whether help/docstring should include 'With default' text
Expand Down
16 changes: 9 additions & 7 deletions cdd/argparse_function/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ def argparse_ast(
:param word_wrap: Whether to word-wrap. Set `DOCTRANS_LINE_LENGTH` to configure length.
:type word_wrap: ```bool```
:return: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:return: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:rtype: ```dict```
"""
assert isinstance(
Expand Down
16 changes: 9 additions & 7 deletions cdd/argparse_function/utils/emit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ def _parse_return(e, intermediate_repr, function_def, emit_default_doc):
:param e: Return AST node
:type e: Return
:param intermediate_repr: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:param intermediate_repr: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:type intermediate_repr: ```dict```
:param function_def: AST node for function definition
Expand Down
16 changes: 9 additions & 7 deletions cdd/class_/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ def class_(
"""
Construct a class
:param intermediate_repr: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:param intermediate_repr: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:type intermediate_repr: ```dict```
:param emit_call: Whether to emit a `__call__` method from the `_internal` IR subdict
Expand Down
66 changes: 38 additions & 28 deletions cdd/class_/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from functools import partial
from itertools import filterfalse
from operator import setitem
from typing import Optional, TypedDict, Any, Literal

import cdd.docstring.parse
import cdd.function.parse
Expand Down Expand Up @@ -57,15 +58,18 @@ def class_(
:param word_wrap: Whether to word-wrap. Set `DOCTRANS_LINE_LENGTH` to configure length.
:type word_wrap: ```bool```
:return: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:return: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:rtype: ```dict```
"""

assert not isinstance(class_def, FunctionDef), "Expected not `FunctionDef`"
is_supported_ast_node = isinstance(class_def, (Module, ClassDef))
if not is_supported_ast_node and isinstance(class_def, type):
Expand Down Expand Up @@ -254,13 +258,15 @@ def _class_from_memory(
:param word_wrap: Whether to word-wrap. Set `DOCTRANS_LINE_LENGTH` to configure length.
:type word_wrap: ```bool```
:return: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:return: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:rtype: ```dict```
"""
ir = cdd.shared.parse.utils.parser_utils._inspect(
Expand Down Expand Up @@ -322,25 +328,29 @@ def _merge_inner_function(
:param infer_type: Whether to try inferring the typ (from the default)
:type infer_type: ```bool```
:param intermediate_repr: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:param intermediate_repr: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:type intermediate_repr: ```dict```
:param merge_inner_function: Name of inner function to merge. If None, merge nothing.
:type merge_inner_function: ```Optional[str]```
:return: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:return: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:rtype: ```dict```
"""
function_def = next(
Expand Down
16 changes: 9 additions & 7 deletions cdd/compound/exmod_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,15 @@ def _emit_symbol(
:param init_filepath: The filepath of the __init__.py file
:type init_filepath: ```str```
:param intermediate_repr: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:param intermediate_repr: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:type intermediate_repr: ```dict```
:param isfile_emit_filename: Whether the emit filename exists
Expand Down
32 changes: 18 additions & 14 deletions cdd/compound/openapi/utils/emit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,25 +373,29 @@ def ensure_has_primary_key(intermediate_repr, force_pk_id=False):
"""
Add a primary key to the input (if nonexistent) then return the input.
:param intermediate_repr: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:param intermediate_repr: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:type intermediate_repr: ```dict```
:param force_pk_id: Whether to force primary_key to be named `id` (if there isn't already a primary_key)
:type force_pk_id: ```bool```
:return: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:return: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:rtype: ```dict```
"""
params = (
Expand Down
16 changes: 9 additions & 7 deletions cdd/docstring/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ def docstring(
"""
Converts an IR to a docstring
:param intermediate_repr: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:param intermediate_repr: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:type intermediate_repr: ```dict```
:param docstring_format: Format of docstring
Expand Down
16 changes: 9 additions & 7 deletions cdd/function/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ def function(
"""
Construct a function from our IR
:param intermediate_repr: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:param intermediate_repr: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:type intermediate_repr: ```dict```
:param function_name: name of function_def
Expand Down
16 changes: 9 additions & 7 deletions cdd/function/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ def function(
:param function_name: name of function_def
:type function_name: ```str```
:return: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:return: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:rtype: ```dict```
"""
if isinstance(function_def, FunctionType):
Expand Down
32 changes: 18 additions & 14 deletions cdd/function/utils/parse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@ def _interpolate_return(function_def, intermediate_repr):
:param function_def: function definition
:type function_def: ```FunctionDef```
:param intermediate_repr: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:param intermediate_repr: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:type intermediate_repr: ```dict```
:return: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:return: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:rtype: ```dict```
"""
return_ast = next(
Expand Down
16 changes: 9 additions & 7 deletions cdd/json_schema/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ def json_schema(
"""
Construct a JSON schema dict
:param intermediate_repr: a dictionary of form
{ "name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}]
"returns": Optional[OrderedDict[Literal['return_type'],
{'typ': str, 'doc': Optional[str], 'default': Any}),)]] }
:param intermediate_repr: a dictionary consistent with `IntermediateRepr`, defined as:
ParamVal = TypedDict("ParamVal", {"typ": str, "doc": Optional[str], "default": Any})
IntermediateRepr = TypedDict("IntermediateRepr", {
"name": Optional[str],
"type": Optional[str],
"doc": Optional[str],
"params": OrderedDict[str, ParamVal],
"returns": Optional[OrderedDict[Literal["return_type"], ParamVal]],
})
:type intermediate_repr: ```dict```
:param identifier: The `$id` of the schema
Expand Down
Loading

0 comments on commit daef4ad

Please sign in to comment.