Skip to content

Commit

Permalink
Replace | with optional/union
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro committed Jan 22, 2024
1 parent 5bce387 commit b620979
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 46 deletions.
10 changes: 5 additions & 5 deletions nbs/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
" \"\"\"\n",
"\n",
" def __init__(self) -> None:\n",
" self.parent: \"WithChildrenMixin\" | None = None\n",
" self.parent: Optional[\"WithChildrenMixin\"] = None\n",
" self.children: Sequence[\"WithChildrenMixin\"] = []\n",
"\n",
" def __len__(self) -> int:\n",
Expand All @@ -105,7 +105,7 @@
" \"\"\"\n",
" Add a child element to the children list and set its parent to self.\n",
" \"\"\"\n",
" self.children.append(child) # type: ignore[attr-defined]\n",
" self.children.append(child) # type: ignore[attr-defined]\n",
" child.set_parent(self)\n",
" return child\n",
"\n",
Expand All @@ -116,7 +116,7 @@
" self.parent = parent\n",
"\n",
" def __iter__(self) -> Generator:\n",
" def iter_data(obj: \"WithChildrenMixin\", level: int | None = 0) -> Generator:\n",
" def iter_data(obj: \"WithChildrenMixin\", level: Optional[int] = 0) -> Generator:\n",
" \"\"\"Simply yields parent and then children\"\"\"\n",
" yield obj, level\n",
" for child in obj.children:\n",
Expand Down Expand Up @@ -150,7 +150,7 @@
" def __init__(\n",
" self,\n",
" name: str, # Name of this element\n",
" attrs: dict[str, Any] | None = None, # Attributes for this element\n",
" attrs: Optional[dict[str, Any]] = None, # Attributes for this element\n",
" ) -> None:\n",
" \"\"\"\n",
" Initialize Data object.\n",
Expand Down Expand Up @@ -484,7 +484,7 @@
"outputs": [],
"source": [
"# | export\n",
"def map_data(obj: Data, process: Callable, level: int | None=0) -> MappedData:\n",
"def map_data(obj: Data, process: Callable, level: Optional[int] = 0) -> MappedData:\n",
" \"\"\"Maps over a `Data` inst returning `MappedData` instances\"\"\"\n",
" child_results = [map_data(c, process, level=(level or 0) + 1) for c in obj.children]\n",
" value = process(obj, level)\n",
Expand Down
37 changes: 25 additions & 12 deletions nbs/02_codegen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"import tempfile\n",
"import nbdev.showdoc as showdoc\n",
"from fastcore.test import *\n",
"from typing import Optional, Generator\n",
"from typing import Generator\n",
"from jinja2 import UndefinedError\n",
"from textwrap import dedent\n",
"\n",
Expand Down Expand Up @@ -58,6 +58,7 @@
"from dataclasses import dataclass\n",
"from pathlib import Path\n",
"from textwrap import dedent\n",
"from typing import Optional\n",
"\n",
"from sal.core import Data\n",
"from sal.loaders import xml_file_to_data\n",
Expand Down Expand Up @@ -102,13 +103,15 @@
"metadata": {},
"outputs": [],
"source": [
"struct: Data = xml_to_data(\"\"\"\n",
"struct: Data = xml_to_data(\n",
" \"\"\"\n",
" <model name=\"User\">\n",
" <field name=\"id\" type=\"integer\"/>\n",
" <field name=\"username\" type=\"char\"/>\n",
" <field name=\"email\" type=\"email\"/>\n",
" </model>\n",
"\"\"\")"
"\"\"\"\n",
")"
]
},
{
Expand Down Expand Up @@ -161,19 +164,23 @@
"source": [
"# | exporti\n",
"\n",
"\n",
"@dataclass\n",
"class WriteFileResult:\n",
" to: str\n",
" content: str\n",
"\n",
"\n",
"class SalAction(abc.ABC):\n",
" @property\n",
" @abc.abstractmethod\n",
" def name(self) -> str:\n",
" pass\n",
"\n",
" @abc.abstractmethod\n",
" def process_data(self, sal: \"Sal\", data: Data) -> tuple[str, WriteFileResult | None]:\n",
" def process_data(\n",
" self, sal: \"Sal\", data: Data\n",
" ) -> tuple[str, Optional[WriteFileResult]]:\n",
" pass\n",
"\n",
" def __str__(self) -> str:\n",
Expand Down Expand Up @@ -211,10 +218,12 @@
"source": [
"# | export\n",
"\n",
"\n",
"class Config(BaseModel):\n",
" template_directories: list[Path]\n",
" filters: dict[str, Callable] = {}\n",
"\n",
"\n",
"class Sal:\n",
" def __init__(self, config: Config, renderer: Renderer):\n",
" self.config = config\n",
Expand All @@ -234,7 +243,7 @@
" d.attrs.update(new_attributes)\n",
" return data\n",
"\n",
" def process_data(self, data: Data) -> str | Any:\n",
" def process_data(self, data: Data) -> Optional[str]:\n",
" try:\n",
" for action in self.actions:\n",
" if data.name == action.name:\n",
Expand All @@ -246,7 +255,8 @@
" except MissingTemplateException as e:\n",
" path = Path(self.config.template_directories[0]) / f\"{e.name}.jinja2\"\n",
" raise RuntimeError(\n",
" dedent(f\"\"\"\n",
" dedent(\n",
" f\"\"\"\n",
" The template `{e.name}` was not found. Here's a default template to \n",
" get you started:\n",
" \n",
Expand All @@ -255,10 +265,11 @@
" ---\n",
" at: {path}\n",
"\n",
" \"\"\").strip()\n",
" \"\"\"\n",
" ).strip()\n",
" )\n",
"\n",
" def process_xml_from_filename(self, file: str) -> str | Any:\n",
" def process_xml_from_filename(self, file: str) -> Optional[str]:\n",
" struct: Data = xml_file_to_data(file)\n",
" return self.process(struct)\n",
"\n",
Expand All @@ -271,10 +282,10 @@
" else:\n",
" raise RuntimeError(f\"Unsupported action {action_result}\")\n",
"\n",
" def process(self, data: Data) -> str | Any:\n",
" def process(self, data: Data) -> Optional[str]:\n",
" return self._process(data)\n",
"\n",
" def _process(self, data: Data) -> str | Any:\n",
" def _process(self, data: Data) -> Optional[str]:\n",
" data = self.pre_process_data(data)\n",
" result = self.process_data(data)\n",
" self.process_action_results()\n",
Expand All @@ -285,9 +296,11 @@
" cls,\n",
" *,\n",
" template_directories: list[Path],\n",
" filters: dict[str, Callable] | None = None\n",
" filters: Optional[dict[str, Callable]] = None,\n",
" ) -> \"Sal\":\n",
" config = Config(template_directories=template_directories, filters=filters or {})\n",
" config = Config(\n",
" template_directories=template_directories, filters=filters or {}\n",
" )\n",
"\n",
" repository = TemplateLoader.from_directories(config.template_directories)\n",
"\n",
Expand Down
1 change: 0 additions & 1 deletion nbs/03_cli.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"import click\n",
"from pathlib import Path\n",
"from typing import Any\n",
"\n",
"from sal.codegen import Sal\n",
"from sal.utils import is_notebook"
]
Expand Down
4 changes: 3 additions & 1 deletion nbs/99_frontmatter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"# | export\n",
"from typing import Any\n",
"from textwrap import dedent\n",
"from typing import Optional\n",
"from frontmatter.util import u # type: ignore[import-untyped]\n",
"from frontmatter.default_handlers import YAMLHandler # type: ignore[import-untyped]"
]
Expand Down Expand Up @@ -76,8 +77,9 @@
"source": [
"# | hide\n",
"# | export\n",
"\n",
"class FrontMatter:\n",
" def __init__(self, h: YAMLHandler | None = None): # type: ignore[no-any-unimported]\n",
" def __init__(self, h: Optional[YAMLHandler] = None): # type: ignore[no-any-unimported] # noqa: E501\n",
" if h is None:\n",
" h = YAMLHandler()\n",
" self.handler = h\n",
Expand Down
25 changes: 14 additions & 11 deletions nbs/99_templates.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"source": [
"# | exporti\n",
"from pathlib import Path\n",
"from typing import Any, Optional\n",
"from typing import Any, Optional, Union\n",
"from jinja2 import (\n",
" Environment,\n",
" BaseLoader,\n",
Expand Down Expand Up @@ -193,10 +193,17 @@
"# | exporti\n",
"MissingTemplateException = TemplateNotFound\n",
"\n",
"\n",
"class TemplateLoader:\n",
" def __init__(self, templates: dict[str, str] | None=None, folders: list[Path] | None = None):\n",
" def __init__(\n",
" self,\n",
" templates: Optional[dict[str, str]] = None,\n",
" folders: Optional[list[Path]] = None,\n",
" ):\n",
" self.frontmatter_handler = FrontMatter()\n",
" loaders: list[DictLoader|FileSystemLoader] = [DictLoader(templates or {})]\n",
" loaders: list[Union[DictLoader, FileSystemLoader]] = [\n",
" DictLoader(templates or {})\n",
" ]\n",
" if folders:\n",
" for folder in folders:\n",
" loaders.append(FileSystemLoader(folder))\n",
Expand All @@ -215,7 +222,7 @@
"\n",
" @classmethod\n",
" def from_directories(cls, directories: list[Path]) -> \"TemplateLoader\":\n",
" return cls(folders=directories) "
" return cls(folders=directories)"
]
},
{
Expand Down Expand Up @@ -270,7 +277,7 @@
" *,\n",
" renderer: TemplateRenderer,\n",
" repository: TemplateLoader,\n",
" filters: dict | None = None\n",
" filters: Optional[dict] = None\n",
" ):\n",
" self.renderer = renderer\n",
" self.repository = repository\n",
Expand All @@ -297,15 +304,11 @@
" return self.repository.get_source(*args, **kwargs)\n",
"\n",
" def get_metadata_for_template(self, path: str, data: Data) -> dict:\n",
" template = self.repository.get_frontmatter_source(\n",
" path\n",
" )\n",
" template = self.repository.get_frontmatter_source(path)\n",
"\n",
" rendered = self.render(data, template)\n",
"\n",
" ret: dict = self.repository.frontmatter_handler.parse(\n",
" rendered\n",
" )\n",
" ret: dict = self.repository.frontmatter_handler.parse(rendered)\n",
"\n",
" return ret"
]
Expand Down
1 change: 0 additions & 1 deletion sal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import click
from pathlib import Path
from typing import Any

from .codegen import Sal
from .utils import is_notebook

Expand Down
13 changes: 7 additions & 6 deletions sal/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dataclasses import dataclass
from pathlib import Path
from textwrap import dedent
from typing import Optional

from .core import Data
from .loaders import xml_file_to_data
Expand All @@ -36,7 +37,7 @@ def name(self) -> str:
@abc.abstractmethod
def process_data(
self, sal: "Sal", data: Data
) -> tuple[str, WriteFileResult | None]:
) -> tuple[str, Optional[WriteFileResult]]:
pass

def __str__(self) -> str:
Expand Down Expand Up @@ -89,7 +90,7 @@ def pre_process_data(self, data: Data) -> Data:
d.attrs.update(new_attributes)
return data

def process_data(self, data: Data) -> str | Any:
def process_data(self, data: Data) -> Optional[str]:
try:
for action in self.actions:
if data.name == action.name:
Expand All @@ -115,7 +116,7 @@ def process_data(self, data: Data) -> str | Any:
).strip()
)

def process_xml_from_filename(self, file: str) -> str | Any:
def process_xml_from_filename(self, file: str) -> Optional[str]:
struct: Data = xml_file_to_data(file)
return self.process(struct)

Expand All @@ -128,10 +129,10 @@ def process_action_results(self) -> None:
else:
raise RuntimeError(f"Unsupported action {action_result}")

def process(self, data: Data) -> str | Any:
def process(self, data: Data) -> Optional[str]:
return self._process(data)

def _process(self, data: Data) -> str | Any:
def _process(self, data: Data) -> Optional[str]:
data = self.pre_process_data(data)
result = self.process_data(data)
self.process_action_results()
Expand All @@ -142,7 +143,7 @@ def from_config(
cls,
*,
template_directories: list[Path],
filters: dict[str, Callable] | None = None,
filters: Optional[dict[str, Callable]] = None,
) -> "Sal":
config = Config(
template_directories=template_directories, filters=filters or {}
Expand Down
8 changes: 4 additions & 4 deletions sal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WithChildrenMixin:
"""

def __init__(self) -> None:
self.parent: "WithChildrenMixin" | None = None
self.parent: Optional["WithChildrenMixin"] = None
self.children: Sequence["WithChildrenMixin"] = []

def __len__(self) -> int:
Expand All @@ -40,7 +40,7 @@ def set_parent(self, parent: "WithChildrenMixin") -> None:
self.parent = parent

def __iter__(self) -> Generator:
def iter_data(obj: "WithChildrenMixin", level: int | None = 0) -> Generator:
def iter_data(obj: "WithChildrenMixin", level: Optional[int] = 0) -> Generator:
"""Simply yields parent and then children"""
yield obj, level
for child in obj.children:
Expand All @@ -60,7 +60,7 @@ class Data(WithChildrenMixin):
def __init__(
self,
name: str, # Name of this element
attrs: dict[str, Any] | None = None, # Attributes for this element
attrs: Optional[dict[str, Any]] = None, # Attributes for this element
) -> None:
"""
Initialize Data object.
Expand Down Expand Up @@ -138,7 +138,7 @@ def __init__(self, value: Any):
super().__init__()

# %% ../nbs/00_core.ipynb 36
def map_data(obj: Data, process: Callable, level: int | None = 0) -> MappedData:
def map_data(obj: Data, process: Callable, level: Optional[int] = 0) -> MappedData:
"""Maps over a `Data` inst returning `MappedData` instances"""
child_results = [map_data(c, process, level=(level or 0) + 1) for c in obj.children]
value = process(obj, level)
Expand Down
3 changes: 2 additions & 1 deletion sal/frontmatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
# %% ../nbs/99_frontmatter.ipynb 3
from typing import Any
from textwrap import dedent
from typing import Optional
from frontmatter.util import u # type: ignore[import-untyped]
from frontmatter.default_handlers import YAMLHandler # type: ignore[import-untyped]

# %% ../nbs/99_frontmatter.ipynb 6
class FrontMatter:
def __init__(self, h: YAMLHandler | None = None): # type: ignore[no-any-unimported]
def __init__(self, h: Optional[YAMLHandler] = None): # type: ignore[no-any-unimported] # noqa: E501
if h is None:
h = YAMLHandler()
self.handler = h
Expand Down
Loading

0 comments on commit b620979

Please sign in to comment.