Skip to content

Commit

Permalink
mypy & format fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tetron authored and mr-c committed Jun 24, 2021
1 parent debbc11 commit de41677
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 21 additions & 7 deletions schema_salad/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ def validate_doc(
break
except ValidationException as exc2:
errors.append(
ValidationException(f"tried `{validate.friendly(name)}` but", sourceline, [exc2])
ValidationException(
f"tried `{validate.friendly(name)}` but", sourceline, [exc2]
)
)

objerr = "Invalid"
Expand Down Expand Up @@ -410,7 +412,9 @@ def get_anon_name(
if rec["type"] in ("enum", saladp + "enum"):
for sym in rec["symbols"]:
anon_name += sym
return "anon.enum_" + hashlib.sha1(anon_name.encode("UTF-8")).hexdigest() # nosec
return (
"anon.enum_" + hashlib.sha1(anon_name.encode("UTF-8")).hexdigest()
) # nosec
if rec["type"] in ("record", saladp + "record"):
for field in rec["fields"]:
if isinstance(field, Mapping):
Expand Down Expand Up @@ -488,6 +492,7 @@ def replace_type(
found.add(items)
return items


primitives = {
"http://www.w3.org/2001/XMLSchema#string": "string",
"http://www.w3.org/2001/XMLSchema#boolean": "boolean",
Expand All @@ -498,9 +503,10 @@ def replace_type(
saladp + "null": "null",
saladp + "enum": "enum",
saladp + "array": "array",
saladp + "record": "record"
saladp + "record": "record",
}


def avro_type_name(url: str) -> str:
"""
Turn a URL into an Avro-safe name.
Expand Down Expand Up @@ -546,7 +552,7 @@ def make_valid_avro(
alltypes: Dict[str, Dict[str, Any]],
found: Set[str],
union: bool = False,
field: bool = False
fielddef: bool = False,
) -> Union[
Avro, MutableMapping[str, str], str, List[Union[Any, MutableMapping[str, str], str]]
]:
Expand All @@ -555,7 +561,7 @@ def make_valid_avro(
if isinstance(items, MutableMapping):
avro = copy.copy(items)
if avro.get("name") and avro.get("inVocab", True):
if field:
if fielddef:
avro["name"] = avro_field_name(avro["name"])
else:
avro["name"] = avro_type_name(avro["name"])
Expand All @@ -573,14 +579,22 @@ def make_valid_avro(
found.add(avro["name"])
for field in ("type", "items", "values", "fields"):
if field in avro:
avro[field] = make_valid_avro(avro[field], alltypes, found, union=True, field=(field=="fields"))
avro[field] = make_valid_avro(
avro[field],
alltypes,
found,
union=True,
fielddef=(field == "fields"),
)
if "symbols" in avro:
avro["symbols"] = [avro_field_name(sym) for sym in avro["symbols"]]
return avro
if isinstance(items, MutableSequence):
ret = []
for i in items:
ret.append(make_valid_avro(i, alltypes, found, union=union, field=field))
ret.append(
make_valid_avro(i, alltypes, found, union=union, fielddef=fielddef)
)
return ret
if union and isinstance(items, str):
if items in alltypes and avro_type_name(items) not in found:
Expand Down
2 changes: 2 additions & 0 deletions schema_salad/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ def validate(
LONG_MIN_VALUE = -(1 << 63)
LONG_MAX_VALUE = (1 << 63) - 1


def avro_shortname(name: str) -> str:
return name.split(".")[-1]


def friendly(v): # type: (Any) -> Any
if isinstance(v, avro.schema.NamedSchema):
return avro_shortname(v.name)
Expand Down

0 comments on commit de41677

Please sign in to comment.