Repository: enthought/okonomiyaki

Issue:
Wrong exception raised for invalid python attribute in egg metadata
The exception raised should be `InvalidMetadataField`, as used elsewhere in the module.

```
Traceback (most recent call last):
  File "okonomiyaki/file_formats/_egg_info.py", line 733, in _from_egg
    spec_depend = LegacySpecDepend._from_egg(path_or_file, sha256)
  File "okonomiyaki/file_formats/_egg_info.py", line 463, in _from_egg
    return _create_spec_depend(path_or_file)
  File "okonomiyaki/file_formats/_egg_info.py", line 455, in _create_spec_depend
    spec_depend_string, epd_platform, sha256
  File "okonomiyaki/file_formats/_egg_info.py", line 647, in _normalized_info_from_string
    raw_data[_TAG_PYTHON]
  File "okonomiyaki/file_formats/_egg_info.py", line 327, in _guess_python_tag
    raise InvalidMetadata(msg.format(pyver))
okonomiyaki.errors.InvalidMetadata: python_tag cannot be guessed for python = red
```

Discussion:

Patch:
diff --git a/okonomiyaki/errors.py b/okonomiyaki/errors.py
index be3e7d6..fd003c4 100644
--- a/okonomiyaki/errors.py
+++ b/okonomiyaki/errors.py
@@ -47,8 +47,11 @@ class InvalidMetadataField(InvalidMetadata):
     def __init__(self, name, value, *a, **kw):
         self.name = name
         self.value = value
-        message = 'Metadata field is invalid ({0} = {1!r})'.format(
-            name, value)
+        if value is InvalidMetadataField.undefined:
+            message = "Missing metadata field {0!r}".format(self.name)
+        else:
+            message = 'Invalid value for metadata field {0!r}: {1!r}'.format(
+                name, value)
         super(InvalidMetadataField, self).__init__(message, *a, **kw)
 
 
diff --git a/okonomiyaki/file_formats/_egg_info.py b/okonomiyaki/file_formats/_egg_info.py
index 332168f..49fae61 100644
--- a/okonomiyaki/file_formats/_egg_info.py
+++ b/okonomiyaki/file_formats/_egg_info.py
@@ -347,8 +347,7 @@ def _guess_python_tag(pyver):
     else:
         m = _PYVER_RE.search(pyver)
         if m is None:
-            msg = "python_tag cannot be guessed for python = {0}"
-            raise InvalidMetadata(msg.format(pyver))
+            raise InvalidMetadataField('python', pyver)
         else:
             major = m.groupdict()["major"]
             minor = m.groupdict()["minor"]


Test patch:
diff --git a/okonomiyaki/file_formats/tests/test__egg_info.py b/okonomiyaki/file_formats/tests/test__egg_info.py
index 6765b8f..7760c64 100644
--- a/okonomiyaki/file_formats/tests/test__egg_info.py
+++ b/okonomiyaki/file_formats/tests/test__egg_info.py
@@ -301,10 +301,14 @@ packages = [
 
         # When/Then
         with self.assertRaisesRegexp(
-                InvalidMetadata,
-                r'^python_tag cannot be guessed'):
+            InvalidMetadataField,
+            r"^Invalid value for metadata field 'python': 'a.7'"
+        ) as exc:
             LegacySpecDepend.from_string(s)
 
+        self.assertEqual(exc.exception.name, "python")
+        self.assertEqual(exc.exception.value, "a.7")
+
     def test_blacklisted_platform(self):
         # Given
         egg = XZ_5_2_0_EGG
@@ -776,8 +780,8 @@ class TestParseRawspec(unittest.TestCase):
 
         # When/Then
         with self.assertRaisesRegexp(
-                InvalidMetadataField,
-                r'^Metadata field is invalid \(name = <undefined>\)$'):
+            InvalidMetadataField, r"^Missing metadata field 'name'"
+        ):
             parse_rawspec(spec_string)
 
     def test_simple_1_2(self):
@@ -911,8 +915,9 @@ packages = [
 
         # When/Then
         with self.assertRaisesRegexp(
-                InvalidMetadataField,
-                r'^Metadata field is invalid \(metadata_version = None\)$'):
+            InvalidMetadataField,
+            r"^Invalid value for metadata field 'metadata_version': None"
+        ):
             parse_rawspec(spec_s)
 
         # Given a spec_string without some other metadata in >= 1.1
@@ -933,8 +938,8 @@ packages = [
 
         # When/Then
         with self.assertRaisesRegexp(
-                InvalidMetadataField,
-                r'^Metadata field is invalid \(platform = <undefined>\)$'):
+            InvalidMetadataField, r"^Missing metadata field 'platform'"
+        ):
             parse_rawspec(spec_s)
 
         # Given a spec_string without some other metadata in >= 1.2
@@ -956,8 +961,8 @@ packages = [
 
         # When/Then
         with self.assertRaisesRegexp(
-                InvalidMetadataField,
-                r'^Metadata field is invalid \(python_tag = <undefined>\)$'):
+            InvalidMetadataField, r"^Missing metadata field 'python_tag'"
+        ):
             parse_rawspec(spec_s)
 
 
diff --git a/okonomiyaki/platforms/tests/test_python_implementation.py b/okonomiyaki/platforms/tests/test_python_implementation.py
index 8ccddd9..92d6cf9 100644
--- a/okonomiyaki/platforms/tests/test_python_implementation.py
+++ b/okonomiyaki/platforms/tests/test_python_implementation.py
@@ -50,8 +50,9 @@ class TestPythonImplementation(unittest.TestCase):
 
         # When/Then
         with self.assertRaisesRegexp(
-                InvalidMetadataField,
-                r"^Metadata field is invalid \(python_tag = 'cp'\)$"):
+            InvalidMetadataField,
+            r"^Invalid value for metadata field 'python_tag': 'cp'"
+        ):
             PythonImplementation.from_string(s)
 
         # Given
@@ -59,8 +60,9 @@ class TestPythonImplementation(unittest.TestCase):
 
         # When/Then
         with self.assertRaisesRegexp(
-                InvalidMetadataField,
-                r"^Metadata field is invalid \(python_tag = 'py2'\)$"):
+            InvalidMetadataField,
+            r"^Invalid value for metadata field 'python_tag': 'py2'$"
+        ):
             PythonImplementation.from_string(s)
 
         # Given
@@ -68,8 +70,9 @@ class TestPythonImplementation(unittest.TestCase):
 
         # When/Then
         with self.assertRaisesRegexp(
-                InvalidMetadataField,
-                r"^Metadata field is invalid \(python_tag = 'py234'\)$"):
+            InvalidMetadataField,
+            r"^Invalid value for metadata field 'python_tag': 'py234'$"
+        ):
             PythonImplementation.from_string(s)
 
     def test_simple(self):
@@ -125,8 +128,9 @@ class TestPythonImplementation(unittest.TestCase):
 
         # When/Then
         with self.assertRaisesRegexp(
-                InvalidMetadataField,
-                r"^Metadata field is invalid \(python_tag = 'py3'\)$"):
+            InvalidMetadataField,
+            r"^Invalid value for metadata field 'python_tag': 'py3'$"
+        ):
             PythonImplementation.from_string(tag_string)
 
         # Given
@@ -134,6 +138,7 @@ class TestPythonImplementation(unittest.TestCase):
 
         # When/Then
         with self.assertRaisesRegexp(
-                InvalidMetadataField,
-                r"^Metadata field is invalid \(python_tag = 'py345'\)$"):
+            InvalidMetadataField,
+            r"^Invalid value for metadata field 'python_tag': 'py345'$"
+        ):
             PythonImplementation.from_string(tag_string)