Open e40d1192-a6a4-43aa-b414-855b7d3116ac opened 7 years ago
import mimetypes
print(mimetypes.guess_type('foo.manifest'))
mimetypes.add_type('text/plain', 'manifest')
print(mimetypes.guess_type('foo.manifest'))
results in:
('application/x-ms-manifest', None)
('application/x-ms-manifest', None)
I (mistakenly) expected the latter print to give me "('text/plain', None)". It doesn't because I should have prepended a . to the second add_type argument.
I think add_type should error out when given an extension without a dot-prefix, because it's extremely unlikely that code that does so is behaving as intended with the current implementation.
(At the very least, documentation should be updated to make this expectation clearer.)
Ideally, use .startswith('.') Instead if find, but this is a clear UI fix. First pull request fixes doc, second fixes code. OK to apply both.
I think we should deprecate calling add_type
with an empty ext
, as I don't think it's a feature, more a side effect of the current implementation.
But as some may use it like a feature, I asked the current PR to emit a warning, to not break the current implementations.
To be able to simplify in the future, why not deprecating empty ext
so we can remove it in a few versions?
I just found a case where the empty mime type is actually usefull, it's in Lib/http/server.py:
extensions_map = mimetypes.types_map.copy()
extensions_map.update({
'': 'application/octet-stream', # Default
'.py': 'text/plain',
'.c': 'text/plain',
'.h': 'text/plain',
})
It does *not* uses add_type, but demos the fact that the empty type may be usefull from time to time, maybe don't warn and don't deprecate it at all.
Checking the top 8k PyPI projects:
Of the 77 calls (in 26 projects) to mimetypes.add_type
:
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = None closed_at = None created_at =
labels = ['3.7', 'library', 'docs']
title = 'mimetypes.add_type should complain when you give it an undotted ext'
updated_at =
user = 'https://github.com/OddBloke'
```
bugs.python.org fields:
```python
activity =
actor = 'mdk'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation', 'Library (Lib)']
creation =
creator = 'odd_bloke'
dependencies = []
files = []
hgrepos = []
issue_num = 31040
keywords = []
message_count = 4.0
messages = ['299135', '306501', '352266', '352286']
nosy_count = 5.0
nosy_names = ['odd_bloke', 'docs@python', 'Jim.Jewett', 'mdk', 'rcj']
pr_nums = ['2871', '2895']
priority = 'normal'
resolution = None
stage = 'commit review'
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue31040'
versions = ['Python 3.7']
```