Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-3370: Allow complex types as names #1533

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lang/py/avro/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""Contains the Name classes."""
from typing import TYPE_CHECKING, Dict, Optional

from avro.constants import VALID_TYPES
from avro.constants import PRIMITIVE_TYPES

if TYPE_CHECKING:
from avro.schema import NamedSchema
Expand Down Expand Up @@ -154,7 +154,7 @@ def add_name(self, name_attr: str, space_attr: Optional[str], new_schema: "Named
"""
to_add = Name(name_attr, space_attr, self.default_namespace)

if to_add.fullname in VALID_TYPES:
if to_add.fullname in PRIMITIVE_TYPES:
raise avro.errors.SchemaParseException(f"{to_add.fullname} is a reserved type name.")
if to_add.fullname in self.names:
raise avro.errors.SchemaParseException(f'The name "{to_add.fullname}" is already in use.')
Expand Down
8 changes: 8 additions & 0 deletions lang/py/avro/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ class DefaultValueTestCaseType(TypedDict):
},
{"value": {"car": {"value": "head"}, "cdr": {"value": None}}},
),
(
{"type": "record", "name": "record", "fields": [{"name": "value", "type": "int"}, {"name": "next", "type": ["null", "record"]}]},
{"value": 0, "next": {"value": 1, "next": None}},
),
(
{"type": "record", "name": "ns.long", "fields": [{"name": "value", "type": "int"}, {"name": "next", "type": ["null", "ns.long"]}]},
{"value": 0, "next": {"value": 1, "next": None}},
),
)
)

Expand Down
14 changes: 14 additions & 0 deletions lang/py/avro/test/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ class InvalidTestSchema(TestSchema):
InvalidTestSchema([{"type": "array", "items": "long"}, {"type": "array", "items": "string"}]),
]

NAME_EXAMPLES = [
ValidTestSchema({"type": "enum", "name": "record", "symbols": ["A", "B"]}),
ValidTestSchema({"type": "record", "name": "record", "fields": [{"name": "f", "type": "long"}]}),
InvalidTestSchema({"type": "enum", "name": "int", "symbols": ["A", "B"]}),
ValidTestSchema({"type": "enum", "name": "ns.int", "symbols": ["A", "B"]}),
ValidTestSchema({"type": "enum", "namespace": "ns", "name": "int", "symbols": ["A", "B"]}),
ValidTestSchema(
{"type": "record", "name": "LinkedList", "fields": [{"name": "value", "type": "int"}, {"name": "next", "type": ["null", "LinkedList"]}]}
),
ValidTestSchema({"type": "record", "name": "record", "fields": [{"name": "value", "type": "int"}, {"name": "next", "type": ["null", "record"]}]}),
ValidTestSchema({"type": "record", "name": "ns.int", "fields": [{"name": "value", "type": "int"}, {"name": "next", "type": ["null", "ns.int"]}]}),
]

NAMED_IN_UNION_EXAMPLES = [
ValidTestSchema(
{
Expand Down Expand Up @@ -512,6 +525,7 @@ class InvalidTestSchema(TestSchema):
EXAMPLES += ARRAY_EXAMPLES
EXAMPLES += MAP_EXAMPLES
EXAMPLES += UNION_EXAMPLES
EXAMPLES += NAME_EXAMPLES
EXAMPLES += NAMED_IN_UNION_EXAMPLES
EXAMPLES += RECORD_EXAMPLES
EXAMPLES += DOC_EXAMPLES
Expand Down