Skip to content

Disable the stdlib distribution finder when backport is present

Jason R. Coombs requested to merge feature/91-override-stdlib into master

This change takes the (admittedly sketchy) approach of disabling the stdlib DistributionFinder functionality from the stdlib PathFinder (effectively taking over for finding Distribution objects). On one hand, this approach fixes a couple of issues that have emerged since the Python 3.8 implementation stabilized (#100 (closed), #91 (closed)). On the other hand, it feels dangerous and dirty. It effectively bypasses the stdlib behavior, which could be a good thing, given that this backport is meant to be suitable to supersede the stdlib behavior (supplying future functionality even on Python 3.8).

I did confirm that the API functionality of the stdlib behavior continues to function as expected:

$ .tox/python/bin/python                                                                                                    
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib.metadata
>>> import importlib_metadata
>>> importlib.metadata.version('importlib_metadata')
'0.24.dev2+gc471831.d20191130'

The biggest risk of this change, in my opinion, is that of a potential race with importing of importlib_metadata. Consider for example the case where some code first finds distributions using stdlib, then later some other code imports the backport, then the code once again tries to find distributons--it will get a different result than originally.

$ .tox/python/bin/python                                                                                                    
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib.metadata
>>> importlib.metadata.distribution('importlib_metadata')
<importlib.metadata.PathDistribution object at 0x10c861370>
>>> import importlib_metadata
>>> importlib.metadata.distribution('importlib_metadata')
<importlib_metadata.PathDistribution object at 0x10c5d0a60>

My instinct is that's low risk, and it has the benefit of giving the backport primacy (for Distribution discovery) when imported. The most unfortunate part is that this behavior must necessarily be implicit and could be invoked by even the most innocent bystander library (any project that chooses to import importlib_metadata on Python 3.8+ will alter the global state).

I note that this change does not address the duplicate metadata condition described here, but I believe that condition should be addressed separately.

Merge request reports