# Buggy code
def finished(self, job, keep_metadata=True):
    if not keep_metadata:
        for f in job.expanded_output:
            self._delete_record(self._incomplete_path, f)
        return

    version = str(job.rule.version) if job.rule.version is not None else None
    code = self._code(job.rule)
    input = self._input(job)
    log = self._log(job)
    params = self._params(job)
    shellcmd = job.shellcmd
    conda_env = self._conda_env(job)
    fallback_time = time.time()
    for f in job.expanded_output:
        rec_path = self._record_path(self._incomplete_path, f)
        starttime = os.path.getmtime(rec_path) if os.path.exists(rec_path) else None
        endtime = f.mtime.local_or_remote() if f.exists else fallback_time
        self._record(
            self._metadata_path,
            {
                "version": version,
                "code": code,
                "rule": job.rule.name,
                "input": input,
                "log": log,
                "params": params,
                "shellcmd": shellcmd,
                "incomplete": False,
                "starttime": starttime,
                "endtime": endtime,
                "job_hash": hash(job),
                "conda_env": conda_env,
                "container_img_url": job.container_img_url,
            },
            f,
        )
        self._delete_record(self._incomplete_path, f)


# Traceback (TypeError)
user@:/analysis$ snakemake --cores 1 -pr --report test.html all
Building DAG of jobs...
Creating report...
Adding shell_rule.txt (0 MB).
Adding run_rule.txt (0 MB).
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/snakemake/__init__.py", line 668, in snakemake
success = workflow.execute(
File "/usr/local/lib/python3.8/dist-packages/snakemake/workflow.py", line 816, in execute
auto_report(dag, report, stylesheet=report_stylesheet)
File "/usr/local/lib/python3.8/dist-packages/snakemake/report/__init__.py", line 758, in auto_report
rec.starttime = min(rec.starttime, meta["starttime"])
TypeError: '<' not supported between instances of 'NoneType' and 'int'

# Fixed code
def finished(self, job, keep_metadata=True):
    if not keep_metadata:
        for f in job.expanded_output:
            self._delete_record(self._incomplete_path, f)
        return

    version = str(job.rule.version) if job.rule.version is not None else None
    code = self._code(job.rule)
    input = self._input(job)
    log = self._log(job)
    params = self._params(job)
    shellcmd = job.shellcmd
    conda_env = self._conda_env(job)
    fallback_time = time.time()
    for f in job.expanded_output:
        rec_path = self._record_path(self._incomplete_path, f)
        starttime = os.path.getmtime(rec_path) if os.path.exists(rec_path) else None
        # Sometimes finished is called twice, if so, lookup the previous starttime
        if not os.path.exists(rec_path):
            starttime = self._read_record(self._metadata_path, f).get("starttime", None)
        endtime = f.mtime.local_or_remote() if f.exists else fallback_time
        self._record(
            self._metadata_path,
            {
                "version": version,
                "code": code,
                "rule": job.rule.name,
                "input": input,
                "log": log,
                "params": params,
                "shellcmd": shellcmd,
                "incomplete": False,
                "starttime": starttime,
                "endtime": endtime,
                "job_hash": hash(job),
                "conda_env": conda_env,
                "container_img_url": job.container_img_url,
            },
            f,
        )
        self._delete_record(self._incomplete_path, f)