Repository: Lightning-AI/pytorch-lightning
File: pytorch_lightning/loggers/comet.py (Python)

Code before:
            # Comet.ml rest API, used to determine version number
            self.rest_api_key = rest_api_key
            self.comet_api = API(self.rest_api_key)
        else:
            self.rest_api_key = None
            self.comet_api = None

        self._kwargs = kwargs

    @property
    @rank_zero_experiment
    def experiment(self):
        r"""
        Actual Comet object. To use Comet features in your
        :class:`~pytorch_lightning.core.lightning.LightningModule` do the following.

        Example::

            self.logger.experiment.some_comet_function()

        """
        if self._experiment is not None:
            return self._experiment

        manual_set_experiment_key_environment_variable = self._future_experiment_key is not None
        if manual_set_experiment_key_environment_variable:
            os.environ["COMET_EXPERIMENT_KEY"] = self._future_experiment_key
            self._future_experiment_key = None

        try:
            if self.mode == "online":
                if self._experiment_key is None:
                    self._experiment = CometExperiment(
                        api_key=self.api_key,
                        project_name=self._project_name,
                        **self._kwargs,
                    )
                    self._experiment_key = self._experiment.get_key()
                else:
                    self._experiment = CometExistingExperiment(
                        api_key=self.api_key,
                        project_name=self._project_name,
                        previous_experiment=self._experiment_key,
                        **self._kwargs,
                    )
            else:
                self._experiment = CometOfflineExperiment(
                    offline_directory=self.save_dir,
                    project_name=self._project_name,
                    **self._kwargs,
                )

Review comment (suggestion):
This looks good, we can also move the cleaning of `self._future_experiment_key` in the finally block, this way we don't need an additional variable.

Code after:
            # Comet.ml rest API, used to determine version number
            self.rest_api_key = rest_api_key
            self.comet_api = API(self.rest_api_key)
        else:
            self.rest_api_key = None
            self.comet_api = None

        self._kwargs = kwargs

    @property
    @rank_zero_experiment
    def experiment(self):
        r"""
        Actual Comet object. To use Comet features in your
        :class:`~pytorch_lightning.core.lightning.LightningModule` do the following.

        Example::

            self.logger.experiment.some_comet_function()

        """
        if self._experiment is not None:
            return self._experiment

        if self._future_experiment_key is not None:
            os.environ["COMET_EXPERIMENT_KEY"] = self._future_experiment_key

        try:
            if self.mode == "online":
                if self._experiment_key is None:
                    self._experiment = CometExperiment(
                        api_key=self.api_key,
                        project_name=self._project_name,
                        **self._kwargs,
                    )
                    self._experiment_key = self._experiment.get_key()
                else:
                    self._experiment = CometExistingExperiment(
                        api_key=self.api_key,
                        project_name=self._project_name,
                        previous_experiment=self._experiment_key,
                        **self._kwargs,
                    )
            else:
                self._experiment = CometOfflineExperiment(
                    offline_directory=self.save_dir,
                    project_name=self._project_name,
                    **self._kwargs,
                )
        finally:
            if self._future_experiment_key is not None: