Skip to content

Metric.read should support PEP 604 syntax for Optional types #250

@msto

Description

@msto

Summary

#168 doesn't seem to have fully implemented #89.

Example

example.py

Contents of example script
#!/usr/bin/env python

from dataclasses import dataclass
from pathlib import Path

from fgpyo.util.metric import Metric


@dataclass(frozen=True, kw_only=True)
class MyMetric(Metric["MyMetric"]):
    foo: str
    bar: str | None

example_tsv: Path = Path("example.tsv")
with example_tsv.open("w") as fout:
    fout.write("foo\tbar\n")
    fout.write("hello\tworld\n")
    fout.write("hola\t\n")

metrics: list[MyMetric] = list(MyMetric.read(example_tsv))
$ pixi add python=3.12 fgpyo=1.3.0
$ pixi run ./example.py
Traceback (most recent call last):
  File "/Users/msto/code/scratch/fgpyo/./example.py", line 20, in <module>
    metrics: list[MyMetric] = list(MyMetric.read(example_tsv))
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/msto/code/scratch/.pixi/envs/default/lib/python3.12/site-packages/fgpyo/util/metric.py", line 286, in read
    instance: Metric[MetricType] = inspect.attr_from(
                                   ^^^^^^^^^^^^^^^^^^
  File "/Users/msto/code/scratch/.pixi/envs/default/lib/python3.12/site-packages/fgpyo/util/inspect.py", line 467, in attr_from
    set_value
AssertionError: Do not know how to convert string to str | None for value: world

Metadata

Metadata

Assignees

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