Back to all reviewers

avoid internal object access

langflow-ai/langflow
Based on 2 comments
Python

Avoid accessing internal object attributes like `__dict__` or using unpacking syntax when cleaner alternatives exist. Instead, prefer stable public methods or direct assignment patterns that don't couple to object internals.

Code Style Python

Reviewer Prompt

Avoid accessing internal object attributes like __dict__ or using unpacking syntax when cleaner alternatives exist. Instead, prefer stable public methods or direct assignment patterns that don’t couple to object internals.

For object attribute access, use stable methods when available:

# Avoid
"usage": response.usage.__dict__ if hasattr(response, "usage") else None

# Prefer
"usage": response.usage.model_dump() if hasattr(response, "usage") else None
# or explicitly map known fields
"usage": {"tokens": response.usage.tokens, "cost": response.usage.cost} if hasattr(response, "usage") else None

For list/tuple assignments, use direct assignment when possible:

# Avoid
outputs = [*BaseFileComponent._base_outputs]

# Prefer  
outputs = BaseFileComponent._base_outputs

This approach reduces coupling to implementation details, improves maintainability, and makes code more resilient to internal API changes.

2
Comments Analyzed
Python
Primary Language
Code Style
Category

Source Discussions