Skip to content

Fix mimetypes.init() to account for from import #79119

Open

Description

@YoSTEALTH
mannequin
BPO 34938
Nosy @warsaw, @bitdancer, @zooba, @maxking, @YoSTEALTH, @csabella, @tirkarthi, @brandtbucher, @AWhetter, @akulakov
PRs
  • bpo-34938: mimetypes.init() does not overwrite globals #16567
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2018-10-08.22:13:09.185>
    labels = ['type-bug', '3.10']
    title = 'Fix mimetype.init() to account for from import'
    updated_at = <Date 2021-08-13.04:01:58.660>
    user = 'https://github.com/YoSTEALTH'

    bugs.python.org fields:

    activity = <Date 2021-08-13.04:01:58.660>
    actor = 'andrei.avk'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2018-10-08.22:13:09.185>
    creator = 'YoSTEALTH'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34938
    keywords = ['patch']
    message_count = 7.0
    messages = ['327375', '353966', '355581', '358718', '359070', '369746', '399490']
    nosy_count = 10.0
    nosy_names = ['barry', 'r.david.murray', 'steve.dower', 'maxking', 'YoSTEALTH', 'cheryl.sabella', 'xtreak', 'brandtbucher', 'AWhetter', 'andrei.avk']
    pr_nums = ['16567']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue34938'
    versions = ['Python 3.10']

    Activity

    1. YoSTEALTH commented on Oct 8, 2018

      YoSTEALTHmannequin
      MannequinAuthor

      When a user uses from import, there is a flaw in how mimetype.init() updates its global references.

      # Option-1 (flawed)
      # -----------------

      from mimetypes import init, types_map
      
      print(types_map.get('.gz'))  # None
      init()  # <- initialize
      print(types_map.get('.gz'))  # None

      # Option-2
      # --------

      import mimetypes
      
      print(mimetypes.types_map.get('.gz'))  # None
      mimetypes.init()  # <- initialize
      print(mimetypes.types_map.get('.gz'))  # application/gzip

      As you can see in https://github.com/python/cpython/blob/master/Lib/mimetypes.py#L344 line:358 global reference is reassigned and thus it prevents from mimetype import types_map from being updated and using old types_map reference.

      Potential solution would be to types_map.update(new dict content) vs reassigning the variable.

    2. added
      3.7 (EOL)
      3.8 (EOL)
      type-bug
      on Oct 8, 2018
    3. zooba commented on Oct 4, 2019

      @zooba
      Member

      Adding email team here, since you were pinged on the PR.

      PR 16567 doesn't fix the inited member, which is considerably more difficult to do, unfortunately, but it does mean that it's out of sync with the values you hold. Arguably this is a better state to be in though.

      Unfortunately, the "internal" data structures referred to in the docs1 are all documented public API, which means we can't just ignore this issue :( We ought to at least update the docs to mention that inited won't be updated if you've from ... imported it (which would normally be obvious, except that now some other values will be updated).

      Also, the init docs say that it will only be completely rebuilt when files is None (and specifically calls out passing an empty list as a way to avoid this happening). I'm pretty sure we don't respect this now.

      All up, I think we have more work to do here to get the behaviour right. Alternatively, we can make changes in 3.9 to specify better usage (such as explicitly discouraging direct use of the dictionaries and adding more helper functions if necessary).

    4. AWhetter commented on Oct 28, 2019

      AWhettermannequin
      Mannequin

      Maybe updating the dictionaries isn't right at all.
      I think if you were experienced enough you would have the intuition that mutable attributes like this are not going to change in your local scope if you import them into it. But relying on this knowledge makes it difficult for newer users. Plus we all make mistakes ;) So a note in the docs sounds like a good idea to me.

      I think init does rebuild when files is None (

      if files is None or _db is None:
      ) but it's not clear to me whether rebuilding should include any additional types registered with add_type. At the moment it says the mapping will be added to the official MIME types, but I think an additional note that this means that it will persist after subsequent calls to init would make that clear.

      As for importing init into the local scope, I think ideally we wouldn't use global at all. We should be able to make init import mimetypes locally and explicitly access attributes on the module?
      Another option is we make the module attributes accessible through a module level __getattr__ which gets the attributes off of the global Mimetypes object stored in mimetypes._db. But that would still require usage of a global _db.

    5. YoSTEALTH commented on Dec 20, 2019

      YoSTEALTHmannequin
      MannequinAuthor

      I didn't receive any notification of replay on this topic.

      _default_mime_types() should never have been a function. This function should be safe to remove as its an internal function. This would avoid unneeded function call and globals used.

      Stuff like _suffix_map_default could be constants like SUFFIX_MAP_DEFAULT with suffix_map = SUFFIX_MAP_DEFAULT.copy()

      Ashley if you feel like making these changes (optional)

    6. AWhetter commented on Dec 30, 2019

      AWhettermannequin
      Mannequin

      Yes I'm happy to make those changes as part of this.

      So clarify what needs to change in PR 16567:

      1. Include a note in the docs for inited that outlines that if it is imported into the local scope, it will not be updated by calls to init(). Only mimetypes.inited will.

      2. Add a clarification to the add_type() docs that changes made with strict=True means that they will persist after subsequent calls to init().

      3. Remove _default_mime_types() in favour of public global constants. The documentation currently refers to these globals as the "official MIME types". Should that wording change to use "default" instead of "official", or maybe should the names of the constants change to OFFICIAL_SUFFIX_MAP for example?

    7. csabella commented on May 23, 2020

      @csabella
      Contributor

      @steve.dower, Mariatta had a question for you on the PR.

    8. added
      3.10 (EOL)
      and removed
      3.7 (EOL)
      3.8 (EOL)
      3.9 (EOL)
      on Jul 6, 2020
    9. akulakov commented on Aug 13, 2021

      @akulakov
      Contributor

      How about returning the updated dicts from init() and showing an example in the docs of such usage?

    10. transferred this issue fromon Apr 10, 2022
    11. added
      stdlib
      on Nov 27, 2023
    12. changed the title [-]Fix mimetype.init() to account for from import[/-] [+]Fix mimetypes.init() to account for from import[/+] on May 31, 2026
    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

      stdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions