You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix mimetypes.init() to account for from import #79119
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=Noneclosed_at=Nonecreated_at=<Date2018-10-08.22:13:09.185>labels= ['type-bug', '3.10']
title='Fix mimetype.init() to account for from import'updated_at=<Date2021-08-13.04:01:58.660>user='https://github.com/YoSTEALTH'
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).
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.
) 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.
YoSTEALTH commented on Dec 20, 2019
YoSTEALTHmannequin
MannequinAuthor
More actions
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)
AWhetter commented on Dec 30, 2019
AWhettermannequin
Mannequin
More actions
Yes I'm happy to make those changes as part of this.
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.
Add a clarification to the add_type() docs that changes made with strict=True means that they will persist after subsequent calls to init().
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?
Activity
YoSTEALTH commentedon Oct 8, 2018
When a user uses from import, there is a flaw in how mimetype.init() updates its global references.
# Option-1 (flawed)
# -----------------
# Option-2
# --------
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_mapfrom being updated and using oldtypes_mapreference.Potential solution would be to
types_map.update(new dict content)vs reassigning the variable.zooba commentedon Oct 4, 2019
Adding email team here, since you were pinged on the PR.
PR 16567 doesn't fix the
initedmember, 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
initedwon't be updated if you'vefrom ... imported it (which would normally be obvious, except that now some other values will be updated).Also, the
initdocs say that it will only be completely rebuilt whenfilesisNone(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).
AWhetter commentedon Oct 28, 2019
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
initdoes rebuild whenfilesisNone(cpython/Lib/mimetypes.py
Line 350 in edb172a
add_type. At the moment it saysthe 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 toinitwould make that clear.As for importing
initinto the local scope, I think ideally we wouldn't useglobalat all. We should be able to makeinitimportmimetypeslocally 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 globalMimetypesobject stored inmimetypes._db. But that would still require usage of aglobal _db.YoSTEALTH commentedon Dec 20, 2019
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_defaultcould be constants likeSUFFIX_MAP_DEFAULTwithsuffix_map = SUFFIX_MAP_DEFAULT.copy()Ashley if you feel like making these changes (optional)
AWhetter commentedon Dec 30, 2019
Yes I'm happy to make those changes as part of this.
So clarify what needs to change in PR 16567:
Include a note in the docs for
initedthat outlines that if it is imported into the local scope, it will not be updated by calls toinit(). Onlymimetypes.initedwill.Add a clarification to the
add_type()docs that changes made withstrict=Truemeans that they will persist after subsequent calls toinit().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 toOFFICIAL_SUFFIX_MAPfor example?csabella commentedon May 23, 2020
@steve.dower, Mariatta had a question for you on the PR.
akulakov commentedon Aug 13, 2021
How about returning the updated dicts from init() and showing an example in the docs of such usage?
[-]Fix mimetype.init() to account for from import[/-][+]Fix mimetypes.init() to account for from import[/+]