Skip to content
Open
Changes from all commits
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
46 changes: 28 additions & 18 deletions graphrag/config/environment_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

def read_key(value: KeyValue) -> str:
"""Read a key value."""
if not isinstance(value, str):
return value.value.lower()
return value.lower()
# It is slightly faster to use isinstance(str) than to fail on hasattr(value, 'value')
# Check most likely and fastest case first
if isinstance(value, str):
return value.lower()
return value.value.lower()


class EnvironmentReader:
Expand Down Expand Up @@ -82,13 +84,12 @@ def str(
default_value: str | None = None,
) -> str | None:
"""Read a configuration value."""
key = read_key(key)
if self.section and key in self.section:
return self.section[key]

return self._read_env(
env_key or key, default_value, (lambda k, dv: self._env(k, dv))
)
key_str = read_key(key)
section = getattr(self, "section", None)
if section and key_str in section:
return section[key_str]
# Avoids lambda creation on every call; use a direct method call
return self._read_env(env_key or key_str, default_value, self._env)

def int(
self,
Expand Down Expand Up @@ -140,16 +141,25 @@ def list(
default_value: list | None = None,
) -> list | None:
"""Parse an list configuration value."""
key = read_key(key)
result = None
if self.section and key in self.section:
result = self.section[key]
key_str = read_key(key)
section = getattr(self, "section", None)
if section and key_str in section:
result = section[key_str]
if isinstance(result, list):
return result

if result is None:
result = self.str(key, env_key)
result = self.str(key, env_key)
if result is not None:
result = [s.strip() for s in result.split(",")]
return [s for s in result if s]
# Avoid allocating a list in a list comprehension just for stripping;
# split and filter in a single pass for better runtime+memory efficiency
# `.split(',')` returns str elements which need to be stripped and filtered;
# accumulate only stripped non-empty once.
splits = result.split(",")
# Using a generator expression and list comprehension with conditional, skipping empty
out = []
for s in splits:
stripped = s.strip()
if stripped:
out.append(stripped)
return out
return default_value