Skip to content

Writers to accept Path | None #249

@nh13

Description

@nh13

In many of our tools, we have optional metric files that are written when an input path is given. This leads to having to have boilerplate for a nullcontext

from contextlib import nullcontext
with MetricWriter[SomeMetric](out_path, SomeMetric) if out_path is not None else nullcontext() as out_writer:
    ...

or having to condition on if the writer is built:

out_writer = (
    None
    if out_path is None
    else (MetricWriter[SomeMetric](filename=out_path, metric_class=SomeMetric))
)

...
if out_writer is not None:
    out_writer.write(...)

Can we instead have the writer accept out_path as either Path or None, such that it can return a new MetricWriter or nullcontext?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions