Skip to content

mimetypes.guess_type could not find mimetypes added by mimetypes.add_type i.e. in PY3.10.4 // OR : different return values (PY2.7 to PY3.9.7) vs (PY3.9.11 to PY3.10.4) #92455

Open
@ifrh

Description

@ifrh

I think something is broken in mimetype.guess_type or documentation did not cover the change ...

A small working example and its outputs for different Python versions are listed below.
In this example the value from a concrete mimetypes.types_map entry should
match to the returnvalue of mimetypes.guess_type in all tested Python versions:
But it did not; the return value of mimetypes.guess_type is wrong from my point of view.

Operating system and architecture:
Windows 10/WSL

$ uname -s -v -i -m -p -o
Linux #1237-Microsoft Sat Sep 11 14:32:00 PST 2021 x86_64 x86_64 x86_64 GNU/Linux

Ubuntu

$ uname -s -v -i -m -p -o
Linux #220-Ubuntu SMP Thu May 9 12:40:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Small working example : mimetest.py

import mimetypes
mimetypes.init()
MIMETYPE_ADDITIONAL_EXTENSIONS = [("text/x-r-script", ".R"), ]
for (mimetype, extension) in MIMETYPE_ADDITIONAL_EXTENSIONS:
    mimetypes.add_type(mimetype, extension, strict=True)
print("mimetypes.types_map['.R'] = %s , mimetypes.guess_type('example.R')[0] = %s" % (mimetypes.types_map['.R'],mimetypes.guess_type('example.R')[0])) 

Running this via multiple Python versions is showing the problem:

Python 2.7.17
mimetypes.types_map['.R'] = text/x-r-script , mimetypes.guess_type('example.R')[0] = text/x-r-script
Python 3.5.6
mimetypes.types_map['.R'] = text/x-r-script , mimetypes.guess_type('example.R')[0] = text/x-r-script
Python 3.6.13
mimetypes.types_map['.R'] = text/x-r-script , mimetypes.guess_type('example.R')[0] = text/x-r-script
Python 3.9.7
mimetypes.types_map['.R'] = text/x-r-script , mimetypes.guess_type('example.R')[0] = text/x-r-script
Python 3.9.11
mimetypes.types_map['.R'] = text/x-r-script , mimetypes.guess_type('example.R')[0] = None
Python 3.9.12
mimetypes.types_map['.R'] = text/x-r-script , mimetypes.guess_type('example.R')[0] = None
Python 3.10.4
mimetypes.types_map['.R'] = text/x-r-script , mimetypes.guess_type('example.R')[0] = None

perhaps this different behavior is a result of issue #64591
and its related merged pull requests

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions