11import re
22import sys
33
4+ from packaging .markers import default_environment
45from packaging .requirements import Requirement
56
67try :
1011
1112
1213class DistributionCache :
13- def __init__ (self , sys_path = None ):
14- self ._resolver = Distribution .discover (context = DistributionFinder .Context (path = sys_path or sys . path ))
14+ def __init__ (self , sys_path ):
15+ self ._resolver = Distribution .discover (context = DistributionFinder .Context (path = sys_path ))
1516 self ._distributions = {}
1617 self ._search_exhausted = False
1718 self ._canonical_regex = re .compile (r'[-_.]+' )
@@ -35,7 +36,10 @@ def __getitem__(self, item):
3536 self ._search_exhausted = True
3637
3738
38- def dependency_in_sync (requirement , installed_distributions ):
39+ def dependency_in_sync (requirement , environment , installed_distributions ):
40+ if requirement .marker and not requirement .marker .evaluate (environment ):
41+ return True
42+
3943 distribution = installed_distributions [requirement .name ]
4044 if distribution is None :
4145 return False
@@ -58,10 +62,10 @@ def dependency_in_sync(requirement, installed_distributions):
5862 # extra and it's just a user error/typo. See: https://github.com/pypa/pip/issues/7122
5963 if extra not in available_extras :
6064 return False
61- elif not transitive_requirement .marker .evaluate ({'extra' : extra }):
62- continue
6365
64- if not dependency_in_sync (transitive_requirement , installed_distributions ):
66+ extra_environment = dict (environment )
67+ extra_environment ['extra' ] = extra
68+ if not dependency_in_sync (transitive_requirement , extra_environment , installed_distributions ):
6569 return False
6670
6771 if requirement .specifier and not requirement .specifier .contains (distribution .version ):
@@ -93,6 +97,11 @@ def dependency_in_sync(requirement, installed_distributions):
9397 return True
9498
9599
96- def dependencies_in_sync (requirements , sys_path = None ):
100+ def dependencies_in_sync (requirements , sys_path = None , environment = None ):
101+ if sys_path is None :
102+ sys_path = sys .path
103+ if environment is None :
104+ environment = default_environment ()
105+
97106 installed_distributions = DistributionCache (sys_path )
98- return all (dependency_in_sync (requirement , installed_distributions ) for requirement in requirements )
107+ return all (dependency_in_sync (requirement , environment , installed_distributions ) for requirement in requirements )
0 commit comments