Skip to content

Commit

Permalink
docs(lint): dataclass field types now cause mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Jan 8, 2025
1 parent ff529fe commit 84bbe84
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/dewret/renderers/cwl.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,15 @@ def from_step(cls, step: BaseStep) -> "StepDefinition":
Args:
step: step to convert.
"""
out: list[str] | dict[str, "CommandInputSchema"]
if attrs_has(step.return_type) or (is_dataclass(step.return_type) and isclass(step.return_type)):
out = to_output_schema("out", step.return_type)["fields"]
else:
out = ["out"]
return cls(
name=step.name,
run=step.task.name,
out=(to_output_schema("out", step.return_type)["fields"])
if attrs_has(step.return_type) or is_dataclass(step.return_type)
else ["out"],
out=out,
in_={
key: (
ReferenceDefinition.from_reference(param)
Expand Down Expand Up @@ -463,13 +466,14 @@ def to_output_schema(
for field in attrs_fields(typ)
}
elif is_dataclass(typ):
fields = {
str(field.name): cast(
CommandInputSchema, to_output_schema(field.name, field.type)
)
for field in dataclass_fields(typ)
}

fields = {}
for field in dataclass_fields(typ):
if isinstance(field.type, type) and issubclass(field.type, RawType | AttrsInstance | DataclassProtocol):
fields[str(field.name)] = cast(
CommandInputSchema, to_output_schema(field.name, field.type)
)
else:
raise TypeError("Types of fields in results must also be valid result-types themselves (string-defined types not currently allowed)")
if fields:
output = CommandOutputSchema(
type="record",
Expand Down
6 changes: 5 additions & 1 deletion src/dewret/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,11 @@ def find_field(self: FieldableProtocol, field: str | int, fallback_type: type |
type_hints = get_type_hints(parent_type, localns={parent_type.__name__: parent_type}, include_extras=True)
field_type = type_hints.get(field)
if field_type is None:
field_type = next(iter(filter(lambda fld: fld.name == field, dataclass_fields(parent_type)))).type
dataclass_field_type = next(iter(filter(lambda fld: fld.name == field, dataclass_fields(parent_type)))).type
if isinstance(dataclass_field_type, str):
# TODO: we could ask Python to resolve the str expression for us
raise TypeError("Dataclass fields must be provided as types directly, not str")
field_type = dataclass_field_type
except StopIteration:
raise AttributeError(f"Dataclass {parent_type} does not have field {field}") from None
elif attr_has(parent_type):
Expand Down

0 comments on commit 84bbe84

Please sign in to comment.