Skip to content

Commit

Permalink
AVRO-2921 Add Type Hints to avro.schema
Browse files Browse the repository at this point in the history
  • Loading branch information
kojiromike committed Jul 18, 2023
1 parent 5e6cec1 commit df05ad8
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 236 deletions.
7 changes: 5 additions & 2 deletions lang/py/avro/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ def convert(value: str, field: avro.schema.Field) -> Union[int, float, str, byte


def convert_union(value: str, field: avro.schema.Field) -> Union[int, float, str, bytes, bool, None]:
for name in (s.name for s in field.type.schemas):
if not isinstance(field.type, avro.schema.UnionSchema):
raise avro.errors.UsageError(f"Expected field.type to be a Union, but it was {field.type}")
# Casts to be fixed in AVRO-3798
for name in (cast(avro.schema.NamedSchema, s).name for s in field.type.schemas):
try:
return convert(value, name)
return convert(value, cast(avro.schema.Field, name))
except ValueError:
continue
raise avro.errors.UsageError("Exhausted Union Schema without finding a match")
Expand Down
4 changes: 2 additions & 2 deletions lang/py/avro/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ def to_json(self, names: Optional[avro.name.Names] = None) -> "MessageObject":
to_dump = MessageObject()
except NameError:
to_dump = {}
to_dump["request"] = self.request.to_json(names)
to_dump["request"] = cast(Sequence[Mapping[str, object]], self.request.to_json(names))
to_dump["response"] = self.response.to_json(names)
if self.errors:
to_dump["errors"] = self.errors.to_json(names)
to_dump["errors"] = cast(Optional[Sequence[str]], self.errors.to_json(names))

return to_dump

Expand Down
Loading

0 comments on commit df05ad8

Please sign in to comment.