Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use generic type for load_tapes #98

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions tapeagents/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from contextlib import contextmanager
from pathlib import Path
from typing import Generator, Type
from typing import Generator, Type, TypeVar

import yaml
from pydantic import TypeAdapter
Expand Down Expand Up @@ -54,8 +54,9 @@ def save_json_tape(tape: Tape, tapes_dir: str, name: str = ""):
with open(fpath, "w") as f:
f.write(tape.model_dump_json(indent=4))

T = TypeVar("T", bound=Tape) # Any subclass of a Tape

def load_tapes(tape_class: Type | TypeAdapter, path: Path | str, file_extension: str = ".yaml") -> list[Tape]:
def load_tapes(tape_class: Type[T], path: Path | str, file_extension: str = ".yaml") -> list[T]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, I didn't know about Type[T]!

if not os.path.exists(path):
raise FileNotFoundError(f"File not found: {path}")
if file_extension not in (".yaml", ".json"):
Expand Down
Loading