Skip to content

Commit

Permalink
Use @cached_property instead of caching the schema manually
Browse files Browse the repository at this point in the history
  • Loading branch information
atl-ggregson committed Jan 16, 2025
1 parent ff37910 commit 5219723
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions tap_csv/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import typing as t
from datetime import datetime, timezone
from functools import cached_property

from singer_sdk import typing as th
from singer_sdk.streams import Stream
Expand All @@ -28,7 +29,6 @@ def __init__(self, *args, **kwargs):
"""Init CSVStram."""
# cache file_config so we dont need to go iterating the config list again later
self.file_config = kwargs.pop("file_config")
self.stream_schema = None
super().__init__(*args, **kwargs)

def get_records(self, context: Context | None) -> t.Iterable[dict]:
Expand Down Expand Up @@ -122,17 +122,15 @@ def get_rows(self, file_path: str) -> t.Iterable[list]:
with open(file_path, encoding=encoding) as f:
yield from csv.reader(f, dialect="tap_dialect")

@property
@cached_property
def schema(self) -> dict:
"""Return dictionary of record schema.
Dynamically detect the json schema for the stream.
This property is accessed multiple times for each record
so it's important to cache the schema.
so it's important to cache the result.
"""
if self.stream_schema:
return self.stream_schema

properties: list[th.Property] = []
self.primary_keys = self.file_config.get("keys", [])
Expand Down Expand Up @@ -162,5 +160,4 @@ def schema(self) -> dict:
# Cache header for future use
self.header = header

self.stream_schema = th.PropertiesList(*properties).to_dict()
return self.stream_schema
return th.PropertiesList(*properties).to_dict()

0 comments on commit 5219723

Please sign in to comment.