From debbc11ebf542c231d8e50223036c1c6a4ef2ba9 Mon Sep 17 00:00:00 2001 From: Peter Amstutz Date: Wed, 23 Jun 2021 17:33:46 -0400 Subject: [PATCH] Fix tests --- mypy_requirements.txt | 1 - schema_salad/codegen_base.py | 2 +- schema_salad/java_codegen.py | 2 +- schema_salad/makedoc.py | 10 +++++----- schema_salad/python_codegen.py | 10 ++++++---- schema_salad/schema.py | 4 ++-- schema_salad/validate.py | 10 ++++++---- setup.py | 2 +- 8 files changed, 22 insertions(+), 19 deletions(-) diff --git a/mypy_requirements.txt b/mypy_requirements.txt index d8f3601a..1f438395 100644 --- a/mypy_requirements.txt +++ b/mypy_requirements.txt @@ -1,4 +1,3 @@ mypy==0.910 types-pkg_resources types-requests -pytest diff --git a/schema_salad/codegen_base.py b/schema_salad/codegen_base.py index 02a6072b..17d26cf4 100644 --- a/schema_salad/codegen_base.py +++ b/schema_salad/codegen_base.py @@ -66,7 +66,7 @@ def prologue(self) -> None: @staticmethod def safe_name(name: str) -> str: """Generate a safe version of the given name.""" - return schema.avro_name(name) + return schema.avro_field_name(name) def begin_class( self, # pylint: disable=too-many-arguments diff --git a/schema_salad/java_codegen.py b/schema_salad/java_codegen.py index 37ae62b7..9d80d6e8 100644 --- a/schema_salad/java_codegen.py +++ b/schema_salad/java_codegen.py @@ -151,7 +151,7 @@ def prologue(self) -> None: @staticmethod def property_name(name: str) -> str: - avn = schema.avro_name(name) + avn = schema.avro_field_name(name) return avn @staticmethod diff --git a/schema_salad/makedoc.py b/schema_salad/makedoc.py index 29a190ce..f152b00b 100644 --- a/schema_salad/makedoc.py +++ b/schema_salad/makedoc.py @@ -315,7 +315,7 @@ def typefmt( "https://w3id.org/cwl/salad#record", "https://w3id.org/cwl/salad#enum", ): - frg = schema.avro_name(tp["name"]) + frg = schema.avro_type_name(tp["name"]) if tp["name"] in redirects: return """{}""".format(redirects[tp["name"]], frg) if tp["name"] in self.typemap: @@ -325,7 +325,7 @@ def typefmt( and len(tp["symbols"]) == 1 ): return "constant value {}".format( - schema.avro_name(tp["symbols"][0]) + schema.avro_field_name(tp["symbols"][0]) ) return frg if isinstance(tp["type"], MutableMapping): @@ -335,7 +335,7 @@ def typefmt( return """{}""".format(redirects[tp], redirects[tp]) if str(tp) in basicTypes: return """{}""".format( - self.primitiveType, schema.avro_name(str(tp)) + self.primitiveType, schema.avro_type_name(str(tp)) ) frg2 = urldefrag(tp)[1] if frg2 != "": @@ -443,7 +443,7 @@ def extendsfrom(item: Dict[str, Any], ex: List[Dict[str, Any]]) -> None: desc = i["doc"] - rfrg = schema.avro_name(i["name"]) + rfrg = schema.avro_field_name(i["name"]) tr = """
{}
@@ -472,7 +472,7 @@ def extendsfrom(item: Dict[str, Any], ex: List[Dict[str, Any]]) -> None: for e in ex: for i in e.get("symbols", []): doc += "" - efrg = schema.avro_name(i) + efrg = schema.avro_field_name(i) doc += "{}{}".format( efrg, enumDesc.get(efrg, "") ) diff --git a/schema_salad/python_codegen.py b/schema_salad/python_codegen.py index 8fd79ae1..71150ada 100644 --- a/schema_salad/python_codegen.py +++ b/schema_salad/python_codegen.py @@ -14,7 +14,7 @@ from pkg_resources import resource_stream -from . import schema +from . import schema, validate from .codegen_base import CodeGenBase, TypeDef from .exceptions import SchemaException from .schema import shortname @@ -56,7 +56,9 @@ def __init__(self, out: IO[str], copyright: Optional[str]) -> None: @staticmethod def safe_name(name): # type: (str) -> str - avn = schema.avro_name(name) + avn = schema.avro_field_name(name) + if avn.startswith("anon."): + avn = avn[5:] if avn in ("class", "in"): # reserved words avn = avn + "_" @@ -498,8 +500,8 @@ def typedsl_loader(self, inner, ref_scope): # type: (TypeDef, Union[int, None]) -> TypeDef return self.declare_type( TypeDef( - f"typedsl_{inner.name}_{ref_scope}", - f"_TypeDSLLoader({inner.name}, {ref_scope})", + f"typedsl_{self.safe_name(inner.name)}_{ref_scope}", + f"_TypeDSLLoader({self.safe_name(inner.name)}, {ref_scope})", ) ) diff --git a/schema_salad/schema.py b/schema_salad/schema.py index 23874d09..cf5e03ad 100644 --- a/schema_salad/schema.py +++ b/schema_salad/schema.py @@ -376,13 +376,13 @@ def validate_doc( except ClassValidationException as exc1: errors = [ ClassValidationException( - f"tried `{name}` but", sourceline, [exc1] + f"tried `{validate.friendly(name)}` but", sourceline, [exc1] ) ] break except ValidationException as exc2: errors.append( - ValidationException(f"tried `{name}` but", sourceline, [exc2]) + ValidationException(f"tried `{validate.friendly(name)}` but", sourceline, [exc2]) ) objerr = "Invalid" diff --git a/schema_salad/validate.py b/schema_salad/validate.py index d5cdb57c..71c4cb84 100644 --- a/schema_salad/validate.py +++ b/schema_salad/validate.py @@ -41,10 +41,12 @@ 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 v.name + return avro_shortname(v.name) if isinstance(v, avro.schema.ArraySchema): return "array of <{}>".format(friendly(v.items)) elif isinstance(v, avro.schema.PrimitiveSchema): @@ -52,7 +54,7 @@ def friendly(v): # type: (Any) -> Any elif isinstance(v, avro.schema.UnionSchema): return " or ".join([friendly(s) for s in v.schemas]) else: - return v + return avro_shortname(v) def vpformat(datum): # type: (Any) -> str @@ -139,7 +141,7 @@ def validate_ex( "value is a {} but expected a string".format(type(datum).__name__) ) return False - if expected_schema.name == "Expression": + if expected_schema.name == "w3id.org.cwl.cwl.Expression": if "$(" in datum or "${" in datum: return True if raise_ex: @@ -279,7 +281,7 @@ def validate_ex( raise ValidationException(f"Missing '{f.name}' field") else: return False - if expected_schema.name != d: + if avro_shortname(expected_schema.name) != d: if raise_ex: raise ValidationException( "Expected class '{}' but this is '{}'".format( diff --git a/setup.py b/setup.py index e9054760..5e38e87f 100644 --- a/setup.py +++ b/setup.py @@ -98,7 +98,7 @@ setup( name="schema-salad", - version="7.1", # update the VERSION prefix in the Makefile as well 🙂 + version="8.0", # update the VERSION prefix in the Makefile as well 🙂 description="Schema Annotations for Linked Avro Data (SALAD)", long_description=open(README).read(), long_description_content_type="text/x-rst",