summaryrefslogtreecommitdiff
path: root/setuptools/config/_apply_pyprojecttoml.py
diff options
context:
space:
mode:
authorAnderson Bravalheri <andersonbravalheri@gmail.com>2022-02-23 02:57:49 +0000
committerAnderson Bravalheri <andersonbravalheri@gmail.com>2022-03-05 14:30:52 +0000
commita4b474ecb7ca027ba06b351b254ee57725184ee3 (patch)
treeedefed763c2f5cbd64823be0ec384a3452fd829f /setuptools/config/_apply_pyprojecttoml.py
parentcf32acbcc180938cf665ba1dfa65243bb8e2277f (diff)
downloadpython-setuptools-git-a4b474ecb7ca027ba06b351b254ee57725184ee3.tar.gz
Back-fill Description-Content-Type according to readme suffix
According to PEP 621, the backend should fill-in the content-type if the `readme` field is passed as a string. The value is derived from the extension of the file (an error should be raised when that is not possible). Previously all READMEs were wrongly assumed rst. This error was reported in: https://discuss.python.org/t/help-testing-experimental-features-in-setuptools/13821/4
Diffstat (limited to 'setuptools/config/_apply_pyprojecttoml.py')
-rw-r--r--setuptools/config/_apply_pyprojecttoml.py25
1 files changed, 23 insertions, 2 deletions
diff --git a/setuptools/config/_apply_pyprojecttoml.py b/setuptools/config/_apply_pyprojecttoml.py
index 0d2ead88..f711c8a2 100644
--- a/setuptools/config/_apply_pyprojecttoml.py
+++ b/setuptools/config/_apply_pyprojecttoml.py
@@ -74,18 +74,39 @@ def _set_config(dist: "Distribution", field: str, value: Any):
setattr(dist, field, value)
+_CONTENT_TYPES = {
+ ".md": "text/markdown",
+ ".rst": "text/x-rst",
+ ".txt": "text/plain",
+}
+
+
+def _guess_content_type(file: str) -> Optional[str]:
+ _, ext = os.path.splitext(file.lower())
+ if not ext:
+ return None
+
+ if ext in _CONTENT_TYPES:
+ return _CONTENT_TYPES[ext]
+
+ valid = ", ".join(f"{k} ({v})" for k, v in _CONTENT_TYPES.items())
+ msg = f"only the following file extensions are recognized: {valid}."
+ raise ValueError(f"Undefined content type for {file}, {msg}")
+
+
def _long_description(dist: "Distribution", val: _DictOrStr, root_dir: _Path):
from setuptools.config import expand
if isinstance(val, str):
text = expand.read_files(val, root_dir)
- ctype = "text/x-rst"
+ ctype = _guess_content_type(val)
else:
text = val.get("text") or expand.read_files(val.get("file", []), root_dir)
ctype = val["content-type"]
_set_config(dist, "long_description", text)
- _set_config(dist, "long_description_content_type", ctype)
+ if ctype:
+ _set_config(dist, "long_description_content_type", ctype)
def _license(dist: "Distribution", val: Union[str, dict], _root_dir):