Skip to content

Conversation

gastmaier
Copy link

@gastmaier gastmaier commented Sep 23, 2025

Description

This PR fixes #1246

Summary

Python's default arguments are evaluated once when the function is defined. Config was instantiated at BaseRobyn class definition and init.py body. Call Config() only inside BaseRobyn, and if not None. At SubRouter, forward the config parameter.

For config.compile_rust_path, access from the class object, and unset to ensure compile_rust_path is called once per config object. Allows to write custom argument parsers without colliding with Config.parser.

This allows to pass implement alternative configs, for example:

from robyn import Robyn, Response as RobynResponse
from robyn.argument_parser import Config as RobynConfig

from .handler import my_route

class AltConfig(RobynConfig):
    def __init__(self) -> None:
        self.disable_openapi = True
        self.dev = False
        self.log_level = "DEBUG"
        self.open_browser = False
        self.fast = 1
        self.workers = 1
        self.processes = 1
        self.compile_rust_path = None

config=AltConfig()
app = Robyn(__file__, config=config)

@app.post("/my-route")
async def route_my_route(request):
    response =  my_route(app.env,  request)
    return RobynResponse(
        status_code=response.status_code,
        headers=response.headers,
        description=response.body,
    )

def main():
    app.start(port=app.env.port)

PR Checklist

Please ensure that:

  • The PR contains a descriptive title
  • The PR contains a descriptive summary of the changes
  • You build and test your changes before submitting a PR.
  • You have added relevant documentation
  • You have added relevant tests. We prefer integration tests wherever possible

Pre-Commit Instructions:

High-level PR Summary

This PR refactors how the Config class is initialized in Robyn to fix a bug (#1246) related to Python's default argument evaluation behavior. Instead of initializing the config at class definition and in __init__.py, it now initializes the config inside the BaseRobyn class only when needed. This change enables users to implement custom config classes and argument parsers without colliding with the default Config.parser. The PR also ensures that compile_rust_path is called only once per config object by unsetting it after use.

⏱️ Estimated Review Time: 5-15 minutes

💡 Review Order Suggestion
Order File Path
1 robyn/__init__.py

Copy link

@recurseml recurseml bot left a comment

Choose a reason for hiding this comment

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

Review by RecurseML

🔍 Review performed on 118660f..448338b

✨ No bugs found, your code is sparkling clean

Python's default arguments are evaluated once when the function is
defined. Config was instantiated at BaseRobyn class definition and
__init__.py body. Call Config() only inside BaseRobyn, and if not None.
At SubRouter, forward the config parameter.

For config.compile_rust_path, access from the class object, and unset to
ensure compile_rust_path is called once per config object. Allows to
write custom argument parsers without colliding with Config.parser.
Copy link

vercel bot commented Sep 23, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
robyn Error Error Sep 23, 2025 7:48pm

Copy link

@recurseml recurseml bot left a comment

Choose a reason for hiding this comment

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

Review by RecurseML

🔍 Review performed on 448338b..448338b

✨ No files to analyze

@sansyrox
Copy link
Member

sansyrox commented Oct 4, 2025

@gastmaier , thanks for the PR. I have been on a break for a while. Will review this PR soon. :D

@sansyrox
Copy link
Member

sansyrox commented Oct 4, 2025

@gastmaier , have you tested this in --dev mode?

@gastmaier
Copy link
Author

There shouldn't be any issues with dev mode, just resolved the multiple instantiation of Config() due to the behaviour of the Python interpreter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[argparse] Don't instantiate Config on import Robyn

2 participants