@@ -50,8 +50,10 @@ def project_exists(response, project):
5050
5151
5252def model_exists (project , name , force , versionName = "latest" ):
53- """Checks if model already exists in the same project and either raises an error or deletes
54- the redundant model. If no project version is provided, the version is assumed to be "latest".
53+ """
54+ Checks if model already exists in the same project and either raises an error or
55+ deletes the redundant model. If no project version is provided, the version is
56+ assumed to be "latest".
5557
5658 Parameters
5759 ----------
@@ -62,14 +64,15 @@ def model_exists(project, name, force, versionName="latest"):
6264 force : bool, optional
6365 Sets whether to overwrite models with the same name upon upload.
6466 versionName : str, optional
65- Name of project version to check if a model of the same name already exists. Default
66- value is "latest".
67+ Name of project version to check if a model of the same name already exists.
68+ Default value is "latest".
6769
6870 Raises
6971 ------
7072 ValueError
71- Model repository API cannot overwrite an already existing model with the upload model call.
72- Alerts user of the force argument to allow multi-call API overwriting.
73+ Model repository API cannot overwrite an already existing model with the upload
74+ model call. Alerts user of the force argument to allow multi-call API
75+ overwriting.
7376 """
7477 project = mr .get_project (project )
7578 projectId = project ["id" ]
@@ -80,12 +83,17 @@ def model_exists(project, name, force, versionName="latest"):
8083 versionId = projectVersions [latestVersion ]["id" ]
8184 else :
8285 for version in projectVersions :
83- if versionName is version ["name" ]:
86+ if versionName == version ["name" ]:
8487 versionId = version ["id" ]
8588 break
86- projectModels = mr .get (
87- "/projects/{}/projectVersions/{}/models" .format (projectId , versionId )
88- )
89+ try :
90+ projectModels = mr .get (
91+ "/projects/{}/projectVersions/{}/models" .format (projectId , versionId )
92+ )
93+ except UnboundLocalError :
94+ raise ValueError (
95+ f"The project version { versionName } was not found in project { project } ."
96+ )
8997
9098 for model in projectModels :
9199 # Throws a TypeError if only one model is in the project
@@ -95,19 +103,19 @@ def model_exists(project, name, force, versionName="latest"):
95103 mr .delete_model (model .id )
96104 else :
97105 raise ValueError (
98- "A model with the same model name exists in project {}. Include the force=True argument to overwrite models with the same name." . format (
99- project . name
100- )
106+ "A model with the same model name exists in project {}. "
107+ "Include the force=True argument to overwrite models with the "
108+ "same name." . format ( project . name )
101109 )
102110 except TypeError :
103111 if projectModels ["name" ] == name :
104112 if force :
105113 mr .delete_model (projectModels .id )
106114 else :
107115 raise ValueError (
108- "A model with the same model name exists in project {}. Include the force=True argument to overwrite models with the same name." . format (
109- project . name
110- )
116+ "A model with the same model name exists in project {}. "
117+ "Include the force=True argument to overwrite models with the "
118+ "same name." . format ( project . name )
111119 )
112120
113121
0 commit comments