@@ -177,7 +177,7 @@ def set(ctx, profile, **kwargs):
177177 def unset (ctx , profile , all , ** kwargs ):
178178 """
179179 Unset specific configuration fields or remove the entire profile from the config file.
180- Use `--all` to delete the entire profile, be carefull if you have unsoported /your own fileds under profile.
180+ Use `--all` to delete the entire profile, be careful if you have unsupported /your own fields under profile.
181181 """
182182
183183 config_manager = ConfigManager ()
@@ -200,7 +200,10 @@ def unset(ctx, profile, all, **kwargs):
200200 except ConfigError as e :
201201 raise FatalError (str (e ))
202202
203- if profile not in raw_data .get ('profiles' , CommentedMap ()):
203+ if 'profiles' not in raw_data :
204+ raw_data ['profiles' ] = CommentedMap ()
205+
206+ if profile not in raw_data ['profiles' ]:
204207 raise ConfigError (f"Profile '{ profile } ' does not exist." )
205208
206209 if all :
@@ -209,14 +212,25 @@ def unset(ctx, profile, all, **kwargs):
209212 print (f'Profile "{ profile } " was completely removed from the config file.' )
210213 return
211214
215+ # Track which fields were actually removed
216+ removed_fields = []
212217 for field in fields_to_unset :
213- del raw_data ['profiles' ][profile ][field ]
218+ # Fix: Check if field exists before trying to delete it
219+ if field in raw_data ['profiles' ][profile ]:
220+ del raw_data ['profiles' ][profile ][field ]
221+ removed_fields .append (field )
222+
223+ # Warn if no fields were actually removed
224+ if not removed_fields :
225+ print (f'No specified fields found in profile "{ profile } ".' )
226+ return
214227
215228 # Remove the profile if it becomes empty
216229 if not raw_data ['profiles' ][profile ]:
217230 del raw_data ['profiles' ][profile ]
231+
218232 _write_config (config_path , raw_data , yaml )
219233
220- print (f'Successfully removed { ", " .join (fields_to_unset )} from the profile "{ profile } ".' )
234+ print (f'Successfully removed { ", " .join (removed_fields )} from the profile "{ profile } ".' )
221235
222236 return config
0 commit comments