Skip to content

Commit

Permalink
linter file cleanups, and upgrading mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
AsherGlick committed Sep 30, 2023
1 parent aad17db commit ec70b05
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
5 changes: 3 additions & 2 deletions xml_converter/generators/code_generator.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from jsonschema import validate # type:ignore
from jsonschema.exceptions import ValidationError # type:ignore
import yaml
import frontmatter # type:ignore
from typing import Any, Dict, List, Tuple, Set, Optional, Final
import os
import markdown
from dataclasses import dataclass, field
from jinja2 import Template, FileSystemLoader, Environment
from jinja_helpers import UnindentBlocks

from schema import schema


SchemaType = Dict[str, Any]


def validate_front_matter_schema(front_matter: Any) -> str:
try:
validate(front_matter, schema)
Expand Down
3 changes: 1 addition & 2 deletions xml_converter/generators/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ jsonschema==4.7.2
Markdown==3.4.1
MarkupSafe==2.1.1
mccabe==0.6.1
mypy==0.971
mypy-extensions==0.4.3
mypy==1.5.1
pyaml==21.10.1
pycodestyle==2.8.0
pyflakes==2.4.0
Expand Down
66 changes: 49 additions & 17 deletions xml_converter/generators/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from jsonschema import validate # type:ignore
from jsonschema.exceptions import ValidationError # type:ignore
from typing import Any, Optional, List, Dict, Iterable, TypedDict, Literal, Union, Tuple
import yaml
from typing import Optional, List, Dict, Iterable, TypedDict, Literal, Union, Tuple


################################################################################
Expand All @@ -14,18 +11,22 @@
"ArrayDef",
"ObjectDef",
"PatternDictionaryDef"
];
]


################################################################################
# String Definition Helpers
#
#
#
################################################################################
class BaseStringDef(TypedDict):
type: Literal["string"]


class StringDef(BaseStringDef, total=False):
pattern: Optional[str]


def string_t(pattern: Optional[str] = None) -> StringDef:
if pattern is None:
return {
Expand All @@ -37,36 +38,48 @@ def string_t(pattern: Optional[str] = None) -> StringDef:
"pattern": pattern,
}


class BooleanDef(TypedDict):
type: Literal["boolean"]


def boolean_t() -> BooleanDef:
return {
"type": "boolean"
}


class EnumDef(TypedDict):
type: Literal["string"]
enum: List[str]


def enum_t(options: Iterable[str]) -> EnumDef:
return {
"type": "string",
"enum": list(options)
}


class ArrayDef(TypedDict):
type: Literal["array"]
items: DefType


def array_t(element_type: DefType) -> ArrayDef:
return {
"type": "array",
"items": element_type
}


class ObjectDef(TypedDict):
type: Literal["object"]
additionalProperties: Literal[False]
required: List[str]
properties: Dict[str, DefType]


def object_t(fields: Dict[str, DefType], optional_fields: Dict[str, DefType] = {}) -> ObjectDef:
return {
"type": "object",
Expand All @@ -75,9 +88,12 @@ def object_t(fields: Dict[str, DefType], optional_fields: Dict[str, DefType] = {
"properties": {**fields, **optional_fields}
}


class PatternDictionaryDef(TypedDict):
type: Literal["object"]
patternProperties: Dict[str, DefType]


def pattern_dictionary_t(pattern_properties: Dict[str, DefType]) -> PatternDictionaryDef:
return {
"type": "object",
Expand All @@ -86,14 +102,34 @@ def pattern_dictionary_t(pattern_properties: Dict[str, DefType]) -> PatternDicti


# Helper function for the union types
def union_partial_t(*, required: Dict[str, DefType]={}, optional: Dict[str, DefType]={}) ->Tuple[Dict[str, DefType], Dict[str, DefType]]:
def union_partial_t(
*,
required: Dict[str, DefType] = {},
optional: Dict[str, DefType] = {}
) -> Tuple[Dict[str, DefType], Dict[str, DefType]]:
return (required, optional)
upt=union_partial_t


class UnionBranchThenDef(TypedDict):
additionalProperties: Literal[False]
required: List[str]
properties: Dict[str, DefType]


UnionBranchDef = TypedDict('UnionBranchDef', {
'if': Dict[Literal["properties"], Dict[Literal["type"], Dict[Literal["const"], str]]],
'then': UnionBranchThenDef,
})


def union_t(options: Dict[str, Tuple[Dict[str, DefType], Dict[str, DefType]]]):
union_type = {
class UnionDef(TypedDict):
type: Literal["object"]
properties: Dict[Literal["type"], EnumDef]
allOf: List[UnionBranchDef]


def union_t(options: Dict[str, Tuple[Dict[str, DefType], Dict[str, DefType]]]) -> UnionDef:
union_type: UnionDef = {
"type": "object",
"properties": {
"type": enum_t(options.keys())
Expand All @@ -113,7 +149,8 @@ def union_t(options: Dict[str, Tuple[Dict[str, DefType], Dict[str, DefType]]]):
"properties": {**value[0], **value[1]}
}
}
for key, value in options.items()]
for key, value in options.items()
]
}

return union_type
Expand All @@ -136,7 +173,7 @@ def union_t(options: Dict[str, Tuple[Dict[str, DefType], Dict[str, DefType]]]):
"String": union_partial_t(required=shared_field_properties),
"Boolean": union_partial_t(required=shared_field_properties),
"MultiflagValue": union_partial_t(
required = {**shared_field_properties, **{
required={**shared_field_properties, **{
"flags": pattern_dictionary_t({"^[a-z_]+$": array_t(string_t())}),
}},
),
Expand Down Expand Up @@ -184,8 +221,3 @@ def union_t(options: Dict[str, Tuple[Dict[str, DefType], Dict[str, DefType]]]):
}
),
})





0 comments on commit ec70b05

Please sign in to comment.