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

focoos default arguments #35

Closed
giuseppeambrosio97 opened this issue Dec 24, 2024 · 1 comment · Fixed by #36
Closed

focoos default arguments #35

giuseppeambrosio97 opened this issue Dec 24, 2024 · 1 comment · Fixed by #36
Assignees
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@giuseppeambrosio97
Copy link
Contributor

Remove environment variables from default function arguments

Problem

Using environment variables as default arguments in function definitions is problematic because:

  1. Default arguments are evaluated at function definition time, not at runtime
  2. This can lead to confusing behavior where changes to environment variables after module import don't affect the default values
  3. Users may incorrectly assume that changing FOCOOS_CONFIG.focoos_api_key or FOCOOS_CONFIG.default_host_url at runtime will update the default arguments

Bug Example

# Initial config
FOCOOS_CONFIG.focoos_api_key = "key1"

# Import the module - default args are evaluated here
from focoos import FocoosClient

# Change config - users might expect this to affect new client instances
FOCOOS_CONFIG.focoos_api_key = "key2"

# Create new client without explicit api_key
client = FocoosClient()
print(client.api_key)  # Prints "key1", NOT "key2" as users might expect!

Current code

def __init__(
    self,
    api_key: str = FOCOOS_CONFIG.focoos_api_key,
    host_url: str = FOCOOS_CONFIG.default_host_url,
):

Proposed code

def __init__(
    self,
    api_key: str | None = None,
    host_url: str | None = None,
):
    """
    Initializes the Focoos API client.

    Args:
        api_key (str, optional): API key for authentication. 
        host_url (str, optional): Base URL for Focoos API.
    """
    self.api_key = api_key or FOCOOS_CONFIG.focoos_api_key
    self.host_url = host_url or FOCOOS_CONFIG.default_host_url

Benefits

  • More explicit handling of configuration values
  • Runtime changes to environment variables will work as expected
@giuseppeambrosio97 giuseppeambrosio97 added bug Something isn't working documentation Improvements or additions to documentation labels Dec 24, 2024
@giuseppeambrosio97 giuseppeambrosio97 linked a pull request Dec 27, 2024 that will close this issue
4 tasks
@giuseppeambrosio97
Copy link
Contributor Author

giuseppeambrosio97 commented Dec 27, 2024

Replace api_key: str | None = None with api_key: Optional[str] = None for better compatibility with Python versions >=3.8. This ensures broader accessibility for the SDK, even if we later downgrade from 3.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants