1
1
from __future__ import annotations
2
2
3
+ from collections import defaultdict
4
+ from functools import reduce
3
5
from typing import TYPE_CHECKING
6
+ from typing import Any
4
7
5
8
from cleo .helpers import option
6
9
@@ -88,6 +91,37 @@ def _validate_readme(self, readme: str | list[str], poetry_file: Path) -> list[s
88
91
errors .append (f"Declared README file does not exist: { name } " )
89
92
return errors
90
93
94
+ def _validate_dependencies_source (self , config : dict [str , Any ]) -> list [str ]:
95
+ """Check dependencies's source are valid"""
96
+ dependency_sources : dict [str , set [str ]] = defaultdict (set )
97
+ sources = {k ["name" ] for k in config .get ("source" , [])}
98
+ errors = []
99
+
100
+ dependency_declarations : list [dict [str , str | dict [str , str ]]] = []
101
+ # scan dependencies and group dependencies settings in pyproject.toml
102
+ if "dependencies" in config :
103
+ dependency_declarations .append (config ["dependencies" ])
104
+
105
+ for group in config .get ("group" , {}).values ():
106
+ if "dependencies" in group :
107
+ dependency_declarations .append (group ["dependencies" ])
108
+
109
+ for dependency_declaration in dependency_declarations :
110
+ for dependency , declaration in dependency_declaration .items ():
111
+ if isinstance (declaration , dict ) and "source" in declaration :
112
+ dependency_sources [dependency ].add (declaration ["source" ])
113
+
114
+ all_referenced_sources : set [str ] = reduce (
115
+ lambda i , j : i | j , dependency_sources .values (), set ()
116
+ )
117
+ if all_referenced_sources not in sources :
118
+ errors .extend ([
119
+ f'Invalid source "{ source } " referenced in dependencies.'
120
+ for source in all_referenced_sources - sources
121
+ ])
122
+
123
+ return errors
124
+
91
125
def handle (self ) -> int :
92
126
from poetry .factory import Factory
93
127
from poetry .pyproject .toml import PyProjectTOML
@@ -108,6 +142,8 @@ def handle(self) -> int:
108
142
errors = self ._validate_readme (config ["readme" ], poetry_file )
109
143
check_result ["errors" ].extend (errors )
110
144
145
+ check_result ["errors" ] += self ._validate_dependencies_source (config )
146
+
111
147
# Verify that lock file is consistent
112
148
if self .option ("lock" ) and not self .poetry .locker .is_locked ():
113
149
check_result ["errors" ] += ["poetry.lock was not found." ]
0 commit comments