[
https://issues.apache.org/jira/browse/AVRO-2183?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16682287#comment-16682287
]
ASF GitHub Bot commented on AVRO-2183:
--------------------------------------
Fokko closed pull request #313: AVRO-2183 Provide name only for named schema
URL: https://github.com/apache/avro/pull/313
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/lang/py3/avro/schema.py b/lang/py3/avro/schema.py
index 0ebd41ffc..2fc7302d1 100644
--- a/lang/py3/avro/schema.py
+++ b/lang/py3/avro/schema.py
@@ -183,18 +183,6 @@ def __init__(self, type, other_props=None):
if other_props:
self._props.update(other_props)
- @property
- def name(self):
- """Returns: the simple name of this schema."""
- return self._props['name']
-
- @property
- def fullname(self):
- """Returns: the fully qualified name of this schema."""
- # By default, the full name is the simple name.
- # Named schemas override this behavior to include the namespace.
- return self.name
-
@property
def namespace(self):
"""Returns: the namespace this schema belongs to, if any, or None."""
@@ -625,6 +613,12 @@ def name(self):
# The name of a primitive type is the type itself.
return self.type
+ @property
+ def fullname(self):
+ """Returns: the fully qualified name of this schema."""
+ # The full name is the simple name for primitive schema.
+ return self.name
+
def to_json(self, names=None):
if len(self.props) == 1:
return self.fullname
diff --git a/lang/py3/avro/tests/test_schema.py
b/lang/py3/avro/tests/test_schema.py
index c83652886..fa7056747 100644
--- a/lang/py3/avro/tests/test_schema.py
+++ b/lang/py3/avro/tests/test_schema.py
@@ -472,6 +472,19 @@ def testCorrectRecursiveExtraction(self):
# it could be reparsed.
self.assertEqual("X", t.fields[0].type.name)
+ def testNoName(self):
+ """Test that schema without a name
+ raise AttributeError when you try
+ to access their name."""
+ cases = [
+ '{"type": "array", "items": "int"}',
+ '{"type": "map", "values": "int"}',
+ '["null", "int"]',
+ ]
+ for case in (schema.Parse(case) for case in cases):
+ self.assertRaises(AttributeError, lambda: case.name)
+ self.assertEqual(getattr(case, "name", "default"), "default")
+
def testParse(self):
correct = 0
for iexample, example in enumerate(EXAMPLES):
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Nameless schema should raise AttributeError when attempting to access name
> --------------------------------------------------------------------------
>
> Key: AVRO-2183
> URL: https://issues.apache.org/jira/browse/AVRO-2183
> Project: Apache Avro
> Issue Type: Bug
> Components: python
> Reporter: Michael A. Smith
> Priority: Major
>
> In the "py" implementation, this works as expected:
> {noformat}
> >>> from avro.schema import parse
> >>> s = parse('{"type": "array", "items": "int"}')
> >>> s.name
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'ArraySchema' object has no attribute 'name'{noformat}
> but in the py3 implementation, a {{NameError}} is raised instead:
> {noformat}
> >>> from avro.schema import Parse
> >>> s=Parse('{"type":"array","items":"int"}')
> >>> s.name
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/home/michaels/dev/avro/lang/py3/avro/schema.py", line 224, in name
> return self._props['name']
> KeyError: 'name'{noformat}
> This behavior breaks several python idioms, including the ability to get a
> default value using {{getattr}}:
> {noformat}
> >>> getattr(s, "name", "default")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "/home/michaels/dev/avro/lang/py3/avro/schema.py", line 224, in name
> return self._props['name']
> KeyError: 'name'{noformat}
> I will open a PR with tests and a fix.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)