Repository: pypa/setuptools
Pull request #1207 (2017-11-21 22:20:34)

Add setup.cfg support for long_description_content_type
[This was already claimed in the docs.](https://github.com/pypa/setuptools/blob/d45be2cc4f7a1e4ddc70b363baaa613c6b068b98/docs/setuptools.txt#L2417) That this did not work before can be seen by cherry picking the changed test in this PR without the other changes: it will fail. Fixes issue #1206 


Patch:
diff --git a/setuptools/command/egg_info.py b/setuptools/command/egg_info.py
index 103c5f20b..d3725a2f2 100755
--- a/setuptools/command/egg_info.py
+++ b/setuptools/command/egg_info.py
@@ -597,10 +597,7 @@ def write_pkg_info(cmd, basename, filename):
         metadata = cmd.distribution.metadata
         metadata.version, oldver = cmd.egg_version, metadata.version
         metadata.name, oldname = cmd.egg_name, metadata.name
-        metadata.long_description_content_type = getattr(
-            cmd.distribution,
-            'long_description_content_type'
-        )
+
         try:
             # write unescaped data to PKG-INFO, so older pkg_resources
             # can still parse it
@@ -640,7 +637,7 @@ def write_requirements(cmd, basename, filename):
 
 
 def write_setup_requirements(cmd, basename, filename):
-    data = io.StringIO()
+    data = StringIO()
     _write_requirements(data, cmd.distribution.setup_requires)
     cmd.write_or_delete_file("setup-requirements", filename, data.getvalue())
 
diff --git a/setuptools/dist.py b/setuptools/dist.py
index aa304500e..8148df954 100644
--- a/setuptools/dist.py
+++ b/setuptools/dist.py
@@ -58,11 +58,8 @@ def write_pkg_file(self, file):
     if self.download_url:
         file.write('Download-URL: %s\n' % self.download_url)
 
-    long_desc_content_type = getattr(
-        self,
-        'long_description_content_type',
-        None
-    ) or 'UNKNOWN'
+    long_desc_content_type = \
+        self.long_description_content_type or 'UNKNOWN'
     file.write('Description-Content-Type: %s\n' % long_desc_content_type)
 
     long_desc = rfc822_escape(self.get_long_description())
@@ -324,14 +321,16 @@ class Distribution(Distribution_parse_config_files, _Distribution):
         self.dist_files = []
         self.src_root = attrs.pop("src_root", None)
         self.patch_missing_pkg_info(attrs)
-        self.long_description_content_type = attrs.get(
-            'long_description_content_type'
-        )
         self.dependency_links = attrs.pop('dependency_links', [])
         self.setup_requires = attrs.pop('setup_requires', [])
         for ep in pkg_resources.iter_entry_points('distutils.setup_keywords'):
             vars(self).setdefault(ep.name, None)
         _Distribution.__init__(self, attrs)
+
+        self.metadata.long_description_content_type = attrs.get(
+            'long_description_content_type'
+        )
+
         if isinstance(self.metadata.version, numbers.Number):
             # Some people apparently take "version number" too literally :)
             self.metadata.version = str(self.metadata.version)


Test patch:
diff --git a/setuptools/tests/test_config.py b/setuptools/tests/test_config.py
index cdfa5af43..15b0cee10 100644
--- a/setuptools/tests/test_config.py
+++ b/setuptools/tests/test_config.py
@@ -110,6 +110,7 @@ class TestMetadata:
             '[metadata]\n'
             'version = 10.1.1\n'
             'description = Some description\n'
+            'long_description_content_type = text/something\n'
             'long_description = file: README\n'
             'name = fake_name\n'
             'keywords = one, two\n'
@@ -131,6 +132,7 @@ class TestMetadata:
 
             assert metadata.version == '10.1.1'
             assert metadata.description == 'Some description'
+            assert metadata.long_description_content_type == 'text/something'
             assert metadata.long_description == 'readme contents\nline2'
             assert metadata.provides == ['package', 'package.sub']
             assert metadata.license == 'BSD 3-Clause License'
diff --git a/setuptools/tests/test_egg_info.py b/setuptools/tests/test_egg_info.py
index 0810ee3bf..a97d0c84b 100644
--- a/setuptools/tests/test_egg_info.py
+++ b/setuptools/tests/test_egg_info.py
@@ -191,8 +191,7 @@ class TestEggInfo(object):
                 test_params = test.lstrip().split('\n\n', 3)
                 name_kwargs = test_params.pop(0).split('\n')
                 if len(name_kwargs) > 1:
-                    val = name_kwargs[1].strip()
-                    install_cmd_kwargs = ast.literal_eval(val)
+                    install_cmd_kwargs = ast.literal_eval(name_kwargs[1].strip())
                 else:
                     install_cmd_kwargs = {}
                 name = name_kwargs[0].strip()
@@ -212,11 +211,9 @@ class TestEggInfo(object):
                                                   expected_requires,
                                                   install_cmd_kwargs,
                                                   marks=marks))
-            return pytest.mark.parametrize(
-                'requires,use_setup_cfg,'
-                'expected_requires,install_cmd_kwargs',
-                argvalues, ids=idlist,
-            )
+            return pytest.mark.parametrize('requires,use_setup_cfg,'
+                                           'expected_requires,install_cmd_kwargs',
+                                           argvalues, ids=idlist)
 
     @RequiresTestHelper.parametrize(
         # Format of a test:
@@ -230,20 +227,6 @@ class TestEggInfo(object):
         #
         # expected contents of requires.txt
 
-        '''
-        install_requires_deterministic
-
-        install_requires=["fake-factory==0.5.2", "pytz"]
-
-        [options]
-        install_requires =
-            fake-factory==0.5.2
-            pytz
-
-        fake-factory==0.5.2
-        pytz
-        ''',
-
         '''
         install_requires_with_marker
 
@@ -378,9 +361,9 @@ class TestEggInfo(object):
         mismatch_marker=mismatch_marker,
         mismatch_marker_alternate=mismatch_marker_alternate,
     )
-    def test_requires(
-            self, tmpdir_cwd, env, requires, use_setup_cfg,
-            expected_requires, install_cmd_kwargs):
+    def test_requires(self, tmpdir_cwd, env,
+                      requires, use_setup_cfg,
+                      expected_requires, install_cmd_kwargs):
         self._setup_script_with_requires(requires, use_setup_cfg)
         self._run_install_command(tmpdir_cwd, env, **install_cmd_kwargs)
         egg_info_dir = os.path.join('.', 'foo.egg-info')