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

[BUG] Issue with empty list as default argument to "create_scenario" function #272

Closed
2 of 4 tasks
AshishKuls opened this issue May 18, 2021 · 3 comments · Fixed by #327
Closed
2 of 4 tasks

[BUG] Issue with empty list as default argument to "create_scenario" function #272

AshishKuls opened this issue May 18, 2021 · 3 comments · Fixed by #327
Assignees
Labels
bug Something isn't working
Milestone

Comments

@AshishKuls
Copy link
Member

AshishKuls commented May 18, 2021

Describe the bug

Using an empty list [] as default argument to a function is not correct and causes unwanted behavior.

project_cards_list argument in create_scenario function

def create_scenario(
    base_scenario: dict = {},
    card_directory: str = "",
    tags: [str] = None,
    project_cards_list=[],
    glob_search="",
    validate_project_cards=True,
) -> Scenario:

create a scenario from base scenario:

scenario_1 = Scenario.create_scenario(base_scenario=base_scenario)
scenario_1.add_project_card_from_file(
    os.path.join(STPAUL_DIR, "project_cards", "1_simple_roadway_attribute_change.yml"),
    validate=False
)
scenario_1.apply_all_projects()
scenario_1.applied_projects

['6th St E Road Diet'] is the applied project

Next, create another scenario from base (without any project card):

scenario_2 = Scenario.create_scenario(base_scenario=base_scenario)
scenario_2.get_project_names()

scenario_2 already contains project ['6th St E Road Diet']

This is because the default parameters are only instantiated once. A new list is created once when the function is defined, and the same list is used in each successive call of the create_scenario function.

After creating the scenario_1, Scenario.create_scenario has this as function definition

<function network_wrangler.scenario.Scenario.create_scenario(base_scenario: 'dict' = {}, card_directory: 'str' = '', tags: '[str]' = None, project_cards_list=[<network_wrangler.projectcard.ProjectCard object at 0x00000251D27306A0>], glob_search='', validate_project_cards=True) -> 'Scenario'>

Status

  • Defined
  • Planned
  • Implemented
  • Tested

Triggering line of code

https://github.com/wsp-sag/network_wrangler/blob/master/network_wrangler/scenario.py#L229

Thoughts on resolution

The solution and the standard way of doing it right is to pass default argument as None instead of [] to the function.

def create_scenario(
        base_scenario: dict = {},
        card_directory: str = "",
        tags: [str] = None,
        project_cards_list = None,
        glob_search = "",
        validate_project_cards = True,
    ) -> Scenario:

...

if project_cards_list is None:
    project_cards_list = []

if project_cards_list:
            WranglerLogger.debug(
                "Adding project cards from List.\n{}".format(
                    ",".join([p.project for p in project_cards_list])
                )
            )

scenario = Scenario(base_scenario, project_cards=project_cards_list)
@AshishKuls AshishKuls added the bug Something isn't working label May 18, 2021
@AshishKuls AshishKuls self-assigned this May 18, 2021
@AshishKuls
Copy link
Member Author

cc @e-lo @DavidOry @i-am-sijia

@AshishKuls
Copy link
Member Author

done.

@e-lo e-lo added this to the v1.0 milestone Apr 15, 2024
@e-lo e-lo linked a pull request Apr 15, 2024 that will close this issue
@e-lo
Copy link
Collaborator

e-lo commented Aug 27, 2024

Fixed to None in v1.

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

Successfully merging a pull request may close this issue.

2 participants