Skip to content

importlib.metadata.metadata("package") fails with email.errors.HeaderParseError when README contains valid markdown link #119650

@hcording

Description

@hcording

Bug report

Bug description:

Here is a minimal example project (tested on Python 3.10):

pyproject.toml

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "test"
readme = "README.md"
version = "0.1.0"

README.md

# Test

[MIT](<https://choosealicense.com/licenses/mit/>)

test package:

  • __init__.py (empty)
  • bug.py: import importlib.metadata; print(importlib.metadata.metadata("test"))

To reproduce:
pip install -e .
python test/bug.py

Traceback:

Traceback (most recent call last):
  File "bug.py", line 1, in <module>
    import importlib.metadata; print(importlib.metadata.metadata("test"))
  File "lib/python3.10/email/message.py", line 135, in __str__
    return self.as_string()
  File "lib/python3.10/email/message.py", line 158, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "/home/mint/miniforge3/lib/python3.10/email/generator.py", line 116, in flatten
    self._write(msg)
  File "lib/python3.10/email/generator.py", line 199, in _write
    self._write_headers(msg)
  File "lib/python3.10/email/generator.py", line 226, in _write_headers
    self.write(self.policy.fold(h, v))
  File "lib/python3.10/email/_policybase.py", line 326, in fold
    return self._fold(name, value, sanitize=True)
  File "lib/python3.10/email/_policybase.py", line 369, in _fold
    parts.append(h.encode(linesep=self.linesep, maxlinelen=maxlinelen))
  File "lib/python3.10/email/header.py", line 389, in encode
    raise HeaderParseError("header value appears to contain "
email.errors.HeaderParseError: header value appears to contain an embedded header: '# Test\n[MIT](<https://choosealicense.com/licenses/mit/>)'

It appears that [MIT] is seen as a header, as it would be in a TOML file. That's my best guess really. Any help is appreciated!

CPython versions tested on:

3.10

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions