Skip to content

Commit

Permalink
misc cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Jan 28, 2023
1 parent e44cae1 commit 6bec6d4
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 43 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ EXTRAS=[pycodegen]
PYSOURCES=$(wildcard ${MODULE}/**.py ${MODULE}/avro/*.py ${MODULE}/tests/*.py) setup.py
DEVPKGS=-rdev-requirements.txt -rtest-requirements.txt -rmypy-requirements.txt
COVBASE=coverage run --append
PYTEST_EXTRA ?= -rs

# Updating the Major & Minor version below?
# Don't forget to update setup.py as well
Expand Down Expand Up @@ -181,7 +182,7 @@ mypy_3.6: $(filter-out setup.py,$(PYSOURCES))
MYPYPATH=$$MYPYPATH:mypy-stubs mypy --python-version 3.6 $^

mypyc: $(PYSOURCES)
MYPYPATH=mypy-stubs SCHEMA_SALAD_USE_MYPYC=1 python setup.py test
MYPYPATH=mypy-stubs SCHEMA_SALAD_USE_MYPYC=1 python setup.py test --addopts "${PYTEST_EXTRA}"

mypyi:
MYPYPATH=mypy-stubs SCHEMA_SALAD_USE_MYPYC=1 python setup.py install
Expand Down Expand Up @@ -223,7 +224,7 @@ release:
flake8: FORCE
flake8 $(PYSOURCES)

schema_salad/metaschema.py: schema_salad/codegen_base.py schema_salad/python_codegen_support.py schema_salad/python_codegen.py schema_salad/metaschema
schema_salad/metaschema.py: schema_salad/codegen_base.py schema_salad/python_codegen_support.py schema_salad/python_codegen.py schema_salad/metaschema/*.yml
schema-salad-tool --codegen python schema_salad/metaschema/metaschema.yml > $@

FORCE:
Expand Down
20 changes: 10 additions & 10 deletions schema_salad/avro/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ def __init__(self, atype: str, other_props: Optional[PropsType] = None) -> None:
# Ensure valid ctor args
if not isinstance(atype, str):
raise SchemaParseException(
f"Schema type '{atype}' must be a string, was '{type(atype)}."
f"Schema type {atype!r} must be a string, was {type(atype)!r}."
)
elif atype not in VALID_TYPES:
fail_msg = f"{atype} is not a valid type."
raise SchemaParseException(fail_msg)

# add members
if not hasattr(self, "_props"):
self._props = {} # type: PropsType
self._props: PropsType = {}
self.set_prop("type", atype)
self.type = atype
self._props.update(other_props or {})
Expand Down Expand Up @@ -154,7 +154,7 @@ def validate(val: Optional[str], name: str) -> None:
validate(space_attr, "Space")
validate(default_space, "Default space")

self._full = name_attr # type: Optional[str]
self._full: Optional[str] = name_attr

if name_attr is None or name_attr == "":
return
Expand Down Expand Up @@ -185,7 +185,7 @@ class Names:
"""Track name set and default namespace during parsing."""

def __init__(self, default_namespace: Optional[str] = None) -> None:
self.names = {} # type: Dict[str, NamedSchema]
self.names: Dict[str, NamedSchema] = {}
self.default_namespace = default_namespace

def has_name(self, name_attr: str, space_attr: Optional[str]) -> bool:
Expand Down Expand Up @@ -217,7 +217,7 @@ def add_name(
fail_msg = f"{to_add.fullname} is a reserved type name."
raise SchemaParseException(fail_msg)
elif to_add.fullname in self.names:
fail_msg = f'The name "{to_add.fullname}" is already in use.'
fail_msg = f"The name {to_add.fullname!r} is already in use."
raise SchemaParseException(fail_msg)
elif to_add.fullname is None:
fail_msg = f"{to_add.fullname} is missing, but this is impossible."
Expand Down Expand Up @@ -296,18 +296,18 @@ def __init__(
raise SchemaParseException(fail_msg)

# add members
self._props = {} # type: PropsType
self._props: PropsType = {}
self._has_default = has_default
self._props.update(other_props or {})

if isinstance(atype, str) and names is not None and names.has_name(atype, None):
type_schema = cast(NamedSchema, names.get_name(atype, None)) # type: Schema
type_schema: Schema = cast(NamedSchema, names.get_name(atype, None))
else:
try:
type_schema = make_avsc_object(atype, names)
except Exception as e:
raise SchemaParseException(
f'Type property "{atype}" not a valid Avro schema: {e}'
f"Type property {atype!r} not a valid Avro schema: {e}"
) from e
self.set_prop("type", type_schema)
self.set_prop("name", name)
Expand Down Expand Up @@ -447,7 +447,7 @@ def __init__(
Schema.__init__(self, "union")

# Add class members
schema_objects = [] # type: List[Schema]
schema_objects: List[Schema] = []
for schema in schemas:
if isinstance(schema, str) and names.has_name(schema, None):
new_schema = cast(Schema, names.get_name(schema, None))
Expand Down Expand Up @@ -481,7 +481,7 @@ class RecordSchema(NamedSchema):
@staticmethod
def make_field_objects(field_data: List[PropsType], names: Names) -> List[Field]:
"""We're going to need to make message parameters too."""
field_objects = [] # type: List[Field]
field_objects: List[Field] = []
parsed_fields: Dict[str, PropsType] = {}
for field in field_data:
if hasattr(field, "get") and callable(field.get):
Expand Down
2 changes: 1 addition & 1 deletion schema_salad/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def codegen(
elif lang == "dotnet":
gen = DotNetCodeGen(base, target=target, package=pkg, examples=examples)
else:
raise SchemaSaladException(f"Unsupported code generation language '{lang}'")
raise SchemaSaladException(f"Unsupported code generation language {lang!r}")

gen.prologue()

Expand Down
4 changes: 2 additions & 2 deletions schema_salad/cpp_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def writeImplDefinition(self, target: IO[str], fullInd: str, ind: str) -> None:
for field in self.fields:
fieldname = safename(field.name)
target.write(
f'{fullInd}{ind}addYamlField(n, "{field.name}", toYaml(*{fieldname}));\n'
f'{fullInd}{ind}addYamlField(n, "{field.name}", toYaml(*{fieldname}));\n' # noqa: B907
)
# target.write(f"{fullInd}{ind}addYamlIfNotEmpty(n, \"{field.name}\", toYaml(*{fieldname}));\n")

Expand Down Expand Up @@ -200,7 +200,7 @@ def writeDefinition(self, target: IO[str], ind: str) -> None:
f"{ind}static auto m = std::map<std::string, {name}, std::less<>> {{\n"
)
for v in self.values:
target.write(f'{ind}{ind}{{"{v}", {name}::{safename(v)}}},\n')
target.write(f'{ind}{ind}{{"{v}", {name}::{safename(v)}}},\n') # noqa: B907
target.write(f"{ind}}};\n{ind}out = m.find(v)->second;\n}}\n")

target.write(f"inline auto toYaml({name} v) {{\n")
Expand Down
14 changes: 8 additions & 6 deletions schema_salad/dlang_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def prologue(self) -> None:
self.target.write(
f"""/// parser information
enum parserInfo = "{self.parser_info}";
"""
""" # noqa: B907
)

def epilogue(self, root_loader: TypeDef) -> None:
Expand Down Expand Up @@ -109,7 +109,7 @@ def epilogue(self, root_loader: TypeDef) -> None:
importFromURI(file.absoluteURI).assertNotThrown(format!"Failed to load %s"(file));
}}
}}
"""
""" # noqa: B907
)

@staticmethod
Expand Down Expand Up @@ -164,9 +164,11 @@ def parse_record_field_type(
subject = jsonld_pred["mapSubject"]
if "mapPredicate" in jsonld_pred:
predicate = jsonld_pred["mapPredicate"]
annotations.append(f'@idMap("{subject}", "{predicate}")')
annotations.append(
f'@idMap("{subject}", "{predicate}")' # noqa: B907
)
else:
annotations.append(f'@idMap("{subject}")')
annotations.append(f'@idMap("{subject}")') # noqa: B907
if jsonld_pred.get("_type", "") == "@id":
if jsonld_pred.get("identity", False):
annotations.append("@link(LinkResolver.id)")
Expand Down Expand Up @@ -224,7 +226,7 @@ def parse_record_field(
value = shortname(type_["symbols"][0])
else:
value = cast(str, parent_name)
return f'{doc_comment}static immutable {fname} = "{value}";'
return f'{doc_comment}static immutable {fname} = "{value}";' # noqa: B907

annotate_str, type_str = self.parse_record_field_type(type_, jsonld_pred)
return f"{doc_comment}{annotate_str}{type_str} {fname};"
Expand Down Expand Up @@ -269,7 +271,7 @@ def parse_enum(self, stype: Dict[str, Any]) -> str:
classname = self.safe_name(name)
syms = "\n".join(
(
f' s{i} = "{shortname(sym)}", ///'
f' s{i} = "{shortname(sym)}", ///' # noqa: B907
for i, sym in enumerate(stype["symbols"])
)
)
Expand Down
6 changes: 4 additions & 2 deletions schema_salad/dotnet_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,10 +904,12 @@ def expand_resource_template_to(resource: str, path: Path) -> None:
self.target_dir / self.package / "Properties" / "AssemblyInfo.cs",
)
vocab = ",\n ".join(
f"""["{k}"] = "{self.vocab[k]}\"""" for k in sorted(self.vocab.keys())
f"""["{k}"] = "{self.vocab[k]}\"""" # noqa: B907
for k in sorted(self.vocab.keys())
)
rvocab = ",\n ".join(
f"""["{self.vocab[k]}"] = "{k}\"""" for k in sorted(self.vocab.keys())
f"""["{self.vocab[k]}"] = "{k}\"""" # noqa: B907
for k in sorted(self.vocab.keys())
)

loader_instances = ""
Expand Down
4 changes: 2 additions & 2 deletions schema_salad/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def fetch_text(self, url: str, content_types: Optional[List[str]] = None) -> str
if content_type not in content_types:
_logger.warning(
f"While fetching {url}, got content-type of "
f"'{content_type}'. Expected one of {content_types}."
f"{content_type!r}. Expected one of {content_types}."
)
return resp.text
if scheme == "file":
Expand Down Expand Up @@ -128,7 +128,7 @@ def check_exists(self, url: str) -> bool:
return os.path.exists(urllib.request.url2pathname(str(path)))
if scheme == "mailto":
return True
raise ValidationException(f"Unsupported scheme '{scheme}' in url: {url}")
raise ValidationException(f"Unsupported scheme {scheme!r} in url: {url}")

def urljoin(self, base_url: str, url: str) -> str:
if url.startswith("_:"):
Expand Down
4 changes: 2 additions & 2 deletions schema_salad/java_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,8 @@ def expand_resource_template_to(resource: str, path: Path) -> None:
vocab = ""
rvocab = ""
for k in sorted(self.vocab.keys()):
vocab += f""" vocab.put("{k}", "{self.vocab[k]}");\n"""
rvocab += f""" rvocab.put("{self.vocab[k]}", "{k}");\n"""
vocab += f""" vocab.put("{k}", "{self.vocab[k]}");\n""" # noqa: B907
rvocab += f""" rvocab.put("{self.vocab[k]}", "{k}");\n""" # noqa: B907

loader_instances = ""
for _, collected_type in self.collected_types.items():
Expand Down
4 changes: 3 additions & 1 deletion schema_salad/makedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,9 @@ def typefmt(
return self.typefmt(tp["type"], redirects)
else:
if str(tp) in redirects:
return f"""<a href="{redirects[tp]}">{redirects[tp]}</a>"""
return (
f"""<a href="{redirects[tp]}">{redirects[tp]}</a>""" # noqa: B907
)
if str(tp) in basicTypes:
return """<a href="{}">{}</a>""".format(
self.primitiveType, vocab_type_name(str(tp))
Expand Down
2 changes: 1 addition & 1 deletion schema_salad/metaschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def expand_url(
if url in loadingOptions.rvocab:
return loadingOptions.rvocab[url]
else:
raise ValidationException(f"Term '{url}' not in vocabulary")
raise ValidationException(f"Term {url!r} not in vocabulary")

return url

Expand Down
17 changes: 11 additions & 6 deletions schema_salad/python_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def prologue(self) -> None:
return "{self.parser_info}"
"""
""" # noqa: B907
)

for primitive in prims.values():
Expand Down Expand Up @@ -266,7 +266,7 @@ def fromDoc(
_doc.lc.data = doc.lc.data
_doc.lc.filename = doc.lc.filename
_errors__ = []
"""
""" # noqa: B907
)

self.idfield = idfield
Expand Down Expand Up @@ -357,7 +357,10 @@ def end_class(self, classname: str, field_names: List[str]) -> None:
self.serializer.write(" return r\n\n")

self.serializer.write(
fmt(f"""attrs = frozenset(["{'", "'.join(field_names)}"])\n""", 4)
fmt(
f"""attrs = frozenset(["{'", "'.join(field_names)}"])\n""", # noqa: B907
4,
)
)

safe_init_fields = [
Expand Down Expand Up @@ -504,7 +507,9 @@ def declare_field(
return

if optional:
self.out.write(f""" if "{shortname(name)}" in _doc:\n""")
self.out.write(
f""" if "{shortname(name)}" in _doc:\n""" # noqa: B907
)
spc = " "
else:
spc = ""
Expand Down Expand Up @@ -648,12 +653,12 @@ def epilogue(self, root_loader: TypeDef) -> None:
"""Trigger to generate the epilouge code."""
self.out.write("_vocab = {\n")
for k in sorted(self.vocab.keys()):
self.out.write(f' "{k}": "{self.vocab[k]}",\n')
self.out.write(f' "{k}": "{self.vocab[k]}",\n') # noqa: B907
self.out.write("}\n")

self.out.write("_rvocab = {\n")
for k in sorted(self.vocab.keys()):
self.out.write(f' "{self.vocab[k]}": "{k}",\n')
self.out.write(f' "{self.vocab[k]}": "{k}",\n') # noqa: B907
self.out.write("}\n\n")

for _, collected_type in self.collected_types.items():
Expand Down
2 changes: 1 addition & 1 deletion schema_salad/python_codegen_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def expand_url(
if url in loadingOptions.rvocab:
return loadingOptions.rvocab[url]
else:
raise ValidationException(f"Term '{url}' not in vocabulary")
raise ValidationException(f"Term {url!r} not in vocabulary")

return url

Expand Down
4 changes: 2 additions & 2 deletions schema_salad/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,10 +803,10 @@ def print_inheritance(doc: List[Dict[str, Any]], stream: IO[Any]) -> None:
"\\l* ".join(shortname(field["name"]) for field in fields)
)
shape = "ellipse" if entry.get("abstract") else "box"
stream.write(f'"{name}" [shape={shape} label="{label}"];\n')
stream.write(f'"{name}" [shape={shape} label="{label}"];\n') # noqa: B907
if "extends" in entry:
for target in aslist(entry["extends"]):
stream.write(f'"{shortname(target)}" -> "{name}";\n')
stream.write(f'"{shortname(target)}" -> "{name}";\n') # noqa: B907
stream.write("}\n")


Expand Down
8 changes: 5 additions & 3 deletions schema_salad/typescript_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def type_loader_enum(self, type_declaration: Dict[str, Any]) -> TypeDef:
for sym in type_declaration["symbols"]:
val = self.safe_name(sym)
const = self.safe_name(sym).replace("-", "_").replace(".", "_").upper()
f.write(f""" {const}='{val}',\n""")
f.write(f""" {const}='{val}',\n""") # noqa: B907
f.write(
"""}
"""
Expand Down Expand Up @@ -770,10 +770,12 @@ def expand_resource_template_to(resource: str, path: Path) -> None:
expand_resource_template_to("index.ts", self.main_src_dir / "index.ts")

vocab = ",\n ".join(
f"""'{k}': '{self.vocab[k]}'""" for k in sorted(self.vocab.keys())
f"""'{k}': '{self.vocab[k]}'""" # noqa: B907
for k in sorted(self.vocab.keys())
)
rvocab = ",\n ".join(
f"""'{self.vocab[k]}': '{k}'""" for k in sorted(self.vocab.keys())
f"""'{self.vocab[k]}': '{k}'""" # noqa: B907
for k in sorted(self.vocab.keys())
)

loader_instances = ""
Expand Down
4 changes: 2 additions & 2 deletions schema_salad/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def validate_ex(
d = datum.get(f.name)
if not d:
if raise_ex:
raise ValidationException(f"Missing '{f.name}' field")
raise ValidationException(f"Missing {f.name!r} field")
else:
return False
avroname = None
Expand Down Expand Up @@ -441,7 +441,7 @@ def validate_ex(
"invalid field `{}`, expected one of: {}".format(
d,
", ".join(
f"'{fn.name}'" for fn in expected_schema.fields
f"{fn.name!r}" for fn in expected_schema.fields
),
),
sl,
Expand Down

0 comments on commit 6bec6d4

Please sign in to comment.