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

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

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions