2525from .. import __version__
2626from ..types import Plugin , Schema
2727
28+ _DEFAULT_MULTI_PRIORITY = 0
29+ _DEFAULT_TOOL_PRIORITY = 1
30+
2831
2932class PluginProtocol (Protocol ):
3033 @property
@@ -42,6 +45,9 @@ def help_text(self) -> str: ...
4245 @property
4346 def fragment (self ) -> str : ...
4447
48+ @property
49+ def priority (self ) -> float : ...
50+
4551
4652class PluginWrapper :
4753 def __init__ (self , tool : str , load_fn : Plugin ):
@@ -64,6 +70,10 @@ def schema(self) -> Schema:
6470 def fragment (self ) -> str :
6571 return ""
6672
73+ @property
74+ def priority (self ) -> float :
75+ return getattr (self ._load_fn , "priority" , _DEFAULT_TOOL_PRIORITY )
76+
6777 @property
6878 def help_text (self ) -> str :
6979 tpl = self ._load_fn .__doc__
@@ -79,10 +89,11 @@ def __str__(self) -> str:
7989
8090
8191class StoredPlugin :
82- def __init__ (self , tool : str , schema : Schema , source : str ):
92+ def __init__ (self , tool : str , schema : Schema , source : str , priority : float ):
8393 self ._tool , _ , self ._fragment = tool .partition ("#" )
8494 self ._schema = schema
8595 self ._source = source
96+ self ._priority = priority
8697
8798 @property
8899 def id (self ) -> str :
@@ -100,6 +111,10 @@ def schema(self) -> Schema:
100111 def fragment (self ) -> str :
101112 return self ._fragment
102113
114+ @property
115+ def priority (self ) -> float :
116+ return self ._priority
117+
103118 @property
104119 def help_text (self ) -> str :
105120 return self .schema .get ("description" , "" )
@@ -161,23 +176,34 @@ def load_from_multi_entry_point(
161176 except Exception as ex :
162177 raise ErrorLoadingPlugin (entry_point = entry_point ) from ex
163178
179+ priority = output .get ("priority" , _DEFAULT_MULTI_PRIORITY )
164180 for tool , schema in output ["tools" ].items ():
165- yield StoredPlugin (tool , schema , f"{ id_ } :{ tool } " )
181+ yield StoredPlugin (tool , schema , f"{ id_ } :{ tool } " , priority )
166182 for i , schema in enumerate (output .get ("schemas" , [])):
167- yield StoredPlugin ("" , schema , f"{ id_ } :{ i } " )
183+ yield StoredPlugin ("" , schema , f"{ id_ } :{ i } " , priority )
168184
169185
170186class _SortablePlugin (NamedTuple ):
171- priority : int
172187 name : str
173188 plugin : Union [PluginWrapper , StoredPlugin ]
174189
175190 def key (self ) -> str :
176191 return self .plugin .tool or self .plugin .id
177192
178193 def __lt__ (self , other : Any ) -> bool :
179- return (self .priority , self .name , self .key ()) < (
180- other .priority ,
194+ # **Major concern**:
195+ # Consistency and reproducibility on which entry-points have priority
196+ # for a given environment.
197+ # The plugin with higher priority overwrites the schema definition.
198+ # The exact order that they are listed itself is not important for now.
199+ # **Implementation detail**:
200+ # By default, "single tool plugins" have priority 1 and "multi plugins"
201+ # have priority 0.
202+ # The order that the plugins will be listed is inverse to the priority.
203+ # If 2 plugins have the same numerical priority, the one whose
204+ # entry-point name is "higher alphabetically" wins.
205+ return (self .plugin .priority , self .name , self .key ()) < (
206+ other .plugin .priority ,
181207 other .name ,
182208 other .key (),
183209 )
@@ -194,22 +220,13 @@ def list_from_entry_points(
194220 loaded and included (or not) in the final list. A ``True`` return means the
195221 plugin should be included.
196222 """
197- # **Major concern**:
198- # Consistency and reproducibility on which entry-points have priority
199- # for a given environment.
200- # The plugin with higher priority overwrites the schema definition.
201- # The exact order itself is not important for now.
202- # **Implementation detail**:
203- # Tool plugins are loaded first, so they are listed first than other schemas,
204- # but multi plugins always have priority, overwriting the tool schemas.
205- # The "higher alphabetically" an entry-point name, the more priority.
206223 tool_eps = (
207- _SortablePlugin (0 , e .name , load_from_entry_point (e ))
224+ _SortablePlugin (e .name , load_from_entry_point (e ))
208225 for e in iterate_entry_points ("validate_pyproject.tool_schema" )
209226 if filtering (e )
210227 )
211228 multi_eps = (
212- _SortablePlugin (1 , e .name , p )
229+ _SortablePlugin (e .name , p )
213230 for e in iterate_entry_points ("validate_pyproject.multi_schema" )
214231 for p in load_from_multi_entry_point (e )
215232 if filtering (e )
0 commit comments