Repository: pypa/twine

Issue:
`twine check` against a distribution with no description fails with `AttributeError`
## Your Environment
```console
$ pip freeze --all
bleach==3.0.0
certifi==2018.8.24
cffi==1.11.5
chardet==3.0.4
cmarkgfm==0.4.2
docutils==0.14
future==0.16.0
idna==2.7
pip==18.1
pkginfo==1.4.2
pycparser==2.19
Pygments==2.2.0
readme-renderer==22.0
requests==2.19.1
requests-toolbelt==0.8.0
setuptools==40.4.3
six==1.11.0
tqdm==4.26.0
twine==1.12.1
urllib3==1.23
webencodings==0.5.1
wheel==0.32.1

$ twine --version
twine version 1.12.1 (pkginfo: 1.4.2, requests: 2.19.1, setuptools: 40.4.3,
requests-toolbelt: 0.8.0, tqdm: 4.26.0)

$ python --version --version
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0]

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.1 LTS
Release:	18.04
Codename:	bionic
```

## Steps to Reproduce
If the issue is predictable and consistently reproducible, please list the steps here.

### `setup.py`

```python
from setuptools import setup

setup(name='foo', version='0.0.0')
```

### reproduction

```console
$ python setup.py sdist >& /dev/null
$ twine check dist/*
Checking distribution dist/foo-0.0.0.tar.gz: Traceback (most recent call last):
  File "/tmp/t/venv/bin/twine", line 11, in <module>
    sys.exit(main())
  File "/tmp/t/venv/lib/python3.6/site-packages/twine/__main__.py", line 28, in main
    return dispatch(sys.argv[1:])
  File "/tmp/t/venv/lib/python3.6/site-packages/twine/cli.py", line 75, in dispatch
    return main(args.args)
  File "/tmp/t/venv/lib/python3.6/site-packages/twine/commands/check.py", line 126, in main
    return check(args.dists)
  File "/tmp/t/venv/lib/python3.6/site-packages/twine/commands/check.py", line 97, in check
    metadata.get("description"), stream=stream, **parameters
  File "/tmp/t/venv/lib/python3.6/site-packages/readme_renderer/rst.py", line 95, in render
    parts = publish_parts(raw, writer=writer, settings_overrides=settings)
  File "/tmp/t/venv/lib/python3.6/site-packages/docutils/core.py", line 450, in publish_parts
    enable_exit_status=enable_exit_status)
  File "/tmp/t/venv/lib/python3.6/site-packages/docutils/core.py", line 664, in publish_programmatically
    output = pub.publish(enable_exit_status=enable_exit_status)
  File "/tmp/t/venv/lib/python3.6/site-packages/docutils/core.py", line 217, in publish
    self.settings)
  File "/tmp/t/venv/lib/python3.6/site-packages/docutils/readers/__init__.py", line 71, in read
    self.input = self.source.read()
  File "/tmp/t/venv/lib/python3.6/site-packages/docutils/io.py", line 435, in read
    return self.decode(self.source)
  File "/tmp/t/venv/lib/python3.6/site-packages/docutils/io.py", line 99, in decode
    data_encoding = self.determine_encoding_from_data(data)
  File "/tmp/t/venv/lib/python3.6/site-packages/docutils/io.py", line 142, in determine_encoding_from_data
    if data.startswith(start_bytes):
AttributeError: 'NoneType' object has no attribute 'startswith'
```

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/pypa/twine/pull/412?src=pr&el=h1) Report
> Merging [#412](https://codecov.io/gh/pypa/twine/pull/412?src=pr&el=desc) into [master](https://codecov.io/gh/pypa/twine/commit/360401bccbe0ae0628c2b1a9304262f5ac033efc?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pypa/twine/pull/412/graphs/tree.svg?width=650&token=NrvN4iFbj6&height=150&src=pr)](https://codecov.io/gh/pypa/twine/pull/412?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #412   +/-   ##
=======================================
  Coverage   78.08%   78.08%           
=======================================
  Files          14       14           
  Lines         730      730           
  Branches      104      104           
=======================================
  Hits          570      570           
  Misses        127      127           
  Partials       33       33
```


| [Impacted Files](https://codecov.io/gh/pypa/twine/pull/412?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [twine/commands/check.py](https://codecov.io/gh/pypa/twine/pull/412/diff?src=pr&el=tree#diff-dHdpbmUvY29tbWFuZHMvY2hlY2sucHk=) | `100% <ø> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pypa/twine/pull/412?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pypa/twine/pull/412?src=pr&el=footer). Last update [360401b...520d7ff](https://codecov.io/gh/pypa/twine/pull/412?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

di: Since all this command does right now is check if the description is valid, perhaps it should give a warning if there is no `description` value, rather than silently passing? It seems to me like there should be a difference between "this package has provided an empty description" and "this package has not provided a description". The user probably wants to know if the latter is happening.
merwok: I agree with the warning or error.  For example, I found this bug and PR because twine does not include long_description in generated sdists: https://github.com/takluyver/flit/issues/216
asottile: This is pretty special / broken from the setuptools / wheel side.  Here's some data:

```
Checking distribution long/dist/pkg-0.0.0-py3-none-any.whl: 
*******************************************************************************
description: 'long description\n\n\n'
description_content_type: 'text/markdown'
*******************************************************************************

Checking distribution missing/dist/pkg-0.0.0-py3-none-any.whl: 
*******************************************************************************
description: 'UNKNOWN\n\n\n'
description_content_type: None
*******************************************************************************

Checking distribution missing_long/dist/pkg-0.0.0-py3-none-any.whl: 
*******************************************************************************
description: 'UNKNOWN\n\n\n'
description_content_type: 'text/markdown'
*******************************************************************************

Checking distribution missing_type/dist/pkg-0.0.0-py3-none-any.whl: 
*******************************************************************************
description: 'long description\n\n\n'
description_content_type: None
*******************************************************************************

Checking distribution long/dist/pkg-0.0.0.tar.gz: 
*******************************************************************************
description: 'long description'
description_content_type: 'text/markdown'
*******************************************************************************

Checking distribution missing/dist/pkg-0.0.0.tar.gz: 
*******************************************************************************
description: None
description_content_type: None
*******************************************************************************

Checking distribution missing_long/dist/pkg-0.0.0.tar.gz: 
*******************************************************************************
description: None
description_content_type: 'text/markdown'
*******************************************************************************

Checking distribution missing_type/dist/pkg-0.0.0.tar.gz: 
*******************************************************************************
description: 'long description'
description_content_type: None
*******************************************************************************
```

Things to note here:

- wheel will set the description to `'UNKNOWN\n\n\n'` when it is missing
- wheel seems to always add `\n\n\n` to both the description and content type
- these fields appear to always be present, but are sometimes `None`

Patch:
diff --git a/twine/commands/check.py b/twine/commands/check.py
index 2037761..f71b2c9 100644
--- a/twine/commands/check.py
+++ b/twine/commands/check.py
@@ -33,7 +33,6 @@ from twine.package import PackageFile
 
 _RENDERERS = {
     None: readme_renderer.rst,  # Default if description_content_type is None
-    "": readme_renderer.rst,  # Default if description_content_type is None
     "text/plain": readme_renderer.txt,
     "text/x-rst": readme_renderer.rst,
     "text/markdown": readme_renderer.markdown,
@@ -85,28 +84,33 @@ def check(dists, output_stream=sys.stdout):
         package = PackageFile.from_filename(filename, comment=None)
 
         metadata = package.metadata_dictionary()
-        content_type, parameters = cgi.parse_header(
-            metadata.get("description_content_type") or ""
-        )
-
-        # Get the appropriate renderer
-        renderer = _RENDERERS.get(content_type, readme_renderer.txt)
-
-        # Actually render the given value
-        rendered = renderer.render(
-            metadata.get("description"), stream=stream, **parameters
-        )
+        description = metadata["description"]
+        description_content_type = metadata["description_content_type"]
 
-        if rendered is None:
-            failure = True
-            output_stream.write("Failed\n")
+        if description_content_type is None:
             output_stream.write(
-                "The project's long_description has invalid markup which will "
-                "not be rendered on PyPI. The following syntax errors were "
-                "detected:\n%s" % stream
+                'warning: `long_description_content_type` missing.  '
+                'defaulting to `text/x-rst`.\n'
             )
-        else:
+            description_content_type = 'text/x-rst'
+
+        content_type, params = cgi.parse_header(description_content_type)
+        renderer = _RENDERERS.get(content_type, _RENDERERS[None])
+
+        if description in {None, 'UNKNOWN\n\n\n'}:
+            output_stream.write('warning: `long_description` missing.\n')
             output_stream.write("Passed\n")
+        else:
+            if renderer.render(description, stream=stream, **params) is None:
+                failure = True
+                output_stream.write("Failed\n")
+                output_stream.write(
+                    "The project's long_description has invalid markup which "
+                    "will not be rendered on PyPI. The following syntax "
+                    "errors were detected:\n%s" % stream
+                )
+            else:
+                output_stream.write("Passed\n")
 
     return failure
 


Test patch:
diff --git a/tests/test_check.py b/tests/test_check.py
index 58bfbdf..deda340 100644
--- a/tests/test_check.py
+++ b/tests/test_check.py
@@ -58,11 +58,13 @@ def test_check_passing_distribution(monkeypatch):
     renderer = pretend.stub(
         render=pretend.call_recorder(lambda *a, **kw: "valid")
     )
-    package = pretend.stub(metadata_dictionary=lambda: {"description": "blah"})
+    package = pretend.stub(metadata_dictionary=lambda: {
+        "description": "blah", 'description_content_type': 'text/markdown',
+    })
     output_stream = check.StringIO()
     warning_stream = ""
 
-    monkeypatch.setattr(check, "_RENDERERS", {"": renderer})
+    monkeypatch.setattr(check, "_RENDERERS", {None: renderer})
     monkeypatch.setattr(check, "_find_dists", lambda a: ["dist/dist.tar.gz"])
     monkeypatch.setattr(
         check,
@@ -81,15 +83,41 @@ def test_check_passing_distribution(monkeypatch):
     ]
 
 
+def test_check_no_description(monkeypatch, capsys):
+    package = pretend.stub(metadata_dictionary=lambda: {
+        'description': None, 'description_content_type': None,
+    })
+
+    monkeypatch.setattr(check, "_find_dists", lambda a: ["dist/dist.tar.gz"])
+    monkeypatch.setattr(
+        check,
+        "PackageFile",
+        pretend.stub(from_filename=lambda *a, **kw: package),
+    )
+
+    # used to crash with `AttributeError`
+    output_stream = check.StringIO()
+    check.check("dist/*", output_stream=output_stream)
+    assert output_stream.getvalue() == (
+        'Checking distribution dist/dist.tar.gz: '
+        'warning: `long_description_content_type` missing.  '
+        'defaulting to `text/x-rst`.\n'
+        'warning: `long_description` missing.\n'
+        'Passed\n'
+    )
+
+
 def test_check_failing_distribution(monkeypatch):
     renderer = pretend.stub(
         render=pretend.call_recorder(lambda *a, **kw: None)
     )
-    package = pretend.stub(metadata_dictionary=lambda: {"description": "blah"})
+    package = pretend.stub(metadata_dictionary=lambda: {
+        "description": "blah", "description_content_type": 'text/markdown',
+    })
     output_stream = check.StringIO()
     warning_stream = "WARNING"
 
-    monkeypatch.setattr(check, "_RENDERERS", {"": renderer})
+    monkeypatch.setattr(check, "_RENDERERS", {None: renderer})
     monkeypatch.setattr(check, "_find_dists", lambda a: ["dist/dist.tar.gz"])
     monkeypatch.setattr(
         check,